Cdoj 1057 Qiushi of elder brother and Flower line tree interval update + interval query

Source: Internet
Author: User

Links:I-Qiushi elder brother and FlowerTime limit:1000MS Memory Limit:65535KB 64bit IO Format:%lld &%llu Submit Status Practice UESTC 1057 Appoint Description:System Crawler (2016-04-19)

Description

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 integers aI, which indicates the initial pleasure value of the first flower.

The third line contains an integer m, which means Qiushi sings the song of M-day.

NextM row with three integers per linel R V, indicating that Qiushi to the flowers in theinterval [l,R], the pleasure value of each flower increased by v.

1≤N,M,AI,|v| ≤100000,1 ≤l≤r≤n< Span id= "mjxp-span-65" class= "Mjxp-mo". /span>

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

3
0 0 0
3
1 2 1
1 2-1
1 3 1

Sample Output

2
0
3

#include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath > #include <vector> #include <queue> #include <stack> #include <map> #include <algorithm > #include <set>using namespace std;typedef long long ll;typedef unsigned long long Ull; #define MM (A, B) memset (A, B , sizeof (a)); const double EPS = 1e-10;const int inf = 0x3f3f3f3f;const double Pi=acos ( -1); const int MAXN=100000;LL ANS[MAXN    +10];struct tree{ll Sum,lazy;    int l,r;        void Fun (ll v) {this->lazy+=v;    sum+=v* (r-l+1);    }//v need to change to a long long or else multiply it to explode int int mid () {return (L+R) >>1;    }}tree[4*maxn+10];void pushdown (int id) {int k=tree[id].lazy;    if (k==0) return;    Tree[2*id].fun (k);    Tree[2*id+1].fun (k); tree[id].lazy=0;} void pushup (int id) {tree[id].sum=tree[2*id].sum+tree[2*id+1].sum;}    void build (int id,int l,int r) {tree[id].l=l;    Tree[id].r=r;    tree[id].lazy=tree[id].sum=0; if (r==l) sCANF ("%lld", &tree[id].sum);        else{int mid= (L+R) >>1;        Build (2*id,l,mid);        Build (2*id+1,mid+1,r);    Pushup (ID);    }}void Update (int id,int l,int R,int v) {if (l<=tree[id].l&&tree[id].r<=r) Tree[id].fun (v);        else {int mid=tree[id].mid ();        Pushdown (ID);        if (l<=mid) update (2*ID,L,R,V);        if (r>mid) update (2*ID+1,L,R,V);    Pushup (ID);     }}ll query (int id,int l,int R) {if (l<=tree[id].l&&tree[id].r<=r) return tree[id].sum;            else {pushdown (ID);            int Mid=tree[id].mid ();            ll suml=0,sumr=0;            if (l<=mid) suml=query (2*id,l,r);            if (r>mid) sumr=query (2*id+1,l,r);            Pushup (ID);        return SUML+SUMR;    }}int Main () {int n,m;        while (~SCANF ("%d", &n)) {build (1,1,n);        scanf ("%d", &m);                for (int i=1;i<=m;i++) {int l,r,v; ScaNF ("%d%d%d", &l,&r,&v);                Update (1,L,R,V);            Ans[i]=query (1,L,R);    } for (int i=1;i<=m;i++) printf ("%lld\n", Ans[i]); } return 0;}

Analysis: template problem, but fun function parameters need to be written in long long form, otherwise burst int

Cdoj 1057 Qiushi of elder brother and Flower line tree interval update + interval query

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.