B-Qiushi brother and Flower Time limit:3000/1000ms (java/others) Memory limit:65535/65535kb (java/others)SubmitStatus
Qiushi elder brother is a gentle person, day to listen to singer night drunk sleep, but before the moon is flowers.
So Qiushi elder brother carefully took care of a lot of flowers. Now all the flowers are lined up and each flower has a pleasant value.
Qiushi's brother sings to a continuous flower every day, and the pleasure of these flowers increases by the same value v(v may be negative).
At the same time he wanted to know how much each time he sang songs the sum of the pleasant values of successive flowers.
Input
The first line has an integer n , which represents the total number of flowers.
The second line contains n integer < Span id= "mathjax-span-610" class= "math" >ai , which represents < Span id= "mathjax-span-615" class= "math" > i The initial pleasure value of a flower.
The third line contains an integer m, which means Qiushi sings the song of m -day.
NextmRows with three integers per lineL R v, means that Qiushi brother to the [l,R] This range of flowers singing, each flower's pleasure value increased v.
1≤N,m,ai,| V| ≤100000 ,1≤l≤r≤n.
Output
Output a total of m -line, the line i indicates that Qiushi eldest brother to complete the first Day of the song, the sum of the joy of the flower.
Sample Input and output
Sample Input |
Sample Output |
30 0 031 2 11 2-11 3 1 |
203 |
Problem Solving Report:
There is nothing to say, line tree template basic problem. The only thing that needs attention is long long.
Of course I am using a tree-like array:
The operation of the topic is an interval update-the interval query < query point is also an interval > type, which can be implemented using a tree array.
C[i] for the co-renewal of flowers from 1 to I.
First consider the update, add[l,r,v]→c[r] + = V, c[l-1]-=v;
SUM[X] = sum of the value of the flower from 1 to x.
SUM[X] = c[1]*1 + c[2]*2 + c[3] * 3 ... + c[x] * x + (c[x+1] + .... C[n]) *x;
= (Segema) f (x) + (Segema) g (x) * x;
= Two tree-like arrays, one maintenance c[i]*i, one maintenance c[i]
Then consider the interval query:
Query (l, R)
= Sum[r]-SUM[L-1]
1#include <iostream>2#include <cstring>3typedefLong Longll;4 using namespacestd;5 Const intMAXN = 1e5 + -;6 ll N;7 ll F[MAXN],G[MAXN];8 9 Ten Inline ll Lowbit (ll idx) One { A returnIDX & (-idx); - } - the voidUpdataf (ll idx,ll Res) - { - if(!idx) - return; + while(IDX <=N) - { +F[IDX] + =Res; AIDX + =lowbit (IDX); at } - } - - voidUpdatag (ll idx,ll Res) - { - if(!idx) in return; - while(IDX <=N) to { +G[IDX] + =Res; -IDX + =lowbit (IDX); the } * } $ Panax Notoginseng ll SUMF (ll idx) - { thell res =0; + while(Idx >0) A { theRes + =F[idx]; +IDX-=lowbit (IDX); - } $ returnRes; $ } - - ll SUMG (ll idx) the { -ll res =0;Wuyi while(Idx >0) the { -Res + =G[idx]; WuIDX-=lowbit (IDX); - } About returnRes; $ } - - intMainintargcChar*argv[]) - { A ll M; +scanf"%lld%lld",&n,&m); theMemset (F,0,sizeof(f)); -Memset (G,0,sizeof(g)); $ for(intj =1; J <= N; ++j) the { the ll v; thescanf"%lld",&v); theUpdataf (j,v*j); -Updataf (J-1,-v* (J-1)); in Updatag (j,v); theUpdatag (J-1,-v); the } About while(m--) the { the ll I,j,v; thescanf"%lld%lld%lld",&i,&j,&v); +Updataf (j,v*j); -UPDATAF (I-1,-v* (i-1)); the Updatag (j,v);BayiUpdatag (I-1,-v); thei--; thell res1 = SUMF (i) + i* (SUMG (N)-SUMG (i)); -ll res2 = SUMF (j) + j* (SUMG (N)-SUMG (j)); -printf"%lld\n", Res2-res1); the } the return 0; the}
Uestc_ Qiushi brother and Flower Uestc Training for Data Structures<problem b>