线段树:
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #define MMAX 200003 5 6 using namespace std; 7 int Q[4*MMAX]; 8 int N, F, ql, qr, p, v; 9 10 void update(int r, int L, int R) 11 { 12 int M = (L+R)/2; 13 if(L == R) 14 Q[r] = v; 15 else{ 16 if(p <= M) 17 update(r*2, L, M); 18 else 19 update(r*2+1, M+1, R); 20 Q[r] = max(Q[r*2], Q[r*2+1]); 21 } 22 }///更新线段树 23 24 int query(int r,int L,int R) 25 { 26 int ans = -1; 27 int M = (L+R)/2; 28 if(ql <= L && R <= qr) 29 return Q[r]; 30 if(qr <= M) 31 return query(r*2, L, M); 32 else if(ql > M) 33 return query(r*2+1, M+1, R); 34 else 35 ans = max(query(r*2, L, M), query(r*2+1, M+1, R)); 36 return ans; 37 }///进行区间查询 38 39 int main() 40 { 41 while(~scanf("%d%d", &N, &F)){ 42 // memset(Q,0,sizeof(Q)); 43 for(int i = 0; i < N; i++){ 44 scanf("%d", &v); 45 p = i + 1; 46 update(1, 1, N); 47 } 48 for(int i = 0; i < F; i++){ 49 char op[5]; 50 int a, b; 51 scanf("%s %d %d", op, &a, &b); 52 if(op[0] == ‘Q‘){ 53 ql = a, qr = b; 54 printf("%d\n", query(1, 1, N));} 55 else { 56 p = a, v = b; 57 update(1, 1, N); 58 } 59 } 60 } 61 return 0; 62 }
HDU 1754 I hate it,布布扣,bubuko.com
原文:http://www.cnblogs.com/zzy9669/p/3894107.html