数据结构训练之三

https://www.luogu.org/problem/P2221

分析,数据结构跑不掉,看标签,什么狗屁期望,一点关系都没有好吧

其实就是一道线段树裸题,唯独就是麻烦了一点而已,因为维护的东西很多

code

#include <iostream>
#include <cstdio>
#define lson now << 1
#define rson now << 1 | 1
#define ll long long
const ll maxN = 100000 + 7;

inline ll read() {
    ll x = 0,f = 1;char c = getchar();
    while(c < '0' || c > '9') {if(c == '-')f = -1;c = getchar();}
    while(c >= '0' && c <= '9') {x = x * 10 + c - '0';c = getchar();}
    return x * f;
}

ll gcd(ll a,ll b) {
    return !b ? a : gcd(b,a % b);
}

struct Node {
    ll sum[6];
    ll lazy;
    ll l,r;
}tree[maxN << 2];
ll sum1,sum2,sum3;

void updata(ll now) {
    tree[now].sum[1] = tree[lson].sum[1] + tree[rson].sum[1];
    tree[now].sum[2] = tree[lson].sum[2] + tree[rson].sum[2];
    tree[now].sum[3] = tree[lson].sum[3] + tree[rson].sum[3];
    return ;
}

void build(ll l,ll r,ll now) {
    tree[now].l = l;tree[now].r = r;
    if(l == r) {
        tree[now].sum[4] = l * l;
        tree[now].sum[5] = l;
        return ;
    }
    ll mid = (l + r) >> 1;
    build(l,mid,lson);
    build(mid + 1,r,rson);
    tree[now].sum[4] = tree[lson].sum[4] + tree[rson].sum[4];
    tree[now].sum[5] = tree[lson].sum[5] + tree[rson].sum[5];
    return ;
}

void work(ll now,ll k) {
    tree[now].sum[1] += (tree[now].r - tree[now].l + 1) * k;
    tree[now].sum[2] += k * tree[now].sum[5];
    tree[now].sum[3] += k * tree[now].sum[4];
    tree[now].lazy += k;
}

void pushdown(ll now) {
    work(lson,tree[now].lazy);
    work(rson,tree[now].lazy);
    tree[now].lazy = 0;
    return ;
}

void modify(ll l,ll r,ll now,ll val) {
    if(tree[now].l >= l && tree[now].r <= r) {
        work(now,val);
        return ;
    }
    if(tree[now].lazy) pushdown(now);
    ll mid = (tree[now].l + tree[now].r) >> 1;
    if(mid >= l) modify(l,r,lson,val);
    if(mid < r) modify(l,r,rson,val);
    updata(now);
    return ;
}

void query(ll l,ll r,ll now) {
    if(tree[now].l >= l && tree[now].r <= r)  {
        sum1 += tree[now].sum[1];
        sum2 += tree[now].sum[2];
        sum3 += tree[now].sum[3];
        return ;
    }
    if(tree[now].lazy) pushdown(now);
    ll mid = (tree[now].l + tree[now].r) >> 1;
    if(mid >= l) query(l,r,lson);
    if(mid < r) query(l,r,rson);
    return ;
}

int main()
{   
    ll n,m,l,r,v;
    char s[3];
    n = read();m = read();
    build(1,n,1);
    while(m --) {
        scanf("%s",&s);
        l = read();r = read() - 1;
        if(s[0] == 'C') {
            v = read();
            modify(l,r,1,v);
        }
        else {
            ll a;
            sum1 = sum2 = sum3 = 0;
            query(l,r,1);
            a = (r - l + 1 - r * l) * sum1 + (r + l) * sum2 - sum3;
            ll b = ( r - l + 2 ) * (r - l + 1) / 2;
            ll g = gcd(a,b);
            printf("%lld/%lld\n", a / g,b / g);
        }
    }
    return 0;
}
posted @ 2019-10-18 09:19  wzx_believer  阅读(139)  评论(0编辑  收藏  举报