码迷,mamicode.com
首页 > 其他好文 > 详细

poj 3468 A Simple Problem with Integers

时间:2018-02-23 19:09:40      阅读:158      评论:0      收藏:0      [点我收藏+]

标签:oid   span   integer   return   poj   c++   --   树状   blog   

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

#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

标签:oid   span   integer   return   poj   c++   --   树状   blog   

原文地址:https://www.cnblogs.com/poorpool/p/8462681.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!