• Codeforces 1146E Hot is Cold 线段树


    Hot is Cold

    好像我写麻烦了, 其实只用线段树维护区间每个数反转, 区间赋值就可以。

    #include<bits/stdc++.h>
    using namespace std;
    
    const int N = (int)1e5 + 7;
    
    int n, q, pos, a[N];
    char op[N];
    
    struct Bit {
        int a[N];
        void modify(int x, int v) {
            for(int i = x; i < N; i += i & -i) {
                a[i] += v;
            }
        }
        int query(int x) {
            int ans = 0;
            for(int i = x; i; i -= i & -i) {
                ans += a[i];
            }
            return ans;
        }
    } bit;
    
    #define lson l, mid, rt << 1
    #define rson mid + 1, r, rt << 1 | 1
    struct SegmentTree {
        int a[N << 2], lazy[N << 2], flip[N << 2];
        inline void push(int rt) {
            if(lazy[rt]) {
                a[rt << 1] = lazy[rt];
                a[rt << 1 | 1] = lazy[rt];
                lazy[rt << 1] = lazy[rt];
                flip[rt << 1] = 0;
                lazy[rt << 1 | 1] = lazy[rt];
                flip[rt << 1 | 1] = 0;
                lazy[rt] = 0;
            }
            if(flip[rt]) {
                a[rt << 1] = -a[rt << 1];
                a[rt << 1 | 1] = -a[rt << 1 | 1];
                if(lazy[rt << 1]) lazy[rt << 1] = -lazy[rt << 1];
                else flip[rt << 1] ^= 1;
                if(lazy[rt << 1 | 1]) lazy[rt << 1 | 1] = -lazy[rt << 1 | 1];
                else flip[rt << 1 | 1] ^= 1;
                flip[rt] = 0;
            }
        }
        void update(int L, int R, int val, int l, int r, int rt) {
            if(R < l || r < L || R < L) return;
            if(L <= l && r <= R) {
                a[rt] = val;
                lazy[rt] = val;
                flip[rt] = 0;
                return;
            }
            push(rt);
            int mid = l + r >> 1;
            update(L, R, val, lson);
            update(L, R, val, rson);
        }
        void update2(int L, int R, int l, int r, int rt) {
            if(R < l || r < L || R < L) return;
            if(L <= l && r <= R) {
                a[rt] = -a[rt];
                if(lazy[rt]) lazy[rt] = -lazy[rt];
                else flip[rt] ^= 1;
                return;
            }
            push(rt);
            int mid = l + r >> 1;
            update2(L, R, lson);
            update2(L, R, rson);
        }
        int query(int p, int l, int r, int rt) {
            if(l == r) return a[rt];
            push(rt);
            int mid = l + r >> 1;
            if(p <= mid) return query(p, lson);
            else return query(p, rson);
        }
    } Tree;
    
    void show() {
        for(int i = 1; i <= n; i++) {
            int ans = 0;
            if(a[i] != 0) {
                if(abs(a[i]) > pos) ans = abs(a[i]) * Tree.query(abs(a[i]), 0, 100000, 1);
                else ans = (bit.query(abs(a[i])) & 1) ? -a[i] : a[i];
            }
            printf("%d%c", ans, " 
    "[i == n]);
        }
    }
    
    int main() {
        scanf("%d%d", &n, &q);
        for(int i = 1; i <= n; i++) scanf("%d", &a[i]);
        pos = 100000;
        while(q--) {
            int x;
            scanf("%s%d", op, &x);
            if(*op == '>') {
                if(x >= 0) {
                    pos = min(pos, x);
                    Tree.update(x + 1, 100000, -1, 0, 100000, 1);
                }
                else {
                    pos = min(pos, -x - 1);
                    Tree.update(-x, 100000, -1, 0, 100000, 1);
                    Tree.update2(1, -x - 1, 0, 100000, 1);
                    bit.modify(1, 1);
                    bit.modify(-x, -1);
                }
            }
            else {
                if(x <= 0) {
                    pos = min(pos, -x);
                    Tree.update(-x + 1, 100000, 1, 0, 100000, 1);
                }
                else {
                    pos = min(pos, x - 1);
                    Tree.update(x, 100000, 1, 0, 100000, 1);
                    Tree.update2(1, x - 1, 0, 100000, 1);
                    bit.modify(1, 1);
                    bit.modify(x, -1);
                }
            }
        }
        show();
        return 0;
    }
    
    /*
    */
  • 相关阅读:
    js获取盒子scrollTop
    获取浏览器宽度,自适应屏幕
    js切割字符串
    有向图的欧拉路径POJ2337
    欧拉回路和欧拉路径
    HDU 4462(暴力枚举)
    HDU 4455(dp)
    鸽巢原理入门
    HDU 4819 Mosaic(二维线段树)
    POJ 1330 Nearest Common Ancestors(LCA模板)
  • 原文地址:https://www.cnblogs.com/CJLHY/p/11791682.html
Copyright © 2020-2023  润新知