Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5.  
  6. int query(int *arr2, int i, int t, int l, int r, int y)
  7. {
  8. int m, k=0;
  9. if ((r==y)&&(l==t)) k = arr2[i];
  10. else{
  11. m=((y+t)/2);
  12. if (r<=m) k=query(arr2, 2*i+1, t, l, r, m);
  13. else{
  14. if (l > m) k = query(arr2, 2*i+2, m+1, l, r, y);
  15. else k = query(arr2, 2*i+1, t, l, m, m) + query(arr2, 2*i+2, m+1, m+1, r, y); };
  16. };
  17. return k;
  18. }
  19.  
  20. void build(int *arr1, int *arr2, int i, int t, int y, int n)
  21. {
  22. if (t != y){
  23. int m = ((y + t)/2);
  24. build(arr1, arr2, 2*i+1, t , m, n);
  25. build(arr1, arr2, 2*i+2, m+1, y, n);
  26. arr2[i]=arr2[2*i + 1] + arr2[2*i + 2];
  27. }else{
  28. if (n == 1) arr2[i] = 1;
  29. else
  30. if ((t == 0) && (arr1[t] >= arr1[t+1])) arr2[i] = 1;
  31. else
  32. if ((t == n-1) && (arr1[t] >= arr1[t-1])) arr2[i] = 1;
  33. else
  34. if ((t > 0) && (t < n-1) && (arr1[t] >= arr1[t+1]) && (arr1[t] >= arr1[t-1])) arr2[i] = 1;
  35. else
  36. arr2[i] = 0; };
  37. }
  38.  
  39. void update(int *arr1, int *arr2, int j, int i, int t, int y, int n)
  40. {
  41. if (t!=y){
  42. int m=((y + t)/2);
  43. if (j <= m) update(arr1, arr2, j, 2*i+1, t, m, n);
  44. else update(arr1, arr2, j, 2*i+2, m+1, y, n);
  45. arr2[i]=arr2[2*i + 1]+arr2[2*i + 2];
  46. }else{
  47. if (n == 1) arr2[i] = 1;
  48. else
  49. if ((t == 0) && (arr1[t] >= arr1[t+1])) arr2[i] = 1;
  50. else
  51. if ((t == n-1) && (arr1[t] >= arr1[t-1])) arr2[i] = 1;
  52. else
  53. if ((t > 0) && (t < n-1) && (arr1[t] >= arr1[t+1]) && (arr1[t] >= arr1[t-1])) arr2[i] = 1;
  54. else
  55. arr2[i] = 0;
  56. }
  57. }
  58.  
  59.  
  60.  
  61. int main(int argc, char **argv)
  62. {
  63. int n, i;
  64. scanf("%d ", &n);
  65. int *arr1=(int *)calloc(n,sizeof(int ));
  66. int *arr2=(int *)calloc(n * 4,sizeof(int ));
  67. i = 0;
  68. while (i < n){
  69. scanf("%d ", &arr1[i]);
  70. i++;
  71. }
  72.  
  73. build(arr1, arr2, 0,0,n-1,n);
  74. int m, t, y, c, d;
  75. char string[5];
  76. scanf("%d", &m);
  77. i = 0;
  78. while (i < m){
  79. scanf("%s", string);
  80. if (strcmp("PEAK", string) == 0){
  81. scanf("%d %d", &t, &y);
  82. printf("%d \n", query(arr2, 0, 0, t, y, n-1));
  83. }
  84. if (strcmp("UPD", string) == 0){
  85. scanf("%d %d", &c, &d);
  86. arr1[c] = d;
  87. for (int k=c-1; k<c+2; k++) update(arr1, arr2, k, 0, 0, n-1, n);
  88. }
  89. i++;
  90. }
  91. free(arr1);
  92. free(arr2);
  93. return 0;
  94. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement