zoukankan      html  css  js  c++  java
  • CF Educational Round 23 F.MEX Queries

    写了3小时 = =。这两天堕落了,昨天也刷了一晚上hihocoder比赛,还爆了零。之后得节制点了,好好准备考研。。

    首先很容易想到 压缩数据 + 线段树
    然后对于Pushdown真很难写。。需要牵涉到状态修改(所以我又写了一个adjust函数,辅助修改)
    我一直跪在test7,因为3号修改在一开始就会出现cover符号的修改,我一开始没有加(比方说1-4都是0,现在 做3 1 4,直接吧1-4的状态改为1就行了)

    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <ctime>
    #include <algorithm>
    #include <iostream>
    #include <map>
    #include <set>
    #include <queue>
    #include <cmath>
    using namespace std;
    typedef long long ll;
    #define lson l,m, rt<<1
    #define rson m+1, r, rt<<1|1
    const int N = 2e5+5;
    const int INF = 0x3f3f3f3f;
    
    int a[N]; ll b[N], c[N];
    ll has[N << 1];
    map<ll, int> mp;
    
    int sum[N << 2];
    int cover[N << 2];
    
    void adjust(int rt) {
        if(cover[rt] == 0) cover[rt] = -2;
        else if(cover[rt] == -2) cover[rt] = 0;
        else cover[rt] *= -1;
    } 
    
    void Pushdown(int rt, int len) {
        if(cover[rt] != 0) {
            if(cover[rt] == 1) {
                cover[rt << 1] = cover[rt << 1|1] = cover[rt];
                sum[rt << 1] = (len+1) / 2;
                sum[rt << 1|1] = len / 2;
            }else if(cover[rt] == -1) {
                cover[rt << 1] = cover[rt << 1|1] = cover[rt];
                sum[rt << 1] = 0;
                sum[rt << 1|1] = 0;
            }else {
                sum[rt << 1] = (len+1) / 2 - sum[rt << 1];
                sum[rt << 1|1] = len / 2 - sum[rt << 1|1];
    
                adjust(rt << 1); adjust(rt << 1|1); 
            }
            cover[rt] = 0;
        }
    }
    
    void Add(int L, int R, int l, int r, int rt) {
        if(sum[rt] == r-l+1) return;
        if(L <= l && r <= R) {
            sum[rt] = r-l+1;
            cover[rt] = 1;
            return ;
        }
        int m = (l + r) >> 1;
        Pushdown(rt, r-l+1);
        if(L <= m) Add(L, R, lson); 
        if(R > m)  Add(L, R, rson);
        sum[rt] = sum[rt << 1] + sum[rt << 1|1];
    }
    void Delete(int L, int R, int l, int r, int rt) {
        if(sum[rt] == 0) return;
        if(L <= l && r <= R) {
            sum[rt] = 0;
            cover[rt] = -1;
            return;
        }
        int m = (l + r) >> 1;
        Pushdown(rt, r-l+1);
    
        if(L <= m) Delete(L, R, lson); 
        if(R > m)  Delete(L, R, rson);
        sum[rt] = sum[rt << 1] + sum[rt << 1|1];
    }
    void Invert(int L, int R, int l, int r, int rt) {
        if(L <= l && r <= R) {
            adjust(rt);
            sum[rt] = (r-l+1) - sum[rt];
            return;
        }
        int m = (l + r) >>1;
        Pushdown(rt, r-l+1);
        if(L <= m) Invert(L, R, lson);
        if(R > m) Invert(L, R, rson);
        sum[rt] = sum[rt << 1] + sum[rt << 1|1];
    }
    int suc = 0;
    void Find(int l, int r, int rt) {
        if(suc) return;
        if(sum[rt] == r-l+1) return;
        else if(sum[rt] == 0) {
            printf("%lld
    ", has[l-1]); suc = 1; return;
        }
        Pushdown(rt, r-l+1);
        int m = (l + r) >>1;
        Find(lson); Find(rson);
    }
    
    int main() {
        int q;
        while(~scanf("%d", &q)) {
            mp.clear();
            memset(sum, 0, sizeof(sum));
            memset(cover, 0, sizeof(cover));
    
            int tot = 0;
            has[tot ++] = 1;
            for(int i = 0; i < q; ++i) {
                scanf("%d %lld %lld", &a[i], &b[i], &c[i]);
                has[tot ++ ] = b[i]; has[tot ++ ] = c[i]+1; 
            }   
            sort(has, has + tot);
            tot = unique(has, has + tot) - has;
            for(int i = 0; i < tot; ++i) {
                mp[has[i]] = i+1;
                //  printf("%lld ", has[i]);
            }
    
            for(int i = 0; i < q; ++i) {
                if(a[i] == 1) {
                    Add(mp[b[i]], mp[c[i]+1]-1, 1, tot, 1);
                }else if(a[i] == 2) {
                    Delete(mp[b[i]], mp[c[i]+1]-1, 1, tot, 1);
                }else Invert(mp[b[i]], mp[c[i]+1]-1, 1, tot, 1);
                suc = 0; Find(1, tot, 1);
            }
    
    
        }
        return 0;
    }
  • 相关阅读:
    PythonStudy——epoll 模块实现异步IO模型
    MySQLStudy——Mac下MySQL 允许用户远程访问数据库
    MySQLStudy——MySQL 基础语句
    MySQLStudy——MySQL 概念
    MySQLStudy——Mac下MySQL 常用命令 启动 关闭 重启服务 查看版本
    PythonStudy——IO模型
    PythonStudy——非阻塞IO模型
    PythonStudy——多路复用IO select实现
    restfull api
    斜体菜单
  • 原文地址:https://www.cnblogs.com/Basasuya/p/8433698.html
Copyright © 2011-2022 走看看