POJ - 3468 A Simple Problem with Integers 树状数组区间查询区间修改

博客给出了POJ-3468题目的链接、题目描述、输入输出要求。题目涉及对给定区间内数字进行操作,包括加特定值和查询区间和。题解指出可用线段树或树状数组解决,同时提醒注意数据范围可能爆int,还提及有对应代码。

题目链接

https://vjudge.net/problem/POJ-3468

题目

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 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.

题解

可以用线段树也可用树状数组。板子题。

注意数据范围,可能爆int。

代码
 

#include<cstdio>
#include<iostream>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
ll bit[maxn],bit2[maxn];
int num[maxn];
int n;
void add(ll* a,int x,ll y){
    while(x<=n){
        a[x]+=y;
        x+=x&-x;
    }
}
ll sum(ll *a,int x){
    ll ans=0;
    while(x){
        ans+=a[x];
        x-=x&-x;
    }
    return ans;
}
ll query(int x){
    return 1LL*x*sum(bit,x)-sum(bit2,x);
}
ll query2(int l,int r){
    return  query(r)-query(l-1);
}
int main(){
    int m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        scanf("%d",num+i);
        add(bit,i,num[i]-num[i-1]);
        add(bit2,i,1LL*(i-1)*(num[i]-num[i-1]));
    }
    while(m--){
        char c[2];
        scanf("%s",c);
        if(c[0]=='Q'){
            int x,y;
            scanf("%d%d",&x,&y);
            printf("%lld\n",query2(x,y));
        }else{
            int x,y,d;
            scanf("%d%d%d",&x,&y,&d);
            add(bit,x,d);
            add(bit,y+1,-d);
            add(bit2,x,(x-1)*d);
            add(bit2,y+1,-y*d);
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值