Segment tree template

Input

The first line has an integer n n representing the total number of flowers.

The second line contains n integers a i ai, representing the initial pleasure value of the ith flower.

The third line contains an integer m m, which means that Big Brother Qiushi sang the song of m m days.

Next m m lines, each line contains three integers lrv v, indicating that Big Brother Qiushi sings to the flowers in the interval [ l , r ] [l,r], and the pleasure value of each flower increases v v.

1 n m a i | v | 100000 1≤n , m , ai , | v | ≤100000 , 1 l r n 1≤l≤r≤n。

Output

Output a total of m lines, the ith line represents the sum of the joy value of the flower after Brother Qiushi finished singing on the ith day

 

Interval modification, interval query, summation

  1 //#include <iostream>
  2 #include <cmath>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <algorithm>
  6 //using namespace std;
  7 const int maxn = 1e5+6;
  8 #define LL long long
  9 #define INF 0x7fffffff
 10 
 11 int n,a[maxn],q;
 12 
 13 struct node
 14 {
 15     int l,r;
 16     long long sum,lazy;
 17     void update(long long x)
 18     {
 19         sum+=(r-l+1)*x;
 20         lazy+=x;
 21     }
 22 }tree[maxn*4];
 23 
 24 void push_up(int x)
 25 {
 26     tree[x].sum=tree[x<<1].sum+tree[x<<1|1].sum;
 27 }
 28 
 29 void push_down(int x)
 30 {
 31     int lazyval=tree[x].lazy;
 32     if(lazyval)
 33     {
 34         tree[x<<1].update(lazyval);
 35         tree[x<<1|1].update(lazyval);
 36         tree[x].lazy=0;
 37     }
 38 }
 39 void build(int x,int l,int r)
 40 {
 41     tree[x].l=l,tree[x].r=r;
 42     tree[x].sum=tree[x].lazy=0;
 43     if(l==r)
 44     {
 45         tree[x].sum=a[l];
 46     }
 47     else
 48     {
 49         int mid = (l+r)/2;
 50         build(x<<1,l,mid);
 51         build(x<<1|1,mid+1,r);
 52         push_up(x);
 53     }
 54 }
 55 void update(int x,int l,int r,long long val)
 56 {
 57     int L =tree[x].l, R = tree[x].r;
 58     if(l<=L&&R<=r)
 59     {
 60         tree[x].update(val);
 61     }
 62     else
 63     {
 64         push_down(x);
 65         int mid = (L+R)/2;
 66         if(mid>=l)update(x<<1,l,r,val);
 67         if(r>mid)update(x<<1|1,l,r,val);
 68         push_up(x);
 69     }
 70 }
 71 
 72 long long query(int x,int l,int r)
 73 {
 74     int L =tree[x].l, R = tree[x].r;
 75     if(l<=L&&R<<r)
 76         return tree[x].sum;
 77     else
 78     {
 79         push_down(x);
 80         long long ans=0;
 81         int mid = (L+R)/2;
 82         if(mid>=l)ans+=query(x<<1,l,r);
 83         if(r>mid)ans+=query(x<<1|1,l,r);
 84         push_up(x);
 85         return ans;
 86     }
 87 }
 88 
 89 int main() {
 90     scanf("%d",&n);
 91     for(int i=1;i<=n;i++)
 92         scanf("%d",&a[i]);
 93     build(1,1,n);
 94     scanf("%d",&q);
 95     for(int i=1;i<=q;i++)
 96     {
 97         int l,r,val;
 98         scanf("%d%d%d",&l,&r,&val);
 99         update(1,l,r,val);
100         printf("%lld\n",query(1,l,r));
101     }
102     return 0;
103}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325343670&siteId=291194637