POJ 3468 - A Simple Problem with Integers(线段树,区间更新)

C - A Simple Problem with Integers
Time Limit:5000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u
Appoint description: 

Description

给出了一个序列,你需要处理如下两种询问。

"C a b c"表示给[a, b]区间中的值全部增加c (-10000 ≤ c ≤ 10000)。

"Q a b" 询问[a, b]区间中所有值的和。

Input

第一行包含两个整数N, Q。1 ≤ N,Q ≤ 100000.

第二行包含n个整数,表示初始的序列A (-1000000000 ≤ Ai ≤ 1000000000)。

接下来Q行询问,格式如题目描述。

Output

对于每一个Q开头的询问,你需要输出相应的答案,每个答案一行。

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

题意:中文题

思路:这种 n*n 的查询更新,肯定用线段树。但是不能单点更新,如果单点更新的话,相当于将区间内所有点都递归到树的最后一层再更新。因此要利用区间更新,大致思路是:1,对每个结点,除了设置 sum 变量之外,再存一个 add 变量用来整个区间被更新的次数。递归过程中如果碰到 ll == l && rr == r 的情况,不必向下更新,直接更新这个区间的 add。 2,每次递归 update 时要更新当前 sum。(更新的必要性也有写到) 3,查询时又上到下传递 add 参数,最后要 return sum+(区间长度)*addd; 

具体有些细节注释中写到了。

代码:

#include <iostream>
#include <string>
using namespace std;

struct Node{
    int l,r;
    long long add;
    long long sum;
}node[100000*4+5];
int a[100005];

void build(int id,int l,int r){
    node[id].l = l;
    node[id].r = r;
    node[id].add = 0;

    if(l == r){
        node[id].sum = a[l];
        return ;
    }
    int mid = (l+r)>>1;
    build(id*2,l,mid);
    build(id*2+1,mid+1,r);

    node[id].sum = node[id*2].sum+node[id*2+1].sum;
}
long long query(int id,int l,int r,long long c){        //c为到该层为止,包含该层子区间的结点共更新了多少次。每次递归需累加
    int ll = node[id].l;
    int rr = node[id].r;
    int mid = (ll+rr)>>1;
    int addd = node[id].add;

    //if(ll == rr)  return node[id].sum + c+addd;
    if(ll == l && rr == r)
        return node[id].sum +  (r-l+1)*(c);
    if(r <= mid)
        return query(id*2,l,r,c+addd);
    else if(l > mid)
        return query(id*2+1,l,r,c+addd);
    else
        return query(id*2,l,mid,c+addd) + query(id*2+1,mid+1,r,c+addd);
}
void update(int id,int l,int r,int c){
    int ll = node[id].l;
    int rr = node[id].r;
    int mid = (ll+rr)>>1;

    node[id].sum += (r-l+1)*c;      //下层区间更新了,但是该层的add未更新,所以要修改该层的sum
    if(ll == l && rr == r){     //用更新区间代替点
        node[id].add += c;
        return ;
    }
    if(r <= mid)
        update(id*2,l,r,c);
    else if(l > mid)
        update(id*2+1,l,r,c);
    else{
        update(id*2,l,mid,c);
        update(id*2+1,mid+1,r,c);
    }
}

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    int n,m;
    while(cin>>n>>m){
        for(int i = 1;i <= n;i ++)  cin>>a[i];

        build(1,1,n);
        string s;
        int ai,bi,ci;
        for(int i = 1;i <= m;i ++){
            cin>>s;
            if(s[0] == 'Q'){
                cin>>ai>>bi;
                long long ans = query(1,ai,bi,0);
                cout<<ans<<endl;
            }
            if(s[0] == 'C'){
                cin>>ai>>bi>>ci;
                update(1,ai,bi,ci);
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值