POJ3468 A Simple Problem with Integers 【线段树/BIT】

9 篇文章 0 订阅
7 篇文章 0 订阅
A Simple Problem with Integers
Time Limit: 5000MS      Memory Limit: 131072K
Total Submissions: 110542       Accepted: 34432
Case Time Limit: 2000MS
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.
Source

POJ Monthly--2007.11.25, Yang Yi

把没看完的挑战看完


线段树:
考虑维护2个线段树
k为[l,r]的下标
rmqA[k]=x –> [l,r]区间的所有元素加上x
rmqB[k]=x –> [l,r]区间的所有元素 - rmqA[k] 的和 = ri=l(a[i]rmqA[k])=ri=la[i](rl+1)rmqA[k]

所以:sum(l,r) = rmqA[k]+rmqB[k]

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#define ll long long
#define pii pair<int,int>
#define MEM(a,x) memset(a,x,sizeof(a))

using namespace std;

const int inf=1e9+7;

const int N = 1e5+5;
ll rmqA[4*N],rmqB[4*N];

void add(int k,int l,int r,int a,int b,int x){
    if(b<l||r<a){
        return;
    }
    if(a<=l&&r<=b){
        rmqA[k]+=x;
        return;
    }
    if(l<r){
        int mid=(l+r)/2;
        add(2*k+1,l,mid,a,b,x);
        add(2*k+2,mid+1,r,a,b,x);
        rmqB[k]+=( min(r,b)-max(l,a) +1 ) * x;
    }
}

ll query(int k,int l,int r,int a,int b){
    if(b<l||r<a){
        return 0;
    }
    if(a<=l&&r<=b){
        return rmqA[k]*(r-l+1)+rmqB[k];
    }
    ll ans=0;
    if(l<r){
        int mid=(l+r)/2;
        ans+=query(2*k+1,l,mid,a,b);
        ans+=query(2*k+2,mid+1,r,a,b);
    }
    return ans+( min(b,r)-max(a,l) +1 ) * rmqA[k];
}

int main()
{
    //freopen("/home/lu/code/r.txt","r",stdin);
    int n,q;
    while(~scanf("%d%d",&n,&q)){
        MEM(rmqA,0);
        MEM(rmqB,0);
        for(int i=0;i<n;++i){
            int x;
            scanf("%d",&x);
            add(0,0,n-1,i,i,x);
        }
        while(q--){
            char ch;
            int a,b;
            scanf("%*c%c%d%d",&ch,&a,&b);
            if(ch=='Q'){
                printf("%lld\n",query(0,0,n-1,a-1,b-1));
            }
            else{
                int x;
                scanf("%d",&x);
                add(0,0,n-1,a-1,b-1,x);
            }
        }
    }
    return 0;
}

BIT:
s[i]=ij=1a[j]
对区间[L,R]元素+x后:

s2[i]=s[i]  //i<L
s2[i]=s[i]+(i-L+1)*x = s[i]+x*i+(1-L)*x //L<=i && i<=R
s2[i]=s[i]+(R-L+1)*x = s[i]+ R*x+(1-L)*x //i>R

设s[i]=bit0*i+bit1
对[L,R]+x后
bit0[L]+=x
bit0[R+1]-=x
bit1[L]+=(1-L)*x
bit1[R+1]+=R*x

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<algorithm>
#define ll long long
#define pii pair<int,int>
#define MEM(a,x) memset(a,x,sizeof(a))
#define lowbit(x) ((x)&-(x))

using namespace std;

const int inf=1e9+7;

const int N = 1e5+5;

ll bit0[N],bit1[N];

void add(ll*bit,int idx,int x,int n){
    while(idx<=n){
        bit[idx]+=x;
        idx+=lowbit(idx);
    }
}

ll sum(ll*bit,int idx){
    ll ans=0;
    while(idx>0){
        ans+=bit[idx];
        idx-=lowbit(idx);
    }
    return ans;
}

int main()
{
    //freopen("/home/lu/code/r.txt","r",stdin);
    int n,q;
    while(~scanf("%d%d",&n,&q)){
        MEM(bit0,0);
        MEM(bit1,0);
        for(int i=0;i<n;++i){
            int x;
            scanf("%d",&x);
            add(bit1,i+1,x,n);
        }
        while(q--){
            char ch;
            int a,b;
            scanf("%*c%c%d%d",&ch,&a,&b);
            if(ch=='Q'){
                ll x=sum(bit0,b)*b-sum(bit0,a-1)*(a-1);
                ll y=sum(bit1,b)-sum(bit1,a-1);
                printf("%lld\n",x+y);
            }
            else{
                int x;
                scanf("%d",&x);
                add(bit0,a,x,n);
                add(bit0,b+1,-x,n);
                add(bit1,a,(-a+1)*x,n);
                add(bit1,b+1,b*x,n);
            }
        }
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值