poj 3468 A Simple Problem with Integers

来源:互联网 发布:ubuntu 找不到安装包 编辑:程序博客网 时间:2024/05/22 17:35

题目链接:点击打开链接

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
Q代表查询,后面是查询的区间
C代表修改,后面是修改的区间和要加的值
ps:裸的线段数区间修改查询

注意:用scanf,和long long 不然超时和wa

<span style="font-size:18px;">#include <iostream>#include<cstdio>#include<cstring>#include<cstdlib>using namespace std;struct node{   int  a,b;    long long  delta;    long long sum;    struct node *left,*right;}*cur;long long  A[100005];void updata(node *cur){    cur->left->sum+=cur->delta*(cur->left->b-cur->left->a);    cur->right->sum+=cur->delta*(cur->right->b-cur->right->a);    cur->left->delta+=cur->delta;    cur->right->delta+=cur->delta;    cur->delta=0;}void build(node *cur,int l,int r){    cur->a=l;    cur->b=r;    cur->delta=0;    if(l+1==r)cur->sum=A[l];    if(l+1<r)    {        cur->left=new node;        cur->right=new node;        int  mid=(l+r)/2;        build(cur->left,l,mid);        build(cur->right,mid,r);        cur->sum=cur->left->sum+cur->right->sum;    }    else cur->left=cur->right=NULL;}long long  query(node *cur,int l,int r){    if(l<=cur->a&&cur->b<=r)    {        return cur->sum;    }    else    {        if(cur->delta!=0)        {            updata(cur);        }        long long  ans=0;        int mid=(cur->a+cur->b)/2;        if(l<mid)        ans+=query(cur->left,l,r);        if(r>mid)        ans+=query(cur->right,l,r);        return ans;    }}void change(node *cur,int l,int r,int delta){    if(l<=cur->a&&cur->b<=r)    {        cur->sum+=delta*(cur->b-cur->a);        cur->delta+=delta;    }    else    {        if(cur->delta!=0)        {            updata(cur);        }        int mid=(cur->a+cur->b)/2;        if(l<mid)        {            change(cur->left,l,r,delta);        }        if(r>mid)        {            change(cur->right,l,r,delta);        }        cur->sum=cur->left->sum+cur->right->sum;    }}int main(){    int n,q;    scanf("%d%d",&n,&q);    for(int i=0;i<n;i++)    {       scanf("%lld",&A[i]);    }    cur=new node ;    build(cur,0,n);    char ch[5];    int a,b,c;    for(int i=0;i<q;i++)    {        scanf("%s",ch);        if(ch[0]=='Q')        {            scanf("%d%d",&a,&b);            a=a-1;            long long  sum=query(cur,a,b);           printf("%lld\n",sum);        }        else if(ch[0]=='C')        {            scanf("%d%d%d",&a,&b,&c);            a=a-1;            change(cur,a,b,c);        }    }    return 0;}</span>


0 0