[poj3468]A Simple Problem with Integers

Posted LJZ_C

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[poj3468]A Simple Problem with Integers相关的知识,希望对你有一定的参考价值。

Description

You have N integers, A1, A2, ... , 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 A1, A2, ... , AN. -1000000000 ≤ Ai ≤ 1000000000.
Each of the next Q lines represents an operation.
"C a b c" means adding c to each of Aa, Aa+1, ... , Ab. -10000 ≤ c ≤ 10000.
"Q a b" means querying the sum of Aa, Aa+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<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
    while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
    return x*f;
}
int n,q;
struct data{LL s,tag;}t[400050];
inline void pushup(int k)
{
    t[k].s=t[k<<1].s+t[k<<1|1].s;
}
inline void pushdown(int k,int l,int r)
{
    LL x=t[k].tag;t[k].tag=0;
    if(x==0)return;
    int mid=(l+r)>>1;
    t[k<<1].s+=x*(mid-l+1),t[k<<1].tag+=x;
    t[k<<1|1].s+=x*(r-mid),t[k<<1|1].tag+=x;
}
void build(int k,int l,int r)
{
    t[k].tag=0;
    if(l==r){t[k].s=read();return;}
    int mid=(l+r)>>1;
    build(k<<1,l,mid),build(k<<1|1,mid+1,r);
    pushup(k);
}
void updata(int k,int l,int r,int a,int b,int key)
{
    if(l==a&&r==b)
    {
        t[k].s+=(LL)(r-l+1)*key;
        t[k].tag+=key;
        return;
    }
    int mid=(l+r)>>1;pushdown(k,l,r);
    if(b<=mid)updata(k<<1,l,mid,a,b,key);
    else if(a>mid)updata(k<<1|1,mid+1,r,a,b,key);
    else updata(k<<1,l,mid,a,mid,key),updata(k<<1|1,mid+1,r,mid+1,b,key);
    pushup(k);
}
LL getsum(int k,int l,int r,int a,int b)
{
    if(l==a&&r==b)return t[k].s;
    int mid=(l+r)>>1;pushdown(k,l,r);
    if(b<=mid)return getsum(k<<1,l,mid,a,b);
    else if(a>mid)return getsum(k<<1|1,mid+1,r,a,b);
    else return getsum(k<<1,l,mid,a,mid)+getsum(k<<1|1,mid+1,r,mid+1,b);
}
int main()
{
    n=read(),q=read();
    build(1,1,n);
    while(q--)
    {
        char op[5];scanf("%s",op);
        if(op[0]==‘Q‘)
        {
            int x=read(),y=read();
            printf("%lld\n",getsum(1,1,n,x,y));
        }
        if(op[0]==‘C‘)
        {
            int x=read(),y=read(),z=read();
            updata(1,1,n,x,y,z);
        }
    }
    return 0;
}

以上是关于[poj3468]A Simple Problem with Integers的主要内容,如果未能解决你的问题,请参考以下文章

A Simple Problem with Integers POJ - 3468

POJ - 3468 A Simple Problem with Integers

[poj3468]A Simple Problem with Integers

POJ3468 a simple problem with integers 分块

POJ 3468 A Simple Problem with Integers 树状数组

POJ 3468 A Simple Problem with Integers