poj 3468 线段树区间更新lazy

题目在这


poj的判题有些老了, 用string RE一发,有点迷。

A Simple Problem with Integers
Time Limit: 5000MS Memory Limit: 131072K
Total Submissions: 107086 Accepted: 33425
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.

Source



#include <vector>
#include <iostream>
#include <string>
#include <map>
#include <stack>
#include <cstring>
#include <queue>
#include <list>
#include <cstdio>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <cctype>
#include <sstream>
#include <functional>
using namespace std;


#define LL long long
#define INF 1E4 * 1E9
#define pi acos(-1)
#define endl '\n'
#define me(x) memset(x,0,sizeof(x));
#define close() ios::sync_with_stdio(0);
const int maxn=1e3+5;
const int maxx=1e5+5;

inline int Scan()
{
    int res=0,ch,flag=0;
    if((ch=getchar())=='-')flag=1;
    else if(ch>='0' && ch<='9')res=ch-'0';
    while((ch=getchar())>='0'&&ch<='9')res=res*10+ch-'0';
    return flag ? -res : res;
}

int a[maxx],n,q;
struct node
{
    int l,r;
    long long sum,lazy;
    void update(long long x)
    {
        sum+=1*(r-l+1)*x;//有多少个  1-3  就是3个  3*2
        lazy+=x;
    }
}tree[maxx*4];

void push_up(int x)//通过当前节点x把值递归向上更新到根结点
{
    tree[x].sum=tree[x<<1].sum+tree[x<<1|1].sum;
}

void push_down(int x)//通过当前结点x递归向下去更新x子节点的值
{
    int lazyval=tree[x].lazy;
    if(lazyval)
    {
        tree[x<<1].update(lazyval);//左结点右结点都加延迟修改量
        tree[x<<1|1].update(lazyval);
        tree[x].lazy=0;
    }
}
void build(int x,int l,int r)
{
    tree[x].l=l;tree[x].r=r;
    tree[x].sum=tree[x].lazy=0;
    if(l==r) tree[x].sum=a[l];
    else
    {
        int mid=(l+r)/2;
        build(x<<1,l,mid);
        build(x<<1|1,mid+1,r);
        push_up(x);
    }
}
void update(int x,int l,int r,long long val)
{
    int L=tree[x].l,R=tree[x].r;
    if(l<=L&&R<=r)  tree[x].update(val);
    else
    {
        push_down(x);
        int mid=(L+R)/2;
        if(mid>=l) update(x<<1,l,r,val);
        if(r>mid) update(x<<1|1,l,r,val);
        push_up(x);
    }
}
long long query(int x,int l,int r)
{
    int L=tree[x].l,R=tree[x].r;
    if(l<=L&&R<=r) return tree[x].sum;
    else
    {
        push_down(x);
        long long ans=0;
        int mid=(L+R)/2;
        if(mid>=l) ans+=query(x<<1,l,r);
        if(r>mid) ans+=query(x<<1|1,l,r);
        push_up(x);
        return ans;
    }
}
int main()
{
    close();
    n=Scan();
    q=Scan();
    for(int i=1;i<=n;i++)
        a[i]=Scan();
    build(1,1,n);
    for(int i=1;i<=q;i++)
    {
        char op[2];
        int l,r,val;
        scanf("%s",op);
        if(op[0]=='Q')
        {
            scanf("%d%d",&l,&r);
            printf("%lld\n",query(1,l,r));
        }
        else
        {
            scanf("%d%d%d",&l,&r,&val);
            update(1,l,r,val);
        }
    }

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值