• 【luogu P1558 色板游戏】 题解


    题目链接:https://www.luogu.org/problemnew/show/P1558

    我知道三十棵线段树很暴力,可是我们可以状压啊。

    颜色最多30,不会爆int

    另外 吐槽评测机

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    using namespace std;
    const int maxn = 100010;
    int n, k, c, q;
    class Segment_Tree{
        public:
            #define lson l, mid, rt<<1
            #define rson mid+1, r, rt<<1|1
            int tree[maxn<<2], lazy[maxn<<2];
            void build(int l, int r, int rt)
            {
                if(l == r)
                {
                    tree[rt] = 1<<1;
                    return;
                }
                int mid = (l + r) >> 1;
                build(lson);
                build(rson);
                PushUP(rt);
            }
            void update(int left, int right, int add, int l, int r, int rt)
            {
                if(l >= left && r <= right)
                {
                    lazy[rt] = add;
                    tree[rt] = (1 << add);
                    return;
                }
                int mid = (l + r) >> 1;
                PushDOWN(l, r, rt);
                if(left <= mid) update(left, right, add, lson);
                if(right > mid) update(left, right, add, rson);
                PushUP(rt);
            }
            int query(int left, int right, int l, int r, int rt)
            {
                int res = 0;
                if(l >= left && r <= right)
                {
                    return tree[rt];
                }
                int mid = (l + r) >> 1;
                PushDOWN(l, r, rt);
                if(left <= mid) res |= query(left, right, lson);
                if(right > mid) res |= query(left, right, rson);
                return res;
            }
        private:
            void PushUP(int rt)
            {
                tree[rt] = tree[rt<<1] | tree[rt<<1|1];
            }
            void PushDOWN(int l, int r, int rt)
            {
                if(lazy[rt])
                {
                    tree[rt<<1] = (1 << lazy[rt]);
                    tree[rt<<1|1] = (1 << lazy[rt]);
                    lazy[rt<<1] = lazy[rt];
                    lazy[rt<<1|1] = lazy[rt];
                    lazy[rt] = 0;
                }
            }
    }T;
    int main()
    {
        scanf("%d%d%d",&n,&c,&q);
        T.build(1,n,1);
        for(int i = 1; i <= q; i++)
         {
         	char opt;
         	int x, y, k;
            cin>>opt;
            if(opt=='C')
              {
                scanf("%d%d%d",&x, &y, &k);
                if (x > y) swap(x, y);
                T.update(x, y, k, 1, n, 1);
            }
            else
              {
                scanf("%d%d",&x,&y);
                if (x > y) swap(x, y);
                int res = T.query(x,y,1,n,1), ans=0;
                for (int j = 1; j <= c; j++) if (res & (1 << j)) ans++;
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
    
  • 相关阅读:
    TUN/TAP区别
    从日志文件解决ArcGIS Server性能低下问题的步骤(1)
    java异常
    Maven
    前车之鉴-web篇
    图论复习总结
    奇(qi)谋(ji)巧(yin)计(qiao)
    莫比乌斯反演呓语
    学习后缀数组笔记
    浅读叶青学长竞赛学习知识目录
  • 原文地址:https://www.cnblogs.com/MisakaAzusa/p/9889890.html
Copyright © 2020-2023  润新知