POJ 3468 A Simple Problem with Integers

来源:互联网 发布:怎么寻找心理医生网络 编辑:程序博客网 时间:2024/05/16 08:25

题目链接:http://poj.org/problem?id=3468


题意:有n个数,将一个区间上的数都加上一个值,或者查询一个区间的和。


思路:线段树或者树状数组模板。


树状数组专题链接:http://blog.csdn.net/chy20142109/article/details/50673749

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>using namespace std;#define LL long long#define rep(i,j,k) for(int i = j; i <= k; i++ )#define Rrep(i,j,k) for(int i = j; i >= k; i-- )#define Clean(x,y) memset(x,y,sizeof(x))const int maxn = 100009;int n,q;LL sum[maxn];LL c[maxn];int lowbit(int x){    return x&(-x);}LL query(int x,LL a[]){    LL ans = 0;    while(x)    {        ans+=a[x];        x-=lowbit(x);    }    return ans;}void add(int x,int k,LL a[]){    while(x<=n)    {        a[x]+=k;        x+=lowbit(x);    }}int main(){    Clean(sum,0);    Clean(c,0);    cin>>n>>q;    int temp;    rep(i,1,n)    {        scanf("%d",&temp);        add(i,temp,sum);    }    char op;    int tx,ty,tz;    while(q--)    {        getchar();        op = getchar();        scanf("%d%d",&tx,&ty);        if ( op == 'C' )        {            scanf("%d",&tz);            add(tx,tz,c);            add(ty+1,-tz,c);            add(tx, (1-tx)*tz,sum);            add(ty+1,ty*tz,sum);        }        else    printf("%I64d\n", query(ty,sum) + ty*query(ty,c) - query(tx-1,sum) - (tx-1)*query(tx-1,c)  );    }    return 0;}


0 0
原创粉丝点击