Query interval, the interval modification

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <algorithm>
 4 #include <cstring>
 5 using namespace std;
 6 typedef long long ll;
 7 
 8 const int N=2e5+5;
 9 int n,m;
10 ll lazy[N<<2],sum[N<<2];
11 
12 void PushUp(int rt){
13     sum[rt]=sum[rt<<1]+sum[rt<<1|1];
14 }
15 
16 void PushDown(int rt,int l,int r){
17     if(lazy[rt]){
18         lazy[rt<<1]+=lazy[rt];  //lazy标记往下传
19         lazy[rt<<1|1]+=lazy[rt];
20         ll mid=(l+r)>>1;
21         sum[rt<<1]+=(mid-l+1)*lazy[rt];
22         sum[rt<<1|1]+=(r-mid)*the lazy [RT];
 23 is          the lazy [RT] = 0 ;    // prevent repeated changes cleared down after the transfer 
24      }
 25  }
 26 is  
27  void the Build ( int L, int R & lt, int RT) {
 28      IF (L == R & lt) {Scanf ( " % D " , & SUM [RT]); return ;}
 29      LL MID = (L + R & lt) >> . 1 ;
 30      IF (L <= MID) the Build (L, MID, RT << . 1 );
 31 is      IF (R & lt> MID) the Build (MID + . 1 , R & lt, RT << . 1 | . 1 );
32      Pushup (RT);   // bottom up back 
33 is  }
 34 is  
35  void the Add ( int L, int R & lt, int RT, int L, int R & lt, int V) {
 36      IF (L <= L && R & lt> = R & lt) {
 37 [          SUM [RT] + = (L-R & lt + . 1 ) * V;
 38 is          the lazy [RT] + = V;
 39          return ;
 40      }
 41 is      PUSHDOWN (RT, L, R & lt); // the following code to use the left son , have a son, lazy passed down to mark 
42 is      int MID = (L + R & lt) >> . 1;
 43 is      IF (L <= MID) the Add (L, MID, RT << . 1 , L, R & lt, V);
 44 is      IF (R & lt> MID) the Add (+ MID . 1 , R & lt, RT << . 1 | . 1 , L, R & lt, V);
 45      Pushup (RT);   // Do not forget upward update 
46 is  }
 47  
48 LL Query ( int L, int R & lt, int RT, int L, int R & lt) {
 49      IF (L <= L && R & lt> = R & lt) {
 50          return SUM [RT];
 51 is      }
 52 is      PUSHDOWN (RT, L, R & lt); // to update down by down must
53     ll mid=(l+r)>>1,sum=0;
54     if(L<=mid) sum+=query(l,mid,rt<<1,L,R);
55     if(R>mid) sum+=query(mid+1,r,rt<<1|1,L,R);
56     return sum;
57 }
58 
59 int main()
60 {
61     scanf("%d",&n);
62     Build(1,n,1);
63     scanf("%d",&m);
64     for(int i=1;i<=m;i++){
65         int a,b,c,X;
66         scanf("%d",&a);
67         if(a==1){
68             scanf("%d%d%d",&b,&c,&X);
69             Add(1,n,1,b,c,X);
70         }
71         else if(a==2){
72             scanf("%d%d",&b,&c);
73             printf("%lld\n",query(1,n,1,b,c));
74         }
75     }
76     return 0;
77 }
View Code

 

Guess you like

Origin www.cnblogs.com/qq-1585047819/p/10987799.html