poj 3468 (区间修改 区间查询)

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
Total Submissions:147133   Accepted: 45718
Case 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 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4

Sample Output

4
55
9
15

Hint

The sums may exceed the range of 32-bit integers.
#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
#include<cstring>

using namespace std;
int n,m;
const int N=1e5+10;
long long s1[N],s2[N];
int lowbit(int x)
{
    return x&(-x);
}

void updata(int p,long long x)
{
    for(int i=p;i<=n;i+=lowbit(i)){
        s1[i]+=x;
        s2[i]+=x*p;
    }
}
long long sum(int p)
{
    long long ans=0;
    for(int i=p;i>0;i-=lowbit(i)){
        ans+=s1[i]*(p+1)-s2[i];
    }
    return ans;
}
int main()
{
    while(scanf("%d %d",&n,&m)==2){
        memset(s1,0,sizeof(s1));
        memset(s2,0,sizeof(s2));
        for(int i=1;i<=n;i++){
            long long x;
            scanf("%lld",&x);
            updata(i,x);
            updata(i+1,-x);
        }
        while(m--){
            char s[10];
            scanf("%s",s);
            if(s[0]=='C'){
                int a,b;
                long long c;
                scanf("%d %d %lld",&a,&b,&c);
                updata(a,c);
                updata(b+1,-c);
            }
            else{
                int a,b;
                scanf("%d %d",&a,&b);
                printf("%lld\n",sum(b)-sum(a-1));
            }
        }
    }
    return 0;
}

 

 
posted @ 2018-12-27 14:07  12-num  阅读(253)  评论(0编辑  收藏  举报