线段树简单区间更新模板+练习
POJ-3468 – A Simple Problem with Integers(线段树区间更新模板)
http://poj.org/problem?id=3468
Description
You have N integers, A1, A2, … , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.
Input
The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1, A2, … , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
“C a b c” means adding c to each of Aa, Aa+1, … , Ab. -10000 ≤ c ≤ 10000.
“Q a b” means querying the sum of Aa, Aa+1, … , Ab.
Output
You need to answer all Q commands in order. One answer in a line.
翻译:
给定长度为N的数列A,然后输入M行操作指令。
第一类指令形如“C a b c”,表示把数列中第a~b个数都加c。
第二类指令形如“Q a b”,表示询问数列中第a~b个数的值的和。
对于每个询问,输出一个整数表示答案。
输入格式
第一行包含两个整数N和M。
第二行包含N个整数A[i]。
接下来M行表示M条指令,每条指令的格式如题目描述所示。
输出格式
对于每个询问,输出一个整数表示答案。
每个答案占一行。
数据范围
1≤N,M≤105
|c|≤10000,
|A[i]|≤1000000000
Sample Input
10 5 1 2 3 4 5 6 7 8 9 10 Q 4 4 Q 1 10 Q 2 4 C 3 6 3 Q 2 4
Sample Output
4 55 9 15
Hint
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <set> 10 #include <map> 11 #include <math.h> 12 const int INF=0x3f3f3f3f; 13 typedef long long LL; 14 const int mod=1e9+7; 15 //const double PI=acos(-1); 16 const int maxn=1e5+10; 17 using namespace std; 18 //ios::sync_with_stdio(false); 19 // cin.tie(NULL); 20 21 int n,m; 22 struct node 23 { 24 int l; 25 int r; 26 LL lazy;//注意lazy也要开LL 27 LL sum; 28 }SegTree[maxn<<2]; 29 30 void PushUp(int rt) 31 { 32 SegTree[rt].sum=SegTree[rt<<1].sum+SegTree[rt<<1|1].sum; 33 } 34 35 void PushDown(int rt) 36 { 37 if(SegTree[rt].lazy!=0) 38 { 39 SegTree[rt<<1].lazy+=SegTree[rt].lazy; 40 SegTree[rt<<1|1].lazy+=SegTree[rt].lazy; 41 SegTree[rt<<1].sum+=(SegTree[rt<<1].r-SegTree[rt<<1].l+1)*SegTree[rt].lazy; 42 SegTree[rt<<1|1].sum+=(SegTree[rt<<1|1].r-SegTree[rt<<1|1].l+1)*SegTree[rt].lazy; 43 SegTree[rt].lazy=0; 44 } 45 } 46 47 void Build(int l,int r,int rt) 48 { 49 SegTree[rt].l=l; 50 SegTree[rt].r=r; 51 SegTree[rt].lazy=0;//多样例时必须加 52 if(l==r) 53 { 54 scanf("%lld",&SegTree[rt].sum); 55 return; 56 } 57 int mid=(l+r)>>1; 58 Build(l,mid,rt<<1); 59 Build(mid+1,r,rt<<1|1); 60 PushUp(rt); 61 } 62 63 void Update(int L,int R,int add,int rt) 64 { 65 int l=SegTree[rt].l; 66 int r=SegTree[rt].r; 67 if(L<=l&&R>=r) 68 { 69 SegTree[rt].sum+=(SegTree[rt].r-SegTree[rt].l+1)*add; 70 SegTree[rt].lazy+=add; 71 return ; 72 } 73 PushDown(rt);//向下更新lazy 74 int mid=(l+r)>>1; 75 if(L<=mid) 76 Update(L,R,add,rt<<1); 77 if(R>mid) 78 Update(L,R,add,rt<<1|1); 79 PushUp(rt); 80 } 81 82 LL Query(int L,int R,int rt) 83 { 84 int l=SegTree[rt].l; 85 int r=SegTree[rt].r; 86 if(L<=l&&R>=r) 87 { 88 return SegTree[rt].sum; 89 } 90 PushDown(rt);//向下更新lazy 91 int mid=(l+r)>>1; 92 LL sum=0; 93 if(L<=mid) 94 sum+=Query(L,R,rt<<1); 95 if(R>mid) 96 sum+=Query(L,R,rt<<1|1); 97 return sum; 98 } 99 100 int main() 101 { 102 while(~scanf("%d %d",&n,&m)) 103 { 104 Build(1,n,1); 105 for(int i=1;i<=m;i++) 106 { 107 char c[5]; 108 int a,b; 109 scanf("%s %d %d",c,&a,&b); 110 if(c[0]==\'Q\') 111 { 112 printf("%lld\n",Query(a,b,1)); 113 } 114 else if(c[0]==\'C\') 115 { 116 int add; 117 scanf("%d",&add); 118 Update(a,b,add,1); 119 } 120 } 121 } 122 return 0; 123 }
View Code
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <set> 11 #include <map> 12 #include <sstream> 13 const int INF=0x3f3f3f3f; 14 typedef long long LL; 15 const double eps =1e-8; 16 const int mod=1e9+7; 17 const int maxn=1e5+10; 18 using namespace std; 19 20 struct node 21 { 22 int l; 23 int r; 24 LL val; 25 LL lazy; 26 }Tr[maxn<<2]; 27 28 void PU(int u) //pushup 29 { 30 Tr[u].val=Tr[u<<1].val+Tr[u<<1|1].val; 31 } 32 33 void PD(int u) //pushdown 34 { 35 if(Tr[u].lazy) 36 { 37 Tr[u<<1].val+=Tr[u].lazy*(Tr[u<<1].r-Tr[u<<1].l+1); 38 Tr[u<<1|1].val+=Tr[u].lazy*(Tr[u<<1|1].r-Tr[u<<1|1].l+1); 39 Tr[u<<1].lazy+=Tr[u].lazy; Tr[u<<1|1].lazy+=Tr[u].lazy; 40 Tr[u].lazy=0; 41 } 42 } 43 44 void Build(int l,int r,int u) 45 { 46 Tr[u].l=l; Tr[u].r=r; Tr[u].lazy=0; 47 if(l==r) 48 { 49 scanf("%lld",&Tr[u].val); 50 return ; 51 } 52 int mid=(l+r)>>1; 53 Build(l,mid,u<<1); 54 Build(mid+1,r,u<<1|1); 55 PU(u); 56 } 57 58 void Update(int L,int R,int u,int x) 59 { 60 int l=Tr[u].l; int r=Tr[u].r; 61 if(L<=l&&R>=r) 62 { 63 Tr[u].val+=(Tr[u].r-Tr[u].l+1)*x; 64 Tr[u].lazy+=x; 65 return ; 66 } 67 PD(u); 68 int mid=(l+r)>>1; 69 if(L<=mid) Update(L,R,u<<1,x); 70 if(R>mid) Update(L,R,u<<1|1,x); 71 PU(u); 72 } 73 74 LL Query(int L,int R,int u) 75 { 76 int l=Tr[u].l; int r=Tr[u].r; 77 if(L<=l&&R>=r) 78 { 79 return Tr[u].val; 80 } 81 PD(u); 82 int mid=(l+r)>>1; 83 LL sum=0; 84 if(L<=mid) sum+=Query(L,R,u<<1); 85 if(R>mid) sum+=Query(L,R,u<<1|1); 86 return sum; 87 } 88 89 90 int main() 91 { 92 #ifdef DEBUG 93 freopen("sample.txt","r",stdin); 94 #endif 95 96 int n,m; 97 scanf("%d %d",&n,&m); 98 Build(1,n,1); 99 for(int i=1;i<=m;i++) 100 { 101 char op[5]; 102 int a,b,c; 103 scanf("%s %d %d",op,&a,&b); 104 if(op[0]==\'C\') 105 { 106 scanf("%d",&c); 107 Update(a,b,1,c); 108 } 109 else if(op[0]==\'Q\') 110 { 111 printf("%lld\n",Query(a,b,1)); 112 } 113 } 114 115 return 0; 116 }
HDU-1698 Just a Hook(线段树区间修改)
Problem Description
Now Pudge wants to do some operations on the hook.
Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:
For each silver stick, the value is 2.
You may consider the original hook is made up of cupreous sticks.
Input
For each case, the first line contains an integer N, 1<=N<=100,000, which is the number of the sticks of Pudge’s meat hook and the second line contains an integer Q, 0<=Q<=100,000, which is the number of the operations.
Next Q lines, each line contains three integers X, Y, 1<=X<=Y<=N, Z, 1<=Z<=3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where Z=1 represents the cupreous kind, Z=2 represents the silver kind and Z=3 represents the golden kind.
Output
Sample Input
1 10 2 1 5 2 5 9 3
Sample Output
Case 1: The total value of the hook is 24.
该题是最典型的线段树区间修改问题, 需要用到懒惰标记。
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <queue> 9 #include <set> 10 #include <map> 11 #include <math.h> 12 const int INF=0x3f3f3f3f; 13 typedef long long LL; 14 const int mod=1e9+7; 15 //const double PI=acos(-1); 16 const int maxn=1e5+10; 17 using namespace std; 18 //ios::sync_with_stdio(false); 19 // cin.tie(NULL); 20 21 int n,m; 22 struct node 23 { 24 int l; 25 int r; 26 int lazy; 27 int sum; 28 }SegTree[maxn<<2]; 29 30 void PushUp(int rt) 31 { 32 SegTree[rt].sum=SegTree[rt<<1].sum+SegTree[rt<<1|1].sum; 33 } 34 35 void PushDown(int rt) 36 { 37 if(SegTree[rt].lazy!=0) 38 { 39 //该题用=而不是+= 40 SegTree[rt<<1].lazy=SegTree[rt].lazy; 41 SegTree[rt<<1|1].lazy=SegTree[rt].lazy; 42 SegTree[rt<<1].sum=(SegTree[rt<<1].r-SegTree[rt<<1].l+1)*SegTree[rt].lazy; 43 SegTree[rt<<1|1].sum=(SegTree[rt<<1|1].r-SegTree[rt<<1|1].l+1)*SegTree[rt].lazy; 44 SegTree[rt].lazy=0; 45 } 46 } 47 48 void Build(int l,int r,int rt) 49 { 50 SegTree[rt].l=l; 51 SegTree[rt].r=r; 52 SegTree[rt].lazy=0;//多样例时必须加 53 if(l==r) 54 { 55 SegTree[rt].sum=1;//该题不用输入,直接初始为1 56 return; 57 } 58 int mid=(l+r)>>1; 59 Build(l,mid,rt<<1); 60 Build(mid+1,r,rt<<1|1); 61 PushUp(rt); 62 } 63 64 void Update(int L,int R,int C,int rt) 65 { 66 int l=SegTree[rt].l; 67 int r=SegTree[rt].r; 68 if(L<=l&&R>=r) 69 { 70 SegTree[rt].sum=(SegTree[rt].r-SegTree[rt].l+1)*C;//该题用=而不是+= 71 SegTree[rt].lazy=C;//该题用=而不是+= 72 return ; 73 } 74 PushDown(rt);//向下更新lazy 75 int mid=(l+r)>>1; 76 if(L<=mid) 77 Update(L,R,C,rt<<1); 78 if(R>mid) 79 Update(L,R,C,rt<<1|1); 80 PushUp(rt); 81 } 82 83 int Query(int L,int R,int rt) 84 { 85 int l=SegTree[rt].l; 86 int r=SegTree[rt].r; 87 if(L<=l&&R>=r) 88 { 89 return SegTree[rt].sum; 90 } 91 PushDown(rt);//向下更新lazy 92 int mid=(l+r)>>1; 93 int sum=0; 94 if(L<=mid) 95 sum+=Query(L,R,rt<<1); 96 if(R>mid) 97 sum+=Query(L,R,rt<<1|1); 98 return sum; 99 } 100 101 int main() 102 { 103 int T; 104 scanf("%d",&T); 105 for(int k=1;k<=T;k++) 106 { 107 scanf("%d %d",&n,&m); 108 Build(1,n,1); 109 for(int i=1;i<=m;i++) 110 { 111 int a,b,c; 112 scanf("%d %d %d",&a,&b,&c); 113 Update(a,b,c,1); 114 } 115 printf("Case %d: The total value of the hook is %d.\n",k,SegTree[1].sum); 116 } 117 return 0; 118 }
1 #include <stdio.h> 2 #include <string.h> 3 #include <iostream> 4 #include <string> 5 #include <math.h> 6 #include <algorithm> 7 #include <vector> 8 #include <stack> 9 #include <queue> 10 #include <set> 11 #include <map> 12 #include <sstream> 13 typedef long long LL; 14 const int INF = 0x3f3f3f3f;//0x7fffffff; 15 const double eps = 1e-8; 16 const int mod = 1e9+7; 17 const int maxn = 1e5+10; 18 using namespace std; 19 20 struct node 21 { 22 int l, r; 23 LL val; 24 LL lazy; 25 }Tr[maxn<<2]; 26 27 void PU(int u) //Pushup 28 { 29 Tr[u].val = Tr[u<<1].val+Tr[u<<1|1].val; 30 } 31 32 void PD(int u) //Pushdown 33 { 34 if(Tr[u].lazy) 35 { //该题用=而不是+= 36 Tr[u<<1].val = Tr[u].lazy*(Tr[u<<1].r-Tr[u<<1].l+1); 37 Tr[u<<1|1].val = Tr[u].lazy*(Tr[u<<1|1].r-Tr[u<<1|1].l+1); 38 Tr[u<<1].lazy = Tr[u].lazy; Tr[u<<1|1].lazy = Tr[u].lazy; 39 Tr[u].lazy = 0; 40 } 41 } 42 43 void Build(int l, int r, int u) //建树 44 { 45 Tr[u].l = l; Tr[u].r = r; Tr[u].lazy = 0; //多样例时必须加lazy=0 46 if(l == r) 47 { 48 Tr[u].val = 1;//该题不用输入,直接初始为1 49 return ; 50 } 51 int mid = (l+r)>>1; 52 Build(l, mid, u<<1); 53 Build(mid+1, r, u<<1|1); 54 PU(u); 55 } 56 57 void Update(int L, int R, int u, LL x) //区间更新 58 { 59 int l = Tr[u].l; int r = Tr[u].r; 60 if(L<=l && R>=r) 61 { 62 Tr[u].val = x*(r-l+1); //该题用=而不是+= 63 Tr[u].lazy = x; //该题用=而不是+= 64 return ; 65 } 66 PD(u); //向下更新lazy 67 int mid = (l+r)>>1; 68 if(L <= mid) Update(L, R, u<<1, x); 69 if(R > mid) Update(L, R, u<<1|1, x); 70 PU(u); 71 } 72 73 LL Query(int L, int R, int u) //区间查询 74 { 75 int l = Tr[u].l; int r=Tr[u].r; 76 if(L<=l && R>=r) 77 { 78 return Tr[u].val; 79 } 80 PD(u); //向下更新lazy 81 int mid = (l+r)>>1; 82 LL sum = 0; 83 if(L <= mid) sum += Query(L, R, u<<1); 84 if(R > mid) sum += Query(L, R, u<<1|1); 85 return sum; 86 } 87 88 int main() 89 { 90 #ifdef DEBUG 91 freopen("sample.txt","r",stdin); //freopen("data.out", "w", stdout); 92 #endif 93 94 int T; 95 scanf("%d",&T); 96 for(int cs=1;cs<=T;cs++) 97 { 98 int n,m; 99 scanf("%d %d",&n,&m); 100 Build(1,n,1); 101 for(int i=1;i<=m;i++) 102 { 103 int a,b,c; 104 scanf("%d %d %d",&a,&b,&c); 105 Update(a,b,1,c); 106 } 107 printf("Case %d: The total value of the hook is %lld.\n",cs,Tr[1].val); 108 } 109 110 return 0; 111 }