poj 3468 A Simple Problem with Integers

Posted poorpool

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了poj 3468 A Simple Problem with Integers相关的知识,希望对你有一定的参考价值。

可以用树状数组,线段树,分块做。代码是分块

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
int n, m, uu, vv, ww, blc, bel[100005], l[100005], r[100005];
ll sum[100005], tag[100005], a[100005];
char ss[15];
ll query(int uu, int vv){
    int p=bel[uu], q=bel[vv];
    ll re=0;
    if(p==q){
        for(int i=uu; i<=vv; i++)   re += a[i];
        re += tag[p] * (vv - uu + 1);
    }
    else{
        for(int i=p+1; i<=q-1; i++) re += sum[i];
        for(int i=uu; i<=r[p]; i++) re += a[i] + tag[p];
        for(int i=l[q]; i<=vv; i++) re += a[i] + tag[q];
    }
    return re;
}
void update(int uu, int vv, int ww){
    int p=bel[uu], q=bel[vv];
    if(p==q){
        for(int i=uu; i<=vv; i++)   a[i] += ww;
        sum[p] += ww * (vv - uu + 1);
    }
    else{
        for(int i=p+1; i<=q-1; i++){
            sum[i] += ww * (r[i] - l[i] + 1);
            tag[i] += ww;
        }
        for(int i=uu; i<=r[p]; i++) a[i] += ww;
        sum[p] += ww * (r[p] - uu + 1);
        for(int i=l[q]; i<=vv; i++) a[i] += ww;
        sum[q] += ww * (vv - l[q] + 1);
    }
}
int main(){
    cin>>n>>m;
    for(int i=1; i<=n; i++) scanf("%lld", &a[i]);
    blc = sqrt(n);
    for(int i=1; i<=n; i++){
        bel[i] = (i - 1) / blc + 1;
        sum[bel[i]] += a[i];
    }
    for(int i=1; i<=blc; i++){
        l[i] = (i - 1) * blc + 1;
        r[i] = i * blc;
    }
    if(blc*blc<n){
        blc++;
        l[blc] = r[blc-1] + 1;
        r[blc] = n;
    }
    while(m--){
        scanf("%s", ss);
        if(ss[0]==‘Q‘){
            scanf("%d %d", &uu, &vv);
            printf("%lld\n", query(uu, vv));
        }
        else{
            scanf("%d %d %d", &uu, &vv, &ww);
            update(uu, vv, ww);
        }
    }
    return 0;
}

以上是关于poj 3468 A Simple Problem with Integers的主要内容,如果未能解决你的问题,请参考以下文章

A Simple Problem with Integers POJ - 3468

POJ - 3468 A Simple Problem with Integers

[poj3468]A Simple Problem with Integers

POJ3468 a simple problem with integers 分块

POJ 3468 A Simple Problem with Integers 树状数组

POJ 3468 A Simple Problem with Integers