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

POJ3468 A Simple Problem with Integers

时间:2014-05-10 09:41:57      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:blog   class   code   c   int   get   

PS: 线段树区间操作,省赛热身。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 100100;
typedef long long LL;
int N, Q;
LL sumv[maxn*3], addv[maxn*3];
int ql, qr;
LL _sum;
//Accepted	4836K	1750MS	C++	1949B	2014-05-04 16:23:50
void maintain(int o, int L, int R) {
    int lc = o*2, rc = o*2+1;
    if(L < R) {
        sumv[o] = sumv[lc] + sumv[rc];
    }
    sumv[o] += (R-L+1)*addv[o];  //WA.
    if(L==R) {
        addv[o] = 0;
    }
}

void update(int o, int L, int R, int v) {
    if(ql<=L && qr >= R) {
        addv[o] += v;
    } else {
        int M = L + (R-L)/2;
        if(ql <= M) update(o*2, L, M, v);
        if(qr > M) update(o*2+1, M+1, R, v);
    }
    maintain(o, L, R);
}

void query(int o, int L, int R, LL add) {
    if(ql<=L && qr >= R) {
        _sum += sumv[o] + add*(R-L+1);
    } else {
        int M = L + (R-L)/2;
        if(ql <= M) query(o*2, L, M, add+addv[o]);
        if(qr > M) query(o*2+1, M+1, R, add+addv[o]);
    }
}

void init() {
    memset(sumv, 0, sizeof(sumv));
    memset(addv, 0, sizeof(addv));
}

int main()
{
   // freopen("in", "r", stdin);
    int tmp, from, to;
    char str[5];
    while(scanf("%d%d", &N, &Q)!=EOF) {
        init();
        for(int i = 1; i <= N; i++) {
            scanf("%d", &tmp);
            ql = qr = i;
            update(1, 1, N, tmp);
        }
        getchar();
        for(int i = 1; i <= Q; i++) {
            scanf("%s", str);
            int v = 0;
            if(str[0]==‘Q‘) {
                scanf("%d%d", &from, &to);
                ql = from, qr = to;
                _sum = 0, v = 0;
                query(1, 1, N, v);
                printf("%lld\n", _sum);
            }
            else if(str[0]==‘C‘) {
                int add;
                scanf("%d%d%d", &from, &to, &add);
                ql = from, qr = to;  // WA.
                update(1, 1, N, add);
            }
        }
    }
    return 0;
}

POJ3468 A Simple Problem with Integers,布布扣,bubuko.com

POJ3468 A Simple Problem with Integers

标签:blog   class   code   c   int   get   

原文地址:http://blog.csdn.net/achiberx/article/details/24987673

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