poj 3468 线段树
题目链接:http://poj.org/problem?id=3468
WA 在了数据类型上,seg[]要用longlong,输入也要注意.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cmath> 3 #include <algorithm> 4 #include <iostream> 5 #include <cstring> 6 #include <queue> 7 #include <vector> 8 9 #define maxn 125000 10 #define lson l,mid,u<<1 11 #define rson mid+1,r,u<<1|1 12 using namespace std; 13 14 long long seg[maxn<<2]; 15 long long pau[maxn<<2]; 16 17 void PushDown(int l,int r,int u){ 18 if(pau[u]){ 19 pau[u<<1] += pau[u]; //这一定要更新对; 20 pau[u<<1|1] += pau[u]; 21 int mid = (l + r)>>1; 22 seg[u<<1] += (mid - l + 1) * pau[u]; 23 seg[u<<1|1] += (r - mid) * pau[u]; 24 pau[u] = 0; 25 } 26 } 27 void PushUp(int u){ 28 seg[u] = seg[u<<1] + seg[u<<1|1]; 29 } 30 void build(int l,int r,int u){ 31 pau[u] = 0; 32 if(l == r){ 33 scanf("%lld",&seg[u]); //longlong 要用%lld ,就因为这WA了十几次 34 return; 35 } 36 int mid = (l + r)>>1; 37 build(lson); 38 build(rson); 39 PushUp(u); 40 } 41 void Update(int L,int R,int num,int l,int r,int u){ 42 if(L <= l && r <= R){ 43 pau[u] += num; 44 seg[u] += (long long)(r - l + 1)*num; 45 return; 46 } 47 PushDown(l,r,u); 48 int mid = (l + r)>>1; 49 if(L <= mid) Update(L,R,num,lson); 50 if(R > mid) Update(L,R,num,rson); 51 PushUp(u); 52 } 53 long long Query(int L,int R,int l,int r,int u){ 54 if(L <= l && r <= R){ 55 return seg[u]; 56 } 57 PushDown(l,r,u); 58 int mid = (l + r)>>1; 59 long long ret = 0; 60 if(L <= mid) ret += Query(L,R,lson); 61 if(R > mid) ret += Query(L,R,rson); 62 return ret; 63 } 64 int main() 65 { 66 //if(freopen("input.txt","r",stdin)== NULL) {printf("Error\n"); exit(0);} 67 68 int N,Q; 69 scanf("%d%d",&N,&Q); 70 build(1,N,1); 71 while(Q--){ 72 char query[2]; 73 int a,b,c; 74 scanf("%s",query); 75 if(query[0] == 'C'){ 76 scanf("%d%d%d",&a,&b,&c); 77 Update(a,b,c,1,N,1); 78 } 79 else{ 80 scanf("%d%d",&a,&b); 81 printf("%lld\n",Query(a,b,1,N,1)); 82 } 83 } 84 }