zoukankan      html  css  js  c++  java
  • 牛客

    https://ac.nowcoder.com/acm/contest/1221/E

    想了一下,然后用文本编辑器魔改就过了,注意开空间要预留新加入的点。

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define ls(p) ch[p][0]
    #define rs(p) ch[p][1]
    
    const int MAXN = 200000 + 5;
    int ch[MAXN][2], rnd[MAXN], siz[MAXN], tot, root;
    ll val[MAXN], sum[MAXN], lazy[MAXN];
    
    int cur;
    
    void Init() {
        root = 0, tot = 0;
    }
    
    inline void PushUp(int p) {
        siz[p] = siz[ls(p)] + siz[rs(p)] + 1;
        sum[p] = sum[ls(p)] + sum[rs(p)] + val[p];
    }
    
    inline void PushDown(int p) {
        if(lazy[p]) {
            sum[ls(p)] += 1ll * siz[ls(p)] * lazy[p];
            lazy[ls(p)] += lazy[p];
            val[ls(p)] += lazy[p];
            sum[rs(p)] += 1ll * siz[rs(p)] * lazy[p];
            lazy[rs(p)] += lazy[p];
            val[rs(p)] += lazy[p];
            lazy[p] = 0;
        }
    }
    
    void SplitRank(int p, int rk, int &x, int &y) {
        if(!p) {
            x = y = 0;
            return;
        }
        PushDown(p);
        if(rk <= siz[ls(p)]) {
            y = p;
            SplitRank(ls(p), rk, x, ls(p));
            PushUp(y);
        } else {
            x = p;
            SplitRank(rs(p), rk - siz[ls(p)] - 1, rs(p), y);
            PushUp(x);
        }
    }
    
    int Merge(int x, int y) {
        if(!x || !y)
            return x | y;
        if(rnd[x] < rnd[y]) {
            PushDown(x);
            rs(x) = Merge(rs(x), y);
            PushUp(x);
            return x;
        } else {
            PushDown(y);
            ls(y) = Merge(x, ls(y));
            PushUp(y);
            return y;
        }
    }
    
    int NewNode(ll v) {
        int p = ++tot;
        ch[p][0] = ch[p][1] = 0;
        val[p] = v, rnd[p] = rand();
        siz[p] = 1;
        sum[p] = v;
        lazy[p] = 0;
        return p;
    }
    
    
    //O(n)建树,返回新树的根
    int st[MAXN], stop;
    inline int Build(int n) {
        Init();
        stop = 0;
        for(int i = 0; i < n; ++i) {
            ll val;
            scanf("%lld", &val);
            int tmp = NewNode(val), last = 0;
            while(stop && rnd[st[stop]] > rnd[tmp]) {
                last = st[stop];
                PushUp(last);
                st[stop--] = 0;
            }
            if(stop)
                rs(st[stop]) = tmp;
            ls(tmp) = last;
            st[++stop] = tmp;
        }
        while(stop)
            PushUp(st[stop--]);
        return st[1];
    }
    
    
    int main() {
    #ifdef Yinku
        freopen("Yinku.in", "r", stdin);
    #endif // Yinku
        int n;
        scanf("%d", &n);
        root = Build(n);
        int m;
        scanf("%d", &m);
        for(int i = 1; i <= m; ++i) {
            int op;
            scanf("%d", &op);
            if(op == 1) {
                int pos;
                scanf("%d", &pos);
                int x, y;
                SplitRank(root, pos - 1, x, y);
                root = Merge(Merge(x, NewNode(0)), y);
            } else if(op == 2) {
                int l, r;
                ll v;
                scanf("%d%d%lld", &l, &r, &v);
                int x, y, z;
                SplitRank(root, l - 1, x, y);
                SplitRank(y, r + 1 - l, y, z);
                sum[y] += 1ll * siz[y] * v;
                lazy[y] += v;
                val[y] += v;
                root = Merge(Merge(x, y), z);
            } else {
                int l, r;
                scanf("%d%d", &l, &r);
                int x, y, z;
                SplitRank(root, l - 1, x, y);
                SplitRank(y, r + 1 - l, y, z);
                printf("%lld
    ", sum[y]);
                root = Merge(Merge(x, y), z);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    yum install nginx
    逻辑分区增加空间 vm中
    pbspro build rpm and installation
    centos 6 and 7 to modify hostname
    activeMQ
    cgo在mac上编译
    redis学习
    Spring Boot 2+gRPC 学习系列1:搭建Spring Boot 2+gRPC本地项目
    KumuluzEE
    前端实现“查看更多”效果
  • 原文地址:https://www.cnblogs.com/Inko/p/11741520.html
Copyright © 2011-2022 走看看