PKU 3468 A simple problem with integers

Source: Internet
Author: User

A simple problem with integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 70482 Accepted: 21735
Case Time Limit: 2000MS

Description

You have n integers, a1,a2, ..., an. You need to deal with both kinds of operations. One type of operation is to add some given number to each number in a given interval. The other are to ask for the sum of numbers in a given interval.

Input

The first line contains numbers N and Q . 1≤ N , Q ≤100000.
The second line contains N numbers, the initial values of A 1 , Em>a 2 , ..., an . -1000000000≤ ai ≤1000000000.
Each of the next Q lines represents an operation.
"C a B c " means adding C to each of Aa , aa +1 , ..., ab . -10000≤ C ≤10000.
"Q a B " means querying the sum of aa , aa +1 , ..., ab .

Output

You need to answer all Q commands in order. One answer in a line.

Sample Input

2 3 4 5 6 7 8 9 10Q 4 4
Q 1 10Q 2 4C 3 6 3Q 2 4
Sample Output
455915
Hint
The sums may exceed the range of 32-bit integers.
Segment tree interval sum, just understand, or understanding is not thorough enough. There will be time to fill up later.
#include <cstdio> #include <algorithm>using namespace std; #define Lson L, M, RT << 1#define Rson m + 1 , R, RT << 1 | 1#define LL __int64const int maxn = 111111; LL add[maxn<<2]; LL sum[maxn<<2];void pushup (int rt) {Sum[rt] = sum[rt<<1] + sum[rt<<1|1];}          void pushdown (int rt,int m) {if (Add[rt]) {add[rt<<1]+=add[rt];          add[rt<<1|1]+=add[rt];          Sum[rt<<1]+=add[rt]* (M-(m>>1));          sum[rt<<1|1]+=add[rt]* (m>>1);   add[rt]=0;        }}void Build (int l,int R,int RT) {Add[rt] = 0;//empty operation if (L = = r) {scanf ("%lld", &sum[rt]);    return;    } int m = (L + r) >> 1;    Build (Lson);    Build (Rson); Pushup (RT);} void update (int l,int r,int c,int l,int r,int RT) {if (L <= l && R <= R)//query {ADD[RT] + = c;//Storage        Save update operation, do not operate on child nodes Sum[rt] + = (LL) c * (r-l + 1);    return; } pushdown (RT, r-l + 1);//For previous delay updatesCorrespondence processing int m = (L + r) >> 1;    if (l <= m) update (L, R, C, Lson);    if (M < R) Update (L, R, C, Rson); Pushup (RT);}    LL query (int l,int r,int l,int r,int RT) {if (l<=l &&r<=r) {return SUM[RT];     } pushdown (RT, r-l + 1);     int m= (L+R) >>1;     LL ans=0;     if (l<=m) ans+=query (L,r,lson);     if (m<r) ans+=query (L,r,rson); return ans;}     int main () {int n,q;     int a,b,c;         while (scanf ("%d%d", &n,&q)!=eof) {build (1,n,1);         Char s[2];             while (q--) {scanf ("%s", s);                 if (s[0]== ' Q ') {scanf ("%d%d", &a,&b);             printf ("%i64d\n", Query (a,b,1,n,1));                 } else {scanf ("%d%d%d", &a,&b,&c);             Update (a,b,c,1,n,1); }}} return 0;}

PKU 3468 A simple problem with integers

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.