标签:
注意容易写错的:update和query的闭开区间及query的递归。
1 #include <cstdio> 2 #include <iostream> 3 #include <algorithm> 4 #include <cstring> 5 #include <cmath> 6 using namespace std; 7 const int maxn = 1000000 + 10; 8 const int maxn3 = maxn * 3; 9 const int INF = -1u >> 1; 10 int A[maxn], Minv[maxn3], Maxv[maxn3], Sumv[maxn3], x, v, ql, qr, _min, _max, _sum; 11 int n, Q, tt = 0; 12 char tp; 13 void fresh(int o, int lc, int rc){ 14 Minv[o] = min(Minv[lc], Minv[rc]); 15 Maxv[o] = max(Maxv[lc], Maxv[rc]); 16 Sumv[o] = Sumv[lc] + Sumv[rc]; 17 return ; 18 } 19 void build(int o, int L, int R){ 20 if(L == R) Sumv[o] = Minv[o] = Maxv[o] = A[L]; 21 else{ 22 int M = L + R >> 1, lc = o << 1, rc = lc | 1; 23 build(lc, L, M); 24 build(rc, M + 1, R); 25 fresh(o, lc, rc); 26 } 27 return ; 28 } 29 void update(int o, int L, int R){ 30 if(L == R) Minv[o] = Maxv[o] = Sumv[o] = v; 31 else{ 32 int M = L + R >> 1, lc = o << 1, rc = lc | 1; 33 if(x <= M) update(lc, L, M); 34 else update(rc, M + 1, R); 35 fresh(o, lc, rc); 36 } 37 return ; 38 } 39 void query(int o, int L, int R){ 40 if(ql <= L && R <= qr){ 41 _min = min(_min, Minv[o]); 42 _max = max(_max, Maxv[o]); 43 _sum = _sum + Sumv[o]; 44 } 45 else{ 46 int M = L + R >> 1, lc = o << 1, rc = lc | 1; 47 if(ql <= M) query(lc, L, M); 48 if(qr > M) query(rc, M + 1, R); 49 } 50 return ; 51 } 52 void read(int &x){ 53 x = 0; int sig = 1; char ch = getchar(); 54 while(!isdigit(ch)) { if(ch == ‘-‘) sig = -1; ch = getchar(); } 55 while(isdigit(ch)) x = 10 * x + ch - ‘0‘, ch = getchar(); 56 x *= sig; return ; 57 } 58 void read(char& x){ 59 x = getchar(); 60 while(!isalpha(x)) x = getchar(); 61 return ; 62 } 63 void init(){ 64 read(n); 65 for(int i = 1; i <= n; i ++) read(A[i]); 66 build(1, 1, n); 67 read(Q); 68 return ; 69 } 70 void work(){ 71 while(Q--){ 72 read(tp); read(ql); read(qr); 73 if(tp == ‘Q‘){ 74 _sum = 0; _max = -INF; _min = INF; 75 query(1, 1, n); 76 printf("MaxNum: %d, MinNum: %d, Sum: %d\n", _max, _min, _sum); 77 } 78 else{ 79 x = ql; v = qr; 80 update(1, 1, n); 81 } 82 } 83 return ; 84 } 85 void print(){ 86 87 return ; 88 } 89 int main(){ 90 init(); 91 work(); 92 print(); 93 return 0; 94 }
标签:
原文地址:http://www.cnblogs.com/chxer/p/4392888.html