A Simple Problem with Integers http://poj.org/problem?id=3468

 1 #include<cstdio>
 2 #define lrrt int L,int R,int rt
 3 #define iall 1,n,1
 4 #define imid int mid=(L+R)>>1
 5 #define lson L,mid,rt<<1
 6 #define rson mid+1,R,rt<<1|1
 7 typedef __int64 LL;
 8 const int M=100010;
 9 int a[M];
10 struct T{
11     LL sum,lazy;
12 }tree[M<<2];
13 void pushup(int rt){
14     tree[rt].sum=tree[rt<<1].sum+tree[rt<<1|1].sum;
15 }
16 void build(lrrt){
17     tree[rt].lazy=0;
18     if(L==R){
19         tree[rt].sum=a[L];
20         return ;
21     }
22     imid;
23     build(lson);
24     build(rson);
25     pushup(rt);
26 }
27 void pushdown(int mid,lrrt){
28     if(tree[rt].lazy){
29         tree[rt<<1].lazy+=tree[rt].lazy;
30         tree[rt<<1|1].lazy+=tree[rt].lazy;
31         tree[rt<<1].sum+=(mid-L+1)*tree[rt].lazy;
32         tree[rt<<1|1].sum+=(R-mid)*tree[rt].lazy;
33         tree[rt].lazy=0;
34     }
35 }
36 void update(int x,int y,int z,lrrt){
37     if(x<=L&&R<=y){
38         tree[rt].sum+=(R-L+1)*z;
39         tree[rt].lazy+=z;
40         return ;
41     }
42     imid;
43     pushdown(mid,L,R,rt);
44     if(mid>=x) update(x,y,z,lson);
45     if(mid<y)  update(x,y,z,rson);
46     pushup(rt);
47 }
48 LL query(int x,int y,lrrt){
49     if(x<=L&&R<=y) return tree[rt].sum;
50     imid;
51     pushdown(mid,L,R,rt);
52     LL ans=0;
53     if(mid>=x) ans+=query(x,y,lson);
54     if(mid<y)  ans+=query(x,y,rson);
55     return ans;
56 }
57 int main(){
58     int n,m;
59     while(~scanf("%d%d",&n,&m)){
60         for(int i=1;i<=n;i++){
61             scanf("%d",&a[i]);
62         }
63         build(iall);
64         while(m--){
65             char op[4];
66             int x,y,z;
67             scanf("%s%d%d",op,&x,&y);
68             if(op[0]=='C'){
69                 scanf("%d",&z);
70                 update(x,y,z,iall);
71             }
72             else{
73                 printf("%I64d\n",query(x,y,iall));
74             }
75         }
76     }
77     return 0;
78 }
View Code

相关文章:

  • 2021-10-19
  • 2022-12-23
  • 2022-12-23
  • 2021-10-01
  • 2021-06-27
  • 2021-09-21
  • 2021-07-05
猜你喜欢
  • 2021-11-02
  • 2022-12-23
  • 2021-08-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-01
相关资源
相似解决方案