记录编号 309523 评测结果 AAAAAAAAAA
题目名称 数列操作C 最终得分 100
用户昵称 Gravatarsxysxy 是否通过 通过
代码语言 C++ 运行时间 0.258 s
提交时间 2016-09-19 21:31:23 内存使用 7.16 MiB
显示代码纯文本
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdarg>
#include <vector>
#include <list>
#include <functional>
#include <map>
using namespace std;
typedef long long LL;
LL fast_read()
{
    LL r;
    char c;
    bool sig = false;
    while(c = getchar())    
    {
        if(c >= '0' && c <= '9')
        {
            r = c^0x30;
            break;
        }else if(c == '-')
            sig = true;
    }
    while(isdigit(c = getchar()))
        r = (r<<3)+(r<<1)+(c^0x30);
    if(sig)return -r;
    return r;
}
#define MAXN 100002
struct node
{
    int l, r, ls ,rs;
    LL sum;
    LL lazyv;
    void calc(LL d)
    {
        sum += d * (r-l+1);
    }
}ns[MAXN<<1];
LL data[MAXN];
int root = 1;
int last = root;
void pushup(node &d)
{
    d.sum = ns[d.ls].sum + ns[d.rs].sum;
}
int build(int l, int r)
{
    if(l > r)return 0;
    int cur = last++;
    node &d = ns[cur];
    d.l = l;
    d.r = r;
    d.lazyv = 0;
    d.sum = 0;
    if(l == r)
        d.sum = data[l];
    else
    {
        int m = (l+r)>>1;
        if(l <= m)
        {
            d.ls = build(l, m);;
            d.rs = build(m+1, r);
            pushup(d);
        }
    }
    return cur;
}
void pushdown(node &d)
{
 
    ns[d.ls].lazyv += d.lazyv;
    ns[d.ls].calc(d.lazyv);
    ns[d.rs].lazyv += d.lazyv;
    ns[d.rs].calc(d.lazyv);
       
    d.lazyv = 0;
}
void update(int c, int l, int r, LL v)
{
    if(!c)return;
    node &d = ns[c];
    if(d.l == l && d.r == r)
    {
        d.lazyv += v;
        d.calc(v);
    }else
    {
        pushdown(d);
        if(l >= ns[d.rs].l)
            update(d.rs, l, r, v);
        else if(r <= ns[d.ls].r)
            update(d.ls, l, r, v);
        else
        {
            update(d.ls, l, ns[d.ls].r, v);
            update(d.rs, ns[d.rs].l, r, v);
        }
        pushup(d);
    }
}
LL query(int c, int l, int r)
{
    if(!c)return 0;
    node &d = ns[c];
    if(d.l == l && d.r == r)
    {
        return d.sum;
    }else
    {
        pushdown(d);
        if(l >= ns[d.rs].l)
            return query(d.rs, l, r);
        else if(r <= ns[d.ls].r)
            return query(d.ls, l, r);
        else
            return query(d.ls, l, ns[d.ls].r) + query(d.rs, ns[d.rs].l, r);
    }
}
void clear(int n)
{
    while(n--)getchar();
}
int main()
{
    freopen("shuliec.in", "r", stdin);
    freopen("shuliec.out", "w", stdout);
    int n;
    n = fast_read();
    for(int i = 1; i <= n; i++)
        data[i] = fast_read();
    build(1, n);
    int k = fast_read();
    while(k--)
    {
        char c;
        while(c = getchar())
        {
            if(c == 'S' || c == 'A')
                break;
        }
        clear(2);
        int a1, a2;
        LL a3;
        switch(c)
        {
            case 'S':
                a1 = (int)fast_read();
                a2 = (int)fast_read();
                printf("%lld\n", query(1, a1, a2));
            break;
            case 'A':
                a1 = (int)fast_read();
                a2 = (int)fast_read();
                a3 = fast_read();
                update(1, a1, a2, a3);
            break;
        }
    }
    return 0;
}