• Codeforces 295E Yaroslav and Points 线段树


    Yaroslav and Points

    明明区间合并一下就好的东西, 为什么我会写得这么麻烦的方法啊啊啊。

    #include<bits/stdc++.h>
    #define LL long long
    #define fi first
    #define se second
    #define mk make_pair
    #define PLL pair<LL, LL>
    #define PLI pair<LL, int>
    #define PII pair<int, int>
    #define SZ(x) ((int)x.size())
    #define ull unsigned long long
    
    using namespace std;
    
    const int N = 2e5 + 7;
    const int inf = 0x3f3f3f3f;
    const LL INF = 0x3f3f3f3f3f3f3f3f;
    const int mod = 1e9 + 7;
    const double eps = 1e-6;
    const double PI = acos(-1);
    
    vector<int> oo;
    int getPos(int x) {
        return lower_bound(oo.begin(), oo.end(), x) - oo.begin() + 1;
    }
    int getPosL(int x) {
        return lower_bound(oo.begin(), oo.end(), x) - oo.begin() + 1;
    }
    int getPosR(int x) {
        return upper_bound(oo.begin(), oo.end(), x) - oo.begin();
    }
    
    namespace SGT {
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
        LL sum[N << 2], cnt[N << 2];
        LL lazya[N << 2], lazyb[N << 2], all[N << 2];
        inline void push(int rt) {
            if(lazya[rt]) {
                lazya[rt << 1] += lazya[rt];
                lazya[rt << 1 | 1] += lazya[rt];
                all[rt << 1] += cnt[rt << 1] * lazya[rt];
                all[rt << 1 | 1] += cnt[rt << 1 | 1] * lazya[rt];
                lazya[rt] = 0;
            }
            if(lazyb[rt]) {
                lazyb[rt << 1] += lazyb[rt];
                lazyb[rt << 1 | 1] += lazyb[rt];
                all[rt << 1] += sum[rt << 1] * lazyb[rt];
                all[rt << 1 | 1] += sum[rt << 1 | 1] * lazyb[rt];
                lazyb[rt] = 0;
            }
        }
        inline void pull(int rt) {
            sum[rt] = sum[rt << 1] + sum[rt << 1 | 1];
            cnt[rt] = cnt[rt << 1] + cnt[rt << 1 | 1];
            all[rt] = all[rt << 1] + all[rt << 1 | 1];
        }
        void modify1(int p, LL val, int l, int r, int rt) {
            if(l == r) {
                cnt[rt] += val;
                sum[rt] += oo[l - 1] * val;
                if(!cnt[rt]) all[rt] = 0;
                return;
            }
            push(rt);
            int mid = l + r >> 1;
            if(p <= mid) modify1(p, val, lson);
            else modify1(p, val, rson);
            pull(rt);
        }
        void modify2(int L, int R, LL val, int l, int r, int rt) {
            if(L > R) return;
            if(l >= L && r <= R) {
                lazya[rt] += val;
                all[rt] += val * cnt[rt];
                return;
            }
            push(rt);
            int mid = l + r >> 1;
            if(L <= mid) modify2(L, R, val, lson);
            if(R > mid)  modify2(L, R, val, rson);
            pull(rt);
        }
        void modify3(int L, int R, LL val, int l, int r, int rt) {
            if(L > R) return;
            if(l >= L && r <= R) {
                lazyb[rt] += val;
                all[rt] += val * sum[rt];
                return;
            }
            push(rt);
            int mid = l + r >> 1;
            if(L <= mid) modify3(L, R, val, lson);
            if(R > mid)  modify3(L, R, val, rson);
            pull(rt);
        }
        PLL query1(int L, int R, int l, int r, int rt) {
            if(L > R) return mk(0, 0);
            if(l >= L && r <= R) return mk(sum[rt], cnt[rt]);
            push(rt);
            int mid = l + r >> 1;
            PLL ans = mk(0, 0), tmp = mk(0, 0);
            if(L <= mid) {
                tmp = query1(L, R, lson);
                ans.fi += tmp.fi; ans.se += tmp.se;
            }
            if(R > mid) {
                tmp = query1(L, R, rson);
                ans.fi += tmp.fi; ans.se += tmp.se;
            }
            return ans;
        }
        LL query2(int L, int R, int l, int r, int rt) {
            if(L > R) return 0;
            if(l >= L && r <= R) return all[rt];
            push(rt);
            int mid = l + r >> 1;
            LL ans = 0;
            if(L <= mid) ans += query2(L, R, lson);
            if(R > mid)  ans += query2(L, R, rson);
            return ans;
        }
    }
    
    int n, m, x[N], c[N];
    pair<PII, int> qus[N];
    
    int main() {
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) {
            scanf("%d", &x[i]);
            c[i] = x[i];
            oo.push_back(x[i]);
        }
        scanf("%d", &m);
        for(int i = 1; i <= m; i++) {
            scanf("%d%d%d", &qus[i].se, &qus[i].fi.fi, &qus[i].fi.se);
            if(qus[i].se == 1) {
                c[qus[i].fi.fi] += qus[i].fi.se;
                oo.push_back(c[qus[i].fi.fi]);
            }
        }
        sort(oo.begin(), oo.end());
        oo.erase(unique(oo.begin(), oo.end()), oo.end());
        for(int i = 1; i <= n; i++) SGT::modify1(getPos(x[i]), 1, 1, SZ(oo), 1);
        for(int i = 1; i <= n; i++) {
            PLL add = SGT::query1(getPos(x[i]), SZ(oo), 1, SZ(oo), 1);
            SGT::modify2(getPos(x[i]), getPos(x[i]), add.fi, 1, SZ(oo), 1);
            SGT::modify3(getPos(x[i]), getPos(x[i]), -add.se, 1, SZ(oo), 1);
        }
        for(int i = 1; i <= m; i++) {
            if(qus[i].se == 1) {
                int p = qus[i].fi.fi, d = qus[i].fi.se;
                if(d > 0) {
                    SGT::modify1(getPos(x[p]), -1, 1, SZ(oo), 1);
                    SGT::modify2(1, getPos(x[p]) - 1, d, 1, SZ(oo), 1);
                    SGT::modify2(getPos(x[p]) + 1, getPos(x[p] + d) - 1, x[p] + d, 1, SZ(oo), 1);
                    SGT::modify3(getPos(x[p]) + 1, getPos(x[p] + d) - 1, -1, 1, SZ(oo), 1);
                    x[p] += d;
                    SGT::modify1(getPos(x[p]), 1, 1, SZ(oo), 1);
                    PLL add = SGT::query1(getPos(x[p]), SZ(oo), 1, SZ(oo), 1);
                    SGT::modify2(getPos(x[p]), getPos(x[p]), add.fi, 1, SZ(oo), 1);
                    SGT::modify3(getPos(x[p]), getPos(x[p]), -add.se, 1, SZ(oo), 1);
                } else if(d < 0) {
                    d = -d;
                    SGT::modify1(getPos(x[p]), -1, 1, SZ(oo), 1);
                    SGT::modify2(1, getPos(x[p] - d) - 1, -d, 1, SZ(oo), 1);
                    SGT::modify2(getPos(x[p] - d) + 1, getPos(x[p]) - 1, -x[p], 1, SZ(oo), 1);
                    SGT::modify3(getPos(x[p] - d) + 1, getPos(x[p]) - 1, 1, 1, SZ(oo), 1);
                    x[p] -= d;
                    SGT::modify1(getPos(x[p]), 1, 1, SZ(oo), 1);
                    PLL add = SGT::query1(getPos(x[p]), SZ(oo), 1, SZ(oo), 1);
                    SGT::modify2(getPos(x[p]), getPos(x[p]), add.fi, 1, SZ(oo), 1);
                    SGT::modify3(getPos(x[p]), getPos(x[p]), -add.se, 1, SZ(oo), 1);
                }
            } else {
                int L = qus[i].fi.fi, R = qus[i].fi.se;
                L = getPosL(L); R = getPosR(R);
                LL ans = SGT::query2(L, R, 1, SZ(oo), 1);
                PLL tmpL = SGT::query1(L, R, 1, SZ(oo), 1);
                PLL tmpR = SGT::query1(R + 1, SZ(oo), 1, SZ(oo), 1);
                ans -= tmpL.se * tmpR.fi;
                ans += tmpL.fi * tmpR.se;
                printf("%lld
    ", ans);
            }
        }
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    利用HttpClient进行带参数的http文件上传
    使用mysqlproxy 快速实现mysql 集群 读写分离 [转]
    SQL Server Express 自动备份方法
    screen 配置文件
    linux启动DHCP
    dynamic table_name in cursor
    LogMiner and supplemental logging
    RAC 规划配置网络环境
    RAC prepare OS and installation media
    一次导数据流程
  • 原文地址:https://www.cnblogs.com/CJLHY/p/10652679.html
Copyright © 2020-2023  润新知