Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- int query(int *arr2, int i, int t, int l, int r, int y)
- {
- int m, k=0;
- if ((r==y)&&(l==t)) k = arr2[i];
- else{
- m=((y+t)/2);
- if (r<=m) k=query(arr2, 2*i+1, t, l, r, m);
- else{
- if (l > m) k = query(arr2, 2*i+2, m+1, l, r, y);
- else k = query(arr2, 2*i+1, t, l, m, m) + query(arr2, 2*i+2, m+1, m+1, r, y); };
- };
- return k;
- }
- void build(int *arr1, int *arr2, int i, int t, int y, int n)
- {
- if (t != y){
- int m = ((y + t)/2);
- build(arr1, arr2, 2*i+1, t , m, n);
- build(arr1, arr2, 2*i+2, m+1, y, n);
- arr2[i]=arr2[2*i + 1] + arr2[2*i + 2];
- }else{
- if (n == 1) arr2[i] = 1;
- else
- if ((t == 0) && (arr1[t] >= arr1[t+1])) arr2[i] = 1;
- else
- if ((t == n-1) && (arr1[t] >= arr1[t-1])) arr2[i] = 1;
- else
- if ((t > 0) && (t < n-1) && (arr1[t] >= arr1[t+1]) && (arr1[t] >= arr1[t-1])) arr2[i] = 1;
- else
- arr2[i] = 0; };
- }
- void update(int *arr1, int *arr2, int j, int i, int t, int y, int n)
- {
- if (t!=y){
- int m=((y + t)/2);
- if (j <= m) update(arr1, arr2, j, 2*i+1, t, m, n);
- else update(arr1, arr2, j, 2*i+2, m+1, y, n);
- arr2[i]=arr2[2*i + 1]+arr2[2*i + 2];
- }else{
- if (n == 1) arr2[i] = 1;
- else
- if ((t == 0) && (arr1[t] >= arr1[t+1])) arr2[i] = 1;
- else
- if ((t == n-1) && (arr1[t] >= arr1[t-1])) arr2[i] = 1;
- else
- if ((t > 0) && (t < n-1) && (arr1[t] >= arr1[t+1]) && (arr1[t] >= arr1[t-1])) arr2[i] = 1;
- else
- arr2[i] = 0;
- }
- }
- int main(int argc, char **argv)
- {
- int n, i;
- scanf("%d ", &n);
- int *arr1=(int *)calloc(n,sizeof(int ));
- int *arr2=(int *)calloc(n * 4,sizeof(int ));
- i = 0;
- while (i < n){
- scanf("%d ", &arr1[i]);
- i++;
- }
- build(arr1, arr2, 0,0,n-1,n);
- int m, t, y, c, d;
- char string[5];
- scanf("%d", &m);
- i = 0;
- while (i < m){
- scanf("%s", string);
- if (strcmp("PEAK", string) == 0){
- scanf("%d %d", &t, &y);
- printf("%d \n", query(arr2, 0, 0, t, y, n-1));
- }
- if (strcmp("UPD", string) == 0){
- scanf("%d %d", &c, &d);
- arr1[c] = d;
- for (int k=c-1; k<c+2; k++) update(arr1, arr2, k, 0, 0, n-1, n);
- }
- i++;
- }
- free(arr1);
- free(arr2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement