// 带飞网址 https://vjudge.net/article/187
专题七 线段树
HDU 1166 敌兵布阵
HDU 1754 I Hate It
√ POJ 3468 A Simple Problem with Integers
POJ 2528 Mayor's posters
HDU 1698 Just a Hook
ZOJ 1610 Count the Colors
√ POJ 3264 Balanced Lineup
HDU 4027 Can you answer these queries?
HDU 1540 Tunnel Warfare
HDU 3974 Assign the task
HDU 4578 Transformation
HDU 4614 Vases and Flowers
HDU 4553 约会安排
POJ 1177 Picture
HDU 1255 覆盖的面积
HDU 1542 Atlantis
HDU 3642 Get The Treasury
// poj 3468 简单板子
1 #include2 3 const int MAXN = 100000; 4 typedef long long ll; 5 6 ll ans; 7 struct Segmemt_tree { 8 int l, r; 9 ll f, w; 10 } tree[MAXN*4+1]; 11 12 inline void build(int k, int ll, int rr) { 13 tree[k].l = ll; tree[k].r = rr; 14 if(tree[k].l == tree[k].r) { 15 scanf("%lld", &tree[k].w); 16 return ; 17 } 18 int m = (ll + rr) / 2; 19 build(k*2, ll, m); 20 build(k*2+1, m+1, rr); 21 tree[k].w = tree[k*2].w + tree[k*2+1].w; 22 } 23 24 inline void down(int k) { 25 tree[k*2].f += tree[k].f; 26 tree[k*2+1].f += tree[k].f; 27 tree[k*2].w += tree[k].f * (tree[k*2].r - tree[k*2].l + 1); 28 tree[k*2+1].w += tree[k].f * (tree[k*2+1].r - tree[k*2+1].l + 1); 29 tree[k].f = 0; 30 } 31 32 inline void query_interval(int k, int x, int y) { 33 if(tree[k].l >= x && tree[k].r <= y) { 34 ans += tree[k].w; 35 return ; 36 } 37 if(tree[k].f) down(k); 38 int m = (tree[k].l + tree[k].r) / 2; 39 if(x <= m) query_interval(k*2, x, y); 40 if(y > m) query_interval(k*2+1, x, y); 41 } 42 43 inline void change_interval(int k, int x, int y, ll v) { 44 if(tree[k].l >= x && tree[k].r <= y) { 45 tree[k].w += (tree[k].r - tree[k].l + 1) * v; 46 tree[k].f += v; 47 return ; 48 } 49 if(tree[k].f) down(k); 50 int m = (tree[k].l + tree[k].r) / 2; 51 if(x <= m) change_interval(k*2, x, y, v); 52 if(y > m) change_interval(k*2+1, x, y, v); 53 tree[k].w = tree[k*2].w + tree[k*2+1].w; 54 } 55 56 int main() { 57 int n, m; 58 scanf("%d%d", &n, &m); 59 build(1, 1, n); 60 int x, y; 61 ll v; 62 char ch; 63 for(int i = 0; i != m; ++i) { 64 getchar(); 65 scanf("%c", &ch); 66 ans = 0; 67 if(ch == 'Q') { 68 scanf("%d%d", &x, &y); 69 query_interval(1, x, y); 70 printf("%lld\n", ans); 71 } 72 else { 73 scanf("%d%d%lld", &x, &y, &v); 74 change_interval(1, x, y, v); 75 } 76 } 77 return 0; 78 }
// poj 3264 简单板子(静态数据)区间最值 含ST(RMQ)解法
1 #include// poj3264 (静态数据)区间最值(最大或最小) 线段树 2 #include // memory 2664k | time 2157ms 3 using namespace std; 4 5 const int MAXN = 50000+5; 6 7 struct segmemt_tree { 8 int l, r, w, maxn, minn; 9 } tree[4*MAXN+1]; 10 11 inline void build(int k, int l, int r) { 12 tree[k].l = l; tree[k].r = r; 13 if(tree[k].l == tree[k].r) { 14 scanf("%d", &tree[k].w); 15 tree[k].maxn = tree[k].w; 16 tree[k].minn = tree[k].w; 17 return ; 18 } 19 int m = (l + r) / 2; 20 build(k*2, l, m); 21 build(k*2+1, m+1, r); 22 tree[k].maxn = max(tree[k*2].maxn, tree[k*2+1].maxn); 23 tree[k].minn = min(tree[k*2].minn, tree[k*2+1].minn); 24 } 25 26 inline int get_max(int k, int x, int y) { 27 if(tree[k].l >= x && tree[k].r <= y) { 28 return tree[k].maxn; 29 } 30 int ans = 0; 31 int m = (tree[k].l + tree[k].r) / 2; 32 if(x <= m) ans = max(ans, get_max(k*2, x, y)); 33 if(y > m) ans = max(ans, get_max(k*2+1, x, y)); 34 return ans; 35 } 36 37 inline int get_min(int k, int x, int y) { 38 if(tree[k].l >= x && tree[k].r <= y) { 39 return tree[k].minn; 40 } 41 int ans = 1000002; 42 int m = (tree[k].l + tree[k].r) / 2; 43 if(x <= m) ans = min(ans, get_min(k*2, x, y)); 44 if(y > m) ans = min(ans, get_min(k*2+1, x, y)); 45 return ans; 46 } 47 48 int main() { 49 int n, m; 50 scanf("%d%d", &n, &m); 51 build(1, 1, n); 52 int x, y; 53 for(int i = 0; i != m; ++i) { 54 scanf("%d%d", &x, &y); 55 printf("%d\n", get_max(1, x, y)-get_min(1, x, y)); 56 } 57 return 0; 58 } 59 // #include // poj3264 ST(RMQ)解法 60 // #include // memory 13376k time3704ms 61 // #include 62 // #include 63 // using namespace std; 64 65 // const int MAXN = 5e4+5; 66 67 // int n, m; // n个数 m次查询 68 // int a[MAXN]; 69 // int maxn[MAXN][32];// f[i][j] 表示从第i位起的 2^j 个数中的最大值 区间[i, i+(1< 70 // int minn[MAXN][32]; 71 72 // void ST() { 73 // for(int i = 1; i <= n; ++i) maxn[i][0] = minn[i][0] = a[i]; 74 // int m = (int)log2(n*1.0)/log2(2*1.0); 75 // for(int j = 1; j <= m; ++j) { 76 // for(int i = 1; i+(1< 77 // maxn[i][j] = max(maxn[i][j-1], maxn[i+(1<<(j-1))][j-1]); 78 // minn[i][j] = min(minn[i][j-1], minn[i+(1<<(j-1))][j-1]); 79 // } 80 // } 81 // } 82 83 // int query(int l, int r) { 84 // int k = log2(r-l+1)/log2(2.0); 85 // return max(maxn[l][k], maxn[r-(1< 86 // } 87 88 // int main() { 89 // scanf("%d%d", &n, &m); 90 // for(int i = 1; i <= n; ++i) { 91 // scanf("%d", &a[i]); 92 // } 93 // ST(); 94 // int l, r; 95 // for(int i = 0; i != m; ++i) { 96 // scanf("%d%d", &l, &r); 97 // printf("%d\n", query(l, r)); 98 // } 99 // return 0; 100 // }