模板1:点修改:支持查询,更新;
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define INF 0x3f3f3f3f 4 const int maxn = 1000 + 10; 5 int a[maxn], qL, qR, p, v; 6 7 struct SegmentTree { 8 int minv[2*maxn]; 9 SegmentTree () {memset(minv, 0x3f, sizeof(minv));} 10 11 void Init_Tree(int o, int L, int R) { 12 int M = L + (R - L) / 2; 13 if (L == R) { 14 minv[o] = a[L]; 15 return; 16 } 17 Init_Tree(o<<1, L, M); 18 minv[o] = min(minv[o], minv[o<<1]); 19 Init_Tree(o<<1|1, M+1, R); 20 minv[o] = min(minv[o], minv[o<<1|1]); 21 } 22 23 int query(int o, int L, int R) { 24 if (qL <= L && R <= qR) return minv[o]; 25 int M = L + (R - L) / 2, ans = INF; 26 if (qL <= M) ans = min(ans, query(o<<1, L, M)); 27 if (M < qR) ans = min(ans, query(o<<1|1, M+1, R)); 28 return ans; 29 } 30 31 void update(int o, int L, int R) { 32 if (L == R) { 33 minv[o] = v; 34 return; 35 } 36 int M = L + (R - L) / 2; 37 if (p <= M) update(o<<1, L, M); 38 else update(o<<1|1, M+1, R); 39 minv[o] = min(minv[o<<1], minv[o<<1|1]); 40 } 41 42 }; 43 44 SegmentTree STree; 45 46 int main() { 47 int n, q; 48 cin>>n>>q; 49 for (int i = 1; i <= n; ++i) 50 cin>>a[i]; 51 STree.Init_Tree(1, 1, n); 52 cout<<"Init is finished!"<<endl; 53 int x; 54 for (int i = 0; i < q; ++i) { 55 cin>>x; 56 if (x == 1) { 57 cin>>qL>>qR; 58 cout<<STree.query(1, 1, n)<<endl; 59 } 60 else if (x == 2) { 61 cin>>p>>v; 62 STree.update(1, 1, n); 63 } 64 } 65 66 return 0; 67 }
模板2:区间修改,支持查询,区间增减;
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson o<<1 4 #define rson o<<1|1 5 #define INF 0x3f3f3f3f 6 #define M(a, b) memset(a, b, sizeof(a)) 7 const int maxn = 1000 + 10; 8 int y1, y2; 9 int _min, _max, _sum, v; 10 11 struct SegmentTree { 12 int sumv[maxn<<2], minv[maxn<<2], maxv[maxn<<2]; 13 int addv[maxn<<2]; 14 15 SegmentTree () {M(sumv, 0); M(minv, INF); M(maxv, -1); M(addv, 0);} 16 17 void maintain (int o, int L, int R) { 18 sumv[o] = minv[o] = maxv[o] = 0; 19 if (L < R) { 20 minv[o] = min(minv[lson], minv[rson]); 21 maxv[o] = max(maxv[lson], maxv[rson]); 22 sumv[o] = sumv[lson] + sumv[rson]; 23 } 24 minv[o] += addv[o]; maxv[o] += addv[o]; 25 sumv[o] += addv[o] * (R - L + 1); 26 } 27 28 void update(int o, int L, int R) { 29 if (y1 <= L && R <= y2) { 30 addv[o] += v; 31 } else { 32 int M = L + (R - L) / 2; 33 if (y1 <= M) update(lson, L, M); 34 if (M < y2) update(rson, M+1, R); 35 } 36 maintain(o, L, R); 37 } 38 39 void query(int o, int L, int R, int add) { 40 if (y1 <= L && R <= y2) { 41 _sum += sumv[o] + add * (R - L + 1); 42 _min = min(_min, minv[o] + add); 43 _max = max(_max, maxv[o] + add); 44 } else { 45 int M = L + (R - L) / 2; 46 if (y1 <= M) query(lson, L, M, add+addv[o]); 47 if (M < y2) query(rson, M+1, R, add+addv[o]); 48 } 49 } 50 }; 51 52 SegmentTree T; 53 54 int main() { 55 int n, q; 56 cin>>n>>q; 57 for (int i = 1; i <= n; ++i) { 58 cin>>v; 59 y1 = y2 = i; 60 T.update(1, 1, n); 61 } 62 cout<<"Init is finished!"<<endl; 63 char op[10]; 64 for (int i = 0; i < q; ++i) { 65 cin>>op; 66 if (!strcmp(op, "add")) { 67 cin>>y1>>y2>>v; 68 T.update(1, 1, n); 69 } 70 else if (!strcmp(op, "query")) { 71 cin>>y1>>y2; 72 _sum = 0; _min = INF; _max = -1; 73 T.query(1, 1, n, 0); 74 cout<<_min<<" "<<_max<<" "<<_sum<<endl; 75 } 76 else cout<<"This operation is false!"<<endl; 77 } 78 79 return 0; 80 }
模板3:区间修改,支持查询,区间值全部修改;
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define lson o<<1 4 #define rson o<<1|1 5 #define INF 0x3f3f3f3f 6 #define M(a, b) memset(a, b, sizeof(a)) 7 const int maxn = 1000 + 10; 8 int _max, _min, _sum, setv[maxn<<2], v, p, y1, y2; 9 10 struct SegmentTree { 11 int maxv[maxn<<2], minv[maxn<<2], sumv[maxn<<2]; 12 13 SegmentTree() {M(maxv, -1), M(minv, INF), M(sumv, 0); M(setv, -1);} 14 15 void Init(int o, int L, int R) { 16 if (L == R) { 17 cin>>v; 18 minv[o] = maxv[o] = sumv[o] = v; 19 } 20 else { 21 int M = L + (R-L)/2; 22 Init(lson, L, M); 23 Init(rson, M+1, R); 24 maintain(o, L, R); 25 } 26 } 27 28 void maintain(int o, int L, int R) { 29 if (setv[o] >= 0) { 30 sumv[o] = setv[o] * (R-L+1); 31 maxv[o] = minv[o] = setv[o]; 32 } 33 else { 34 sumv[o] = sumv[lson] + sumv[rson]; 35 minv[o] = min(minv[lson], minv[rson]); 36 maxv[o] = max(maxv[lson], maxv[rson]); 37 } 38 } 39 40 void pushdown(int o) { 41 if (setv[o] >= 0) { 42 setv[lson] = setv[rson] = setv[o]; 43 setv[o] = -1; 44 } 45 } 46 47 void update(int o, int L, int R) { 48 if (y1 <= L && R <= y2) { 49 setv[o] = v; 50 } 51 else { 52 pushdown(o); 53 int M = L + (R-L)/2; 54 if (y1 <= M) update(lson, L, M); else maintain(lson, L, M); 55 if (M < y2) update(rson, M+1, R); else maintain(rson, M+1, R); 56 } 57 maintain(o, L, R); 58 } 59 60 void query(int o, int L, int R) { 61 if (setv[o] >= 0) { 62 _sum += setv[o] * (min(R, y2)-max(L, y1)+1); 63 _max = max(_max, setv[o]); 64 _min = min(_min, setv[o]); 65 } 66 else if (y1 <= L && R <= y2) { 67 _sum += sumv[o]; 68 _max = max(_max, maxv[o]); 69 _min = min(_min, minv[o]); 70 } 71 else { 72 int M = L + (R-L+1)/2; 73 if (y1 <= M) query(lson, L, M); 74 if (M < y2) query(rson, M+1, R); 75 } 76 } 77 78 }; 79 80 SegmentTree T; 81 82 int main() { 83 int n, q; 84 cin>>n>>q; 85 T.Init(1, 1, n); 86 cout<<"Init is finished!"<<endl; 87 char op[10]; 88 while(q--) { 89 cin>>op; 90 if (op[0] == 'q') { 91 cin>>y1>>y2; 92 _max = -1, _min = INF, _sum = 0; 93 T.query(1, 1, n); 94 cout<<_max<<" "<<_min<<" "<<_sum<<endl; 95 } 96 else if (op[0] == 's') { 97 cin>>y1>>y2>>v; 98 T.update(1, 1, n); 99 } 100 } 101 102 return 0; 103 }