poj 3468 A Simple Problem with Integers(区间更新 区间求和)

来源:互联网 发布:java线程面试题 编辑:程序博客网 时间:2024/05/16 08:18

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072KTotal Submissions: 80012 Accepted: 24687Case Time Limit: 2000MS

Description

You have N integers, A1A2, ... , AN. You need to deal with two kinds of operations. One type of operation is to add some given number to each number in a given interval. The other is to ask for the sum of numbers in a given interval.

Input

The first line contains two numbers N and Q. 1 ≤ N,Q ≤ 100000.
The second line contains N numbers, the initial values of A1A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of AaAa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of AaAa+1, ... , Ab.

Output

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

Sample Input

10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 4

Sample Output

455915

Hint

The sums may exceed the range of 32-bit integers.



给出N个数的初始值  两种操作 一种是把一个区间内所有的所有的数都加c 一种是求出一个区间的和

#include <cstdio>#include <iostream>#include <cstring>#include <cmath>#include <algorithm>#include <string.h>#include <string>#include <vector>#include <queue>#define MEM(a,x) memset(a,x,sizeof a)#define eps 1e-8#define MOD 10009#define MAXN 100010#define MAXM 100010#define INF 99999999#define ll __int64#define bug cout<<"here"<<endl#define fread freopen("ceshi.txt","r",stdin)#define fwrite freopen("out.txt","w",stdout)using namespace std;struct node{    ll l,r;    ll lazy,num;};node no[MAXN*4];ll a[MAXN];void build(ll l,ll r,ll k){    if(l==r)    {        no[k].l=l;        no[k].r=r;        no[k].num=a[l];        no[k].lazy=0;        return;    }    ll mid=(l+r)/2;    build(l,mid,k*2);    build(mid+1,r,k*2+1);    no[k].l=l;    no[k].r=r;    no[k].lazy=0;    no[k].num=no[k*2].num+no[k*2+1].num;}void pushdown(ll k){    if(no[k].lazy)    {        no[k*2].lazy+=no[k].lazy;        no[k*2].num+=(no[k*2].r-no[k*2].l+1)*no[k].lazy;        no[k*2+1].lazy+=no[k].lazy;        no[k*2+1].num+=(no[k*2+1].r-no[k*2+1].l+1)*no[k].lazy;        no[k].lazy=0;        return;    }}void update(ll l,ll r,ll num,ll k){    if(no[k].l>=l&&no[k].r<=r)    {        ll len=no[k].r-no[k].l+1;        no[k].num+=len*num;        no[k].lazy+=num;        return;    }    pushdown(k);    ll mid=(no[k].r+no[k].l)/2;    if(r<=mid) update(l,r,num,k*2);    else if(l>mid) update(l,r,num,k*2+1);    else    {        update(l,mid,num,k*2);        update(mid+1,r,num,k*2+1);    }    no[k].num=no[k*2].num+no[k*2+1].num;}ll ans;void query(ll l,ll r,ll k){    if(no[k].l>=l&&no[k].r<=r)    {        ans+=no[k].num;        return;    }    pushdown(k);    int mid=(no[k].l+no[k].r)/2;    if(r<=mid) query(l,r,k*2);    else if(l>mid) query(l,r,k*2+1);    else    {        query(l,mid,k*2);        query(mid+1,r,k*2+1);    }    no[k].num=no[k*2].num+no[k*2+1].num;}int main(){//    fread;    ll n,q;    while(scanf("%I64d%I64d",&n,&q)!=EOF)    {//        cout<<n<<q<<endl;        for(ll i=1;i<=n;i++)            scanf("%I64d",&a[i]);        build(1,n,1);        while(q--)        {            char ch[10];            ll l,r,num;            scanf("%s",ch);            if(ch[0]=='C')            {                scanf("%I64d%I64d%I64d",&l,&r,&num);                update(l,r,num,1);            }            else            {                scanf("%I64d%I64d",&l,&r);//                cout<<l<<"  "<<r<<endl;                ans=0;                query(l,r,1);                printf("%I64d\n",ans);            }        }    }    return 0;}




0 0
原创粉丝点击