A Simple Problem with Integers

A Simple Problem with Integers
Time Limit: 5000MS   Memory Limit: 131072K
     
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 <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#include <bitset>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define vi vector<int>
#define pii pair<int,int>
#define mod 1000000007
#define inf 0x3f3f3f3f
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
const int maxn=1e7+10;
const int dis[4][2]={{0,1},{-1,0},{0,-1},{1,0}};
using namespace std;
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;}
int n,m,k,t;
ll a[maxn],sum[maxn],sum1[maxn];
ll build(int now,int l,int r)
{
    if(l==r)return sum[now]=a[l];
    else return sum[now]=build(now*2+1,l,l+r>>1)+build(now*2+2,(l+r>>1)+1,r);
}
ll getsum(int ql,int qr,int x,int l,int r)
{
    if(ql>r||qr<l)return 0;
    else if(ql<=l&&qr>=r)return sum[x]+sum1[x]*(r-l+1);
    else
    {
        ll res=(min(qr,r)-max(ql,l)+1)*sum1[x];
        res+=getsum(ql,qr,x*2+1,l,l+r>>1);
        res+=getsum(ql,qr,x*2+2,(l+r>>1)+1,r);
        return res;
    }
}
void update(int ql,int qr,int y,int x,int l,int r)
{
    if(ql<=l&&qr>=r)
    {
        sum1[x]+=y;
    }
    else if(ql<=r&&qr>=l)
    {
        sum[x]+=(min(qr,r)-max(ql,l)+1)*y;
        update(ql,qr,y,x*2+1,l,l+r>>1);
        update(ql,qr,y,x*2+2,(l+r>>1)+1,r);
    }
}
int main()
{
    int i,j;
    scanf("%d%d",&n,&m);
    rep(i,1,n)scanf("%lld",&a[i]);
    build(0,1,n);
    while(m--)
    {
        int x[4];
        char p[10];
        scanf("%s",p);
        if(p[0]=='Q')
        {
            rep(i,1,2)scanf("%d",&x[i]);
            printf("%lld
",getsum(x[1],x[2],0,1,n));
        }
        else
        {
            rep(i,1,3)scanf("%d",&x[i]);
            update(x[1],x[2],x[3],0,1,n);
        }
    }
    //system ("pause");
    return 0;
}
原文地址:https://www.cnblogs.com/dyzll/p/5746765.html