POJ 3468 A Simple Problem with Integers (分块解法)

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.


题意:输入 n, m表初始有 n 个数, 接下来 m 行输入,
Q x y 表示询问区间 [x, y]的和;
C x y z 表示区间 [x, y] 内所有数加上 z 。

解题思路:当时是一道线段树的模板题,现在我们用分块的思想来解决它;
分块思路:
我们将所给序列分成一个一个大小为根号n的块,每次询问时在线处理:
额外用到的数组:
pos[maxn]:pos[i]表示第i个元素属于哪个块。
lazy[maxn]:lazy[i]表示第i个块的增量。
sum[maxn]:sum[i]表示第i个块的总和。
ld[maxn]:ld[i]表示第i个块的左端点。
rd[maxn]:rd[i]表示第i个块的右端点。
一·询问操作
1.如果l和r都在一个块内,直接利用暴力算法从a[l]加到a[r]。
2.如果不一样,我们令p=pos[l],q=pos[r],l和r之间完整的块为从p+1到q-1,所以我们累加sum[i]+add*(R[i]-L[i]+1),i从p+1到q-1。
之后我们对于不完整的两端进行暴力处理。
二·改变操作
1.如果l和r都在一个块内,直接利用朴素算法从a[l]改变到a[r]。
2.如果不一样,我们令p=pos[l],q=pos[r],l和r之间完整的块为从p+1到q-1,所以我们改变lazy[i]+=val,i从p+1到q-1。
之后我们对于不完整的两端进行朴素处理。
总复杂度为O((N+M)*√N)。

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
const int maxn=1000005;
typedef long long ll;
ll a[maxn],sum[maxn],lazy[maxn];
int pos[maxn],ld[maxn],rd[maxn];
void change(int l,int r,int d)
{
    int p=pos[l],q=pos[r];
    if(p==q){
        for(int i=l;i<=r;i++){
            a[i]+=d;
        }
        sum[p]+=d*(r-l+1);
    }
    else{
        for(int i=p+1;i<=q-1;i++){
            lazy[i]+=d;
        }
        for(int i=l;i<=rd[p];i++){
            a[i]+=d;
        }
        sum[p]+=d*(rd[p]-l+1);
        for(int i=ld[q];i<=r;i++){
            a[i]+=d;
        }
        sum[q]+=d*(r-ld[q]+1);
    }
}
ll query(int l,int r)
{
    ll ans=0;
    int p=pos[l],q=pos[r];
    if(p==q){
        for(int i=l;i<=r;i++){
            ans+=a[i];
        }
        ans+=lazy[p]*(r-l+1);
    }
    else{
        for(int i=p+1;i<=q-1;i++){
            ans+=sum[i];
            ans+=lazy[i]*(rd[i]-ld[i]+1);
        }
        for(int i=l;i<=rd[p];i++){
            ans+=a[i];
        }
        ans+=lazy[p]*(rd[p]-l+1);
        for(int i=ld[q];i<=r;i++){
            ans+=a[i];
        }
        ans+=lazy[q]*(r-ld[q]+1);
    }
    return ans;
}
int main()
{
    int n,m,cnt,l,r,d;
    memset(a,0,sizeof(a));
    memset(sum,0,sizeof(sum));
    memset(lazy,0,sizeof(lazy));
    memset(pos,0,sizeof(pos));
    memset(ld,0,sizeof(ld));
    memset(rd,0,sizeof(rd));
    char cz[10];
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%lld",&a[i]);
    }
    cnt=sqrt(n*1.0);
    for(int i=1;i<=cnt;i++){
        ld[i]=(i-1)*cnt+1;
        rd[i]=i*cnt;
    }
    if(rd[cnt]<n){
        cnt++;
        ld[cnt]=rd[cnt-1]+1;
        rd[cnt]=n;
    }
    for(int i=1;i<=cnt;i++){
        for(int j=ld[i];j<=rd[i];j++){
            pos[j]=i;
            sum[i]+=a[j];
        }
    }
    while(m--){
        scanf("%s",cz);
        if(cz[0]=='Q'){
            ll ans=0;
            scanf("%d%d",&l,&r);
            ans=query(l,r);
            printf("%lld\n",ans);
        }
        else if(cz[0]=='C'){
            scanf("%d%d%d",&l,&r,&d);
            change(l,r,d);
        }
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值