[线段树入门]hdu1166 hdu1754
线段树网上各种讲,想学习的不需要我的代码,其实我也可以讲一下,不过我要去写昨天和GF研究出来的题。。所以就算了,代码贴一帖,我的代码可读性还是很强的。
1166:
1 #include<iostream>
2 #include<cstdio>
3 #include<iomanip>
4 #include<algorithm>
5 #include<cstring>
6 #include<string>
7 #include<bitset>
8 #include<queue>
9 #include<vector>
10 #include<string>
11 #include<cmath>
12 #include<map>
13 #include<set>
14 #include<stack>
15 #define rep(i,n,m) for(int i=(n);i<=(m);++i)
16 #define re1(i,n) rep(i,1,n)
17 #define re0(i,n) rep(i,0,n)
18 #define RE(a) ((a)*(a))
19 #define SIZE(a) (int((a).size()))
20 #define vv(a) vector<a>
21 #define vi vv(int)
22 #define vl vv(ll)
23 #define vb vv(bool)
24 //count distance 不能用哦,已经定义了。
25 using namespace std;
26 typedef long long ll;
27 template<class T>
28 void inline maxi(T &a,T &b){
29 a=max(a,b);
30 }
31 template<class T>
32 void inline mini(T &a,T &b){
33 a=min(a,b);
34 }
35 void shownum(int n,int m){
36 rep(i,n,m){
37 cout<<setw(6)<<i;
38 }
39 cout<<endl;
40 }
41 template<class T,class P>
42 void fill(T &a,P v){
43 int n=SIZE(a)-1;
44 re0(i,n)
45 a[i]=v;
46 }
47 template<class T,class P>
48 void fill(T *a,P v,int n){
49 re0(i,n-1)
50 a[i]=v;
51 }
52 template<class T>
53 void show(T &a,int n,int m){
54 rep(i,n,m){
55 cout<<setw(6)<<a[i];
56 }
57 cout<<endl;
58 }
59 template<class T>
60 void show(T *a[10],int n,int m){
61 re0(i,n){
62 re0(j,m)
63 cout<<a[i]<<' ';
64 cout<<endl;
65 }
66 }
67 const int maxnum=4*(int(5e4) + 2) + 1;
68 const int maxint=2147483647;
69 struct node{
70 int l, r, v;
71 friend ostream& operator<<(ostream& out,const node& p){
72 out<<p.l<<'#'<<p.r << '#' << p.v;
73 return out;
74 }
75 }tree[maxnum];
76 void build(int i, int j, int place){
77 if(i>j)
78 return;
79 tree[place].l = i;
80 tree[place].r = j;
81 if(i==j)
82 scanf("%d",&tree[place].v);
83 else{
84 int mid = (i+j)/2;
85 build(i,mid,place*2);
86 build(mid+1,j,place*2+1);
87 tree[place].v = tree[place*2].v + tree[place*2+1].v;
88 }
89 }
90 int query(int i,int j,int place){
91 int left = tree[place].l;
92 int right = tree[place].r;
93 int mid = (left + right) / 2;
94 if(i==left && j==right)
95 return tree[place].v;
96 else{
97 if(j <= mid)
98 return query(i, j, place * 2);
99 else if(mid + 1 <= i)
100 return query(i, j, place * 2 + 1);
101 else
102 return query(i, mid, place * 2) + query(mid + 1, j, place * 2 + 1);
103 }
104 }
105 void update(int i, int place, bool add, int v){
106 int left = tree[place].l;
107 int right = tree[place].r;
108 // cout << left << '#' << right << endl;
109 int mid = (left + right) / 2;
110 int p = add ? 1 : -1;
111 tree[place].v += p * v;
112 if(i == left && i == right)
113 return;
114 if(i <= mid){
115 update(i, place * 2, add, v);
116 }else{
117 update(i, place * 2 + 1, add, v);
118 }
119 }
120 void mmain(){
121 int tcase, tnum=0;
122 scanf("%d", &tcase);
123 while(tcase--){
124 int n;
125 scanf("%d", &n);
126 build(1, n, 1);
127 char cmd[20];
128 printf("Case %d:\n", ++tnum);
129 int i,j,v;
130 while(~scanf("%s", cmd)){
131 // show(tree, 1, 2 * n - 1);
132 if(strcmp(cmd, "End")==0)
133 break;
134 if(strcmp(cmd,"Query")==0){
135 scanf("%d%d", &i, &j);
136 printf("%d\n",query(i, j, 1));
137 }else if(strcmp(cmd, "Add") == 0){
138 scanf("%d%d", &i, &v);
139 update(i, 1, true, v);
140 }else{
141 scanf("%d%d", &i, &v);
142 update(i, 1, false, v);
143 }
144 }
145 }
146 }
147 //#define codeforces CODEFORCES
148 #define codeforces_input CODEFORCES_FILE
149 #define MANY_TEST CODEFORCES_MANY_TEST
150 #define MANY_TESST 3
151 int main(){
152 #ifdef codeforces
153 #ifdef codeforces_input
154 freopen("input.txt","r",stdin);
155 freopen("output.txt","w",stdout);
156 #endif
157 #ifdef MANY_TEST
158 re1(wwwwwwwwwwwwwwwwwwwww,MANY_TESST)
159 mmain();
160 return 0;
161 #endif
162 #endif
163 mmain();
164 }
3 #include<iomanip>
4 #include<algorithm>
5 #include<cstring>
6 #include<string>
7 #include<bitset>
8 #include<queue>
9 #include<vector>
10 #include<string>
11 #include<cmath>
12 #include<map>
13 #include<set>
14 #include<stack>
15 #define rep(i,n,m) for(int i=(n);i<=(m);++i)
16 #define re1(i,n) rep(i,1,n)
17 #define re0(i,n) rep(i,0,n)
18 #define RE(a) ((a)*(a))
19 #define SIZE(a) (int((a).size()))
20 #define vv(a) vector<a>
21 #define vi vv(int)
22 #define vl vv(ll)
23 #define vb vv(bool)
24 //count distance 不能用哦,已经定义了。
25 using namespace std;
26 typedef long long ll;
27 template<class T>
28 void inline maxi(T &a,T &b){
29 a=max(a,b);
30 }
31 template<class T>
32 void inline mini(T &a,T &b){
33 a=min(a,b);
34 }
35 void shownum(int n,int m){
36 rep(i,n,m){
37 cout<<setw(6)<<i;
38 }
39 cout<<endl;
40 }
41 template<class T,class P>
42 void fill(T &a,P v){
43 int n=SIZE(a)-1;
44 re0(i,n)
45 a[i]=v;
46 }
47 template<class T,class P>
48 void fill(T *a,P v,int n){
49 re0(i,n-1)
50 a[i]=v;
51 }
52 template<class T>
53 void show(T &a,int n,int m){
54 rep(i,n,m){
55 cout<<setw(6)<<a[i];
56 }
57 cout<<endl;
58 }
59 template<class T>
60 void show(T *a[10],int n,int m){
61 re0(i,n){
62 re0(j,m)
63 cout<<a[i]<<' ';
64 cout<<endl;
65 }
66 }
67 const int maxnum=4*(int(5e4) + 2) + 1;
68 const int maxint=2147483647;
69 struct node{
70 int l, r, v;
71 friend ostream& operator<<(ostream& out,const node& p){
72 out<<p.l<<'#'<<p.r << '#' << p.v;
73 return out;
74 }
75 }tree[maxnum];
76 void build(int i, int j, int place){
77 if(i>j)
78 return;
79 tree[place].l = i;
80 tree[place].r = j;
81 if(i==j)
82 scanf("%d",&tree[place].v);
83 else{
84 int mid = (i+j)/2;
85 build(i,mid,place*2);
86 build(mid+1,j,place*2+1);
87 tree[place].v = tree[place*2].v + tree[place*2+1].v;
88 }
89 }
90 int query(int i,int j,int place){
91 int left = tree[place].l;
92 int right = tree[place].r;
93 int mid = (left + right) / 2;
94 if(i==left && j==right)
95 return tree[place].v;
96 else{
97 if(j <= mid)
98 return query(i, j, place * 2);
99 else if(mid + 1 <= i)
100 return query(i, j, place * 2 + 1);
101 else
102 return query(i, mid, place * 2) + query(mid + 1, j, place * 2 + 1);
103 }
104 }
105 void update(int i, int place, bool add, int v){
106 int left = tree[place].l;
107 int right = tree[place].r;
108 // cout << left << '#' << right << endl;
109 int mid = (left + right) / 2;
110 int p = add ? 1 : -1;
111 tree[place].v += p * v;
112 if(i == left && i == right)
113 return;
114 if(i <= mid){
115 update(i, place * 2, add, v);
116 }else{
117 update(i, place * 2 + 1, add, v);
118 }
119 }
120 void mmain(){
121 int tcase, tnum=0;
122 scanf("%d", &tcase);
123 while(tcase--){
124 int n;
125 scanf("%d", &n);
126 build(1, n, 1);
127 char cmd[20];
128 printf("Case %d:\n", ++tnum);
129 int i,j,v;
130 while(~scanf("%s", cmd)){
131 // show(tree, 1, 2 * n - 1);
132 if(strcmp(cmd, "End")==0)
133 break;
134 if(strcmp(cmd,"Query")==0){
135 scanf("%d%d", &i, &j);
136 printf("%d\n",query(i, j, 1));
137 }else if(strcmp(cmd, "Add") == 0){
138 scanf("%d%d", &i, &v);
139 update(i, 1, true, v);
140 }else{
141 scanf("%d%d", &i, &v);
142 update(i, 1, false, v);
143 }
144 }
145 }
146 }
147 //#define codeforces CODEFORCES
148 #define codeforces_input CODEFORCES_FILE
149 #define MANY_TEST CODEFORCES_MANY_TEST
150 #define MANY_TESST 3
151 int main(){
152 #ifdef codeforces
153 #ifdef codeforces_input
154 freopen("input.txt","r",stdin);
155 freopen("output.txt","w",stdout);
156 #endif
157 #ifdef MANY_TEST
158 re1(wwwwwwwwwwwwwwwwwwwww,MANY_TESST)
159 mmain();
160 return 0;
161 #endif
162 #endif
163 mmain();
164 }
1754
1 #include<iostream>
2 #include<cstdio>
3 #include<iomanip>
4 #include<algorithm>
5 #include<cstring>
6 #include<string>
7 #include<bitset>
8 #include<queue>
9 #include<vector>
10 #include<string>
11 #include<cmath>
12 #include<map>
13 #include<set>
14 #include<stack>
15 #define rep(i,n,m) for(int i=(n);i<=(m);++i)
16 #define re1(i,n) rep(i,1,n)
17 #define re0(i,n) rep(i,0,n)
18 #define RE(a) ((a)*(a))
19 #define SIZE(a) (int((a).size()))
20 #define vv(a) vector<a>
21 #define vi vv(int)
22 #define vl vv(ll)
23 #define vb vv(bool)
24 //count distance 不能用哦,已经定义了。
25 using namespace std;
26 typedef long long ll;
27 template<class T>
28 void inline maxi(T &a,T &b){
29 a=max(a,b);
30 }
31 template<class T>
32 void inline mini(T &a,T &b){
33 a=min(a,b);
34 }
35 void shownum(int n,int m){
36 rep(i,n,m){
37 cout<<setw(6)<<i;
38 }
39 cout<<endl;
40 }
41 template<class T,class P>
42 void fill(T &a,P v){
43 int n=SIZE(a)-1;
44 re0(i,n)
45 a[i]=v;
46 }
47 template<class T,class P>
48 void fill(T *a,P v,int n){
49 re0(i,n-1)
50 a[i]=v;
51 }
52 template<class T>
53 void show(T &a,int n,int m){
54 rep(i,n,m){
55 cout<<setw(6)<<a[i];
56 }
57 cout<<endl;
58 }
59 template<class T>
60 void show(T *a[10],int n,int m){
61 re0(i,n){
62 re0(j,m)
63 cout<<a[i]<<' ';
64 cout<<endl;
65 }
66 }
67 const int maxnum=200000+1;
68 const int maxint=2147483647;
69 struct node{
70 int l,r,v;
71 }tree[4*(maxnum)+1];
72 void build(int i, int j, int place){
73 tree[place].l = i;
74 tree[place].r = j;
75 int mid = (i + j) / 2;
76 if(i == j)
77 scanf("%d",&tree[place].v);
78 else{
79 build(i, mid, place * 2);
80 build(mid + 1, j, place * 2 + 1);
81 tree[place].v = max(tree[place * 2].v, tree[place * 2 + 1].v);
82 }
83 }
84 int query(int i, int j, int place){
85 int left = tree[place].l;
86 int right = tree[place].r;
87 int mid = (left + right) / 2;
88 if(i == left && j == right)
89 return tree[place].v;
90 else{
91 if(j <= mid){
92 return query(i, j, place * 2);
93 }else if(i >= mid + 1){
94 return query(i, j, place * 2 + 1);
95 }else{
96 return max(query(i, mid, place * 2), query(mid + 1, j, place * 2 + 1));
97 }
98
99 }
100 }
101 void update(int i, int place, int v){
102 int left = tree[place].l;
103 int right = tree[place].r;
104 int mid = (left + right) / 2;
105 if(i == left && i == right){
106 tree[place].v = v;
107 return;
108 }else{
109 maxi(tree[place].v,v);
110 }
111 if(i <= mid){
112 update(i, place * 2, v);
113 }else
114 update(i, place * 2 + 1, v);
115 }
116 void mmain(){
117 int n, m;
118 while(~scanf("%d%d", &n, &m)){
119 build(1, n ,1);
120 int i, j, v;
121 re1(u, m){
122 char cmd[2];
123 scanf("%s", cmd);
124 if(cmd[0] == 'Q'){
125 scanf("%d%d", &i, &j);
126 printf("%d\n",query(i, j ,1));
127 }else{
128 scanf("%d%d", &i, &v);
129 update(i, 1, v);
130 }
131 }
132 }
133 }
134 //#define codeforces CODEFORCES
135 #define codeforces_input CODEFORCES_FILE
136 #define MANY_TEST CODEFORCES_MANY_TEST
137 #define MANY_TESST 3
138 int main(){
139 #ifdef codeforces
140 #ifdef codeforces_input
141 freopen("input.txt","r",stdin);
142 freopen("output.txt","w",stdout);
143 #endif
144 #ifdef MANY_TEST
145 re1(wwwwwwwwwwwwwwwwwwwww,MANY_TESST)
146 mmain();
147 return 0;
148 #endif
149 #endif
150 mmain();
2 #include<cstdio>
3 #include<iomanip>
4 #include<algorithm>
5 #include<cstring>
6 #include<string>
7 #include<bitset>
8 #include<queue>
9 #include<vector>
10 #include<string>
11 #include<cmath>
12 #include<map>
13 #include<set>
14 #include<stack>
15 #define rep(i,n,m) for(int i=(n);i<=(m);++i)
16 #define re1(i,n) rep(i,1,n)
17 #define re0(i,n) rep(i,0,n)
18 #define RE(a) ((a)*(a))
19 #define SIZE(a) (int((a).size()))
20 #define vv(a) vector<a>
21 #define vi vv(int)
22 #define vl vv(ll)
23 #define vb vv(bool)
24 //count distance 不能用哦,已经定义了。
25 using namespace std;
26 typedef long long ll;
27 template<class T>
28 void inline maxi(T &a,T &b){
29 a=max(a,b);
30 }
31 template<class T>
32 void inline mini(T &a,T &b){
33 a=min(a,b);
34 }
35 void shownum(int n,int m){
36 rep(i,n,m){
37 cout<<setw(6)<<i;
38 }
39 cout<<endl;
40 }
41 template<class T,class P>
42 void fill(T &a,P v){
43 int n=SIZE(a)-1;
44 re0(i,n)
45 a[i]=v;
46 }
47 template<class T,class P>
48 void fill(T *a,P v,int n){
49 re0(i,n-1)
50 a[i]=v;
51 }
52 template<class T>
53 void show(T &a,int n,int m){
54 rep(i,n,m){
55 cout<<setw(6)<<a[i];
56 }
57 cout<<endl;
58 }
59 template<class T>
60 void show(T *a[10],int n,int m){
61 re0(i,n){
62 re0(j,m)
63 cout<<a[i]<<' ';
64 cout<<endl;
65 }
66 }
67 const int maxnum=200000+1;
68 const int maxint=2147483647;
69 struct node{
70 int l,r,v;
71 }tree[4*(maxnum)+1];
72 void build(int i, int j, int place){
73 tree[place].l = i;
74 tree[place].r = j;
75 int mid = (i + j) / 2;
76 if(i == j)
77 scanf("%d",&tree[place].v);
78 else{
79 build(i, mid, place * 2);
80 build(mid + 1, j, place * 2 + 1);
81 tree[place].v = max(tree[place * 2].v, tree[place * 2 + 1].v);
82 }
83 }
84 int query(int i, int j, int place){
85 int left = tree[place].l;
86 int right = tree[place].r;
87 int mid = (left + right) / 2;
88 if(i == left && j == right)
89 return tree[place].v;
90 else{
91 if(j <= mid){
92 return query(i, j, place * 2);
93 }else if(i >= mid + 1){
94 return query(i, j, place * 2 + 1);
95 }else{
96 return max(query(i, mid, place * 2), query(mid + 1, j, place * 2 + 1));
97 }
98
99 }
100 }
101 void update(int i, int place, int v){
102 int left = tree[place].l;
103 int right = tree[place].r;
104 int mid = (left + right) / 2;
105 if(i == left && i == right){
106 tree[place].v = v;
107 return;
108 }else{
109 maxi(tree[place].v,v);
110 }
111 if(i <= mid){
112 update(i, place * 2, v);
113 }else
114 update(i, place * 2 + 1, v);
115 }
116 void mmain(){
117 int n, m;
118 while(~scanf("%d%d", &n, &m)){
119 build(1, n ,1);
120 int i, j, v;
121 re1(u, m){
122 char cmd[2];
123 scanf("%s", cmd);
124 if(cmd[0] == 'Q'){
125 scanf("%d%d", &i, &j);
126 printf("%d\n",query(i, j ,1));
127 }else{
128 scanf("%d%d", &i, &v);
129 update(i, 1, v);
130 }
131 }
132 }
133 }
134 //#define codeforces CODEFORCES
135 #define codeforces_input CODEFORCES_FILE
136 #define MANY_TEST CODEFORCES_MANY_TEST
137 #define MANY_TESST 3
138 int main(){
139 #ifdef codeforces
140 #ifdef codeforces_input
141 freopen("input.txt","r",stdin);
142 freopen("output.txt","w",stdout);
143 #endif
144 #ifdef MANY_TEST
145 re1(wwwwwwwwwwwwwwwwwwwww,MANY_TESST)
146 mmain();
147 return 0;
148 #endif
149 #endif
150 mmain();
151 }