F - MEX Queries 线段树上二分 离散化端点 细节lazy标记

题意:
给出三个操作:
1 区间置1
2.区间置零
3.区间反转,零变1,1变成0;
每一次操作过后,求出第一个零开始的位置。
端点直接到了1e18,longlong级数了,所以肯定是先离散化端点,而这里我们求的并不是区间信息,而是只需要看这区间是否是1或者0,所以离散化后不需要+1操作。
置1,置0,基本操作,区间更新,推至lazy标记。
区间反转,基本操作,tree[root]=(区间长度-tree[root])(tree数组保存当前区间1得数量)
当把三个操作合在一起的时候,就需要注意细节。
我们lazy标记不能只用一个,应该一个用来置1置0,一个用来反转。而且推标记有优先级,置1置0操作会把反转标记置0,同时,反转一个区间偶数次等于没有反转。
最后统计答案,我们二分答案然后验证从1~mid这个区间长度是否等于mid-1+1,等于的话就说明该区间全为1,往后二分。
对于答案的统计,我们离散化了端点,答案并不一定是端点值,还可能是r+1,l+1,也应该一并加入并且离散化。

    #include<iostream>
    #include<cstring>
    #include<algorithm>
    #include<cmath>
    #include<cstdlib>
    #include<climits>
    #include<stack>
    #include<vector>
    #include<queue>
    #include<set>
    #include<map>
    //#include<regex>
    #include<cstdio>
    #define up(i,a,b)  for(int i=a;i<b;i++)
    #define dw(i,a,b)  for(int i=a;i>b;i--)
    #define upd(i,a,b) for(int i=a;i<=b;i++)
    #define dwd(i,a,b) for(int i=a;i>=b;i--)
    //#define local
    typedef long long ll;
    const double esp = 1e-6;
    const double pi = acos(-1.0);
    const int INF = 0x3f3f3f3f;
    const int inf = 1e9;
    using namespace std;
    ll read()
    {
        char ch = getchar(); ll x = 0, f = 1;
        while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); }
        while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); }
        return x * f;
    }
    typedef pair<int, int> pir;
    #define lson l,mid,root<<1
    #define rson mid+1,r,root<<1|1
    #define lrt root<<1
    #define rrt root<<1|1
    const int N = 4e5 + 10;
    ll tree[N<<2];
    ll lazy[N<<2];
    ll lazy2[N<<2];
    void pushup(int root)
    {
        tree[root] = tree[lrt] + tree[rrt];
    }
    void pushdown(int root,int l,int r)
    {
        int lenr = (r - l + 1) >> 1;
        int lenl = (r - l + 1) - lenr;
        if (lazy[root])
        {
            lazy[lrt] = lazy[rrt] = lazy[root];
            lazy2[lrt] = lazy2[rrt] = 0;
            if (lazy[root] == 1)
            {
                tree[lrt] = lenl, tree[rrt] = lenr;
            }
            else if (lazy[root] == 2)
            {
                tree[lrt] = tree[rrt] = 0;
            }
            lazy[root] = 0;
        }
        if (lazy2[root] % 2)
        {
            lazy2[lrt] += lazy2[root];
            lazy2[rrt] += lazy2[root];
            tree[lrt] = lenl - tree[lrt]; tree[rrt] = lenr - tree[rrt];
            lazy2[root] = 0;
        }
    }
    void build(int l, int r, int root)
    {
        lazy[root] = 0; lazy2[root] = 0;
        if (l == r)
        {
            tree[root] = 0;
            return;
        }
        int mid = (l + r) >> 1;
        build(lson);
        build(rson);
        pushup(root);
    }
    void update(int l, int r, int root, int lf, int rt, int op)
    {
        if (lf <= l && r <= rt)
        {
            if (op == 1)
            {
                tree[root] = r - l + 1; lazy[root] = 1; lazy2[root] = 0;
            }
            else if(op==2)
            {
                tree[root] = 0; lazy[root] = 2; lazy2[root] = 0;
            }
            else
            {
                tree[root] = r - l + 1 - tree[root]; lazy2[root]++;
            }
            return;
        }
        pushdown(root, l, r);
        int mid = (l + r) >> 1;
        if (lf <= mid)
        {
            update(lson, lf, rt, op);
        }
        if(rt>mid) update(rson, lf, rt, op);
        pushup(root);
    }
    int querry(int l, int r, int root,int lf,int rt)
    {
        if (lf <= l && r <= rt)
        {
            return tree[root];
        }
        pushdown(root, l, r);
        int mid = (l + r) >> 1;
        int ans = 0;
        if (lf <= mid)ans += querry(lson, lf, rt);
        if (rt > mid)ans += querry(rson, lf, rt);
        return ans;
    }
    vector<ll>hi;
    struct node { ll l, r, op; }a[N];
    ll t, l, r, n;
    int findpos(ll val)
    {
        return lower_bound(hi.begin(), hi.end(), val) - hi.begin();
    }
    int main()
    {
        n = read();
        int lim = n;
        up(i, 0, n)
        {
            t = read(), l = read(), r = read();
            a[i] = node{ l,r,t };
            hi.push_back(l), hi.push_back(r); hi.push_back(r + 1); hi.push_back(l + 1);
        }
        hi.push_back(1);
        sort(hi.begin(), hi.end());
        hi.erase(unique(hi.begin(), hi.end()), hi.end());
        n = hi.size(); 
        build(1, n, 1);
        up(i, 0,lim)
        {
            t = a[i].op; l = findpos(a[i].l)+1; r = findpos(a[i].r)+1;
            update(1, n, 1, l, r, t);
            int l = 0; int r = n+1;
            /*cout << "q" << querry(1, n, 1, 1, n) << endl;
            cout << "q" << querry(1, n, 1, 1, 4) << endl;
            cout << "q" << querry(1, n, 1, 1, 2) << endl;*/
        //  cout << "q" << querry(1, n, 1, 1, 1) << endl;
            while (r - 1 > l)
            {
                int midd = (r + l) >> 1;
                //cout << "midd" << midd << endl;
                int temp = querry(1, n, 1, 1, midd);
                    //cout << "temp" << temp << endl;
                if (temp == midd)l = midd;
                else r = midd;
            }
            printf("%lld\n", hi[r - 1]);
        }
        return 0;
    }

猜你喜欢

转载自www.cnblogs.com/LORDXX/p/11625607.html
Mex