记录编号 381115 评测结果 AAAAAAAAAA
题目名称 数列操作C 最终得分 100
用户昵称 GravatarRP++ 是否通过 通过
代码语言 C++ 运行时间 0.272 s
提交时间 2017-03-10 21:10:08 内存使用 6.42 MiB
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

#define maxn 100010
#define LL long long
#define lson rt<<1, l, mid
#define rson rt<<1|1, mid + 1, r

LL tr[maxn<<2], tag[maxn<<2];

void Push_Down(int rt, int l, int r) {
    //tr[rt] += tag[rt] * (r - l + 1);
    if(l < r) {
        int len = r - l + 1;
        tag[rt<<1] += tag[rt], tr[rt<<1] += tag[rt] * ((len + 1) / 2);
        tag[rt<<1|1] += tag[rt], tr[rt<<1|1] += tag[rt] * (len / 2);
    }
    tag[rt] = 0;
}

void Push_Up(int rt) {
    tr[rt] = tr[rt<<1] + tr[rt<<1|1];
}

void Build_Tree(int rt, int l, int r) {
    if(l == r) {
        scanf("%lld", &tr[rt]); return ;
    }
    int mid = (l + r) >> 1;
    Build_Tree(lson), Build_Tree(rson);
    Push_Up(rt);
}

LL Query(int rt, int l, int r, int s, int t) {
    //printf("%d\n", tr[5]);
    //printf("%d %d %d\n", rt, l, r);
    if(tag[rt]) Push_Down(rt, l, r);
    if(l >= s && r <= t) return tr[rt];
    int mid = (l + r) >> 1; LL ans = 0;
    if(s <= mid) ans = Query(lson, s, t);
    if(t > mid) ans += Query(rson, s, t);
    Push_Up(rt);
    return ans;
}

void modify(int rt, int l, int r, int s, int t, int x) {
    //printf("%d %d\n", l, r);
    if(tag[rt]) Push_Down(rt, l, r);
    if(l >= s && r <= t) {
        tr[rt] += x * (r - l + 1), tag[rt] += x;
        return ;
    }
    int mid = (l + r) >> 1;
    if(s <= mid) modify(lson, s, t, x);
    if(t > mid) modify(rson, s, t, x);
    Push_Up(rt);
}

int main() {
    freopen("shuliec.in", "r", stdin);
    freopen("shuliec.out", "w", stdout);
    int n;
    scanf("%d", &n);
        memset(tr, 0, sizeof(tr)), memset(tag, 0, sizeof(tag));
        Build_Tree(1, 1, n);
        char ch[10];
        int q, a, b, c;
        scanf("%d", &q);
        for(int i = 1; i <= q; i++) {
            scanf("%s", ch);
            if(ch[0] == 'S') {
                scanf("%d%d", &a, &b);
                printf("%lld\n", Query(1, 1, n, a, b));
            } else {
                scanf("%d%d%d", &a, &b, &c);
                modify(1, 1, n, a, b, c);
            }
        }
}