HDU 5023 A Corrupt Mayor's Performance Art

题意:

对一个栅栏有两种操作,一种是把从a到b的所有栅栏都刷成c,另一种是询问从a到b有多少种颜色。(栅栏初始的颜色是2)。

思路:

线段树的一道题。

利用位运算存储颜色,反正颜色只用30种,int正好够用。

利用延迟标记更新节点。

想下延伸的时候,需要把本身就置成要修改的颜色,因为标记的时候,这个节点下面的节点就应该都被修改。

Code:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<queue>
#include<stack>
#include<bitset>
#include<set>
#include<map>
#include<cctype>
#include<vector>

//#define TEST

#define LL long long
#define Mt(f, x) memset(f, x, sizeof(f));
#define rep(i, s, e) for(int i = (s); i <= (e); ++i)
#ifdef TEST
    #define See(a) cout << #a << " = " << a << endl;
    #define See2(a, b) cout << #a << " = " << a << ' ' << #b << " = " << b << endl;
    #define debug(a, s, e){ rep(_i, s, e) {cout << a[_i] << ' '; }cout << endl;}
    #define debug2(a, s, e, ss, ee) rep(i_, s, e) {debug(a[i_], ss, ee);}
#else
    #define See(a)
    #define See2(a, b)
    #define debug(a, s, e)
    #define debug2(a, s, e, ss, ee)
#endif

const int MAX = 2e9;
const int MIN = -2e9;
const int PI = acos(-1.0);
const double eps = 1e-8;

using namespace std;

#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1

const int N = 1000000 + 5;

int col[N << 2], dif[N << 2];

void pushUp(int rt)
{
//    See(rt);
    col[rt] = col[rt << 1] | col[rt << 1 | 1];
}

void build(int l, int r, int rt)
{
    if(l == r)
    {
        col[rt] = 1 << 2;
        return ;
    }
    int m = (l + r) >> 1;
    build(lson);
    build(rson);
    pushUp(rt);
}

void pushDown(int rt)
{
//    See(rt);
    if(dif[rt])
    {
        dif[rt << 1] = dif[rt << 1 | 1] = dif[rt];
        col[rt << 1] = col[rt << 1 | 1] = dif[rt];
        dif[rt] = 0;
    }
}

void update(int L, int R, int c, int l, int r, int rt)
{
    if(l >= L && r <= R)
    {
//        See(rt);
        dif[rt] = 1 << c;
        col[rt] = dif[rt];
        return ;
    }
    pushDown(rt);
    int m = (l + r) >> 1;
    if(L <= m) update(L, R, c, lson);
    if(R > m) update(L, R, c, rson);
    pushUp(rt);
}

int query(int L,int R, int l, int r, int rt)
{
    if(l >= L && r <= R)
    {
        return col[rt];
    }
    pushDown(rt);
    int ret = 0;
    int m = (l + r) >> 1;
    if(L <= m) ret |= query(L, R, lson);
    if(R > m) ret |= query(L, R, rson);
    return ret;
}


void print(int t)
{
    bool first = true;
    for(int i = 1; i <= 30; ++i)
    {
        if(t & (1 << i))
        {
            printf(first ? "%d" : " %d", i);
            first = false;
        }
    }
    printf("\n");
}

int main()
{
    int n, q;
    while(~scanf("%d%d", &n, &q))
    {
        Mt(dif, 0);
        if(n + q == 0)
        {
            break;
        }
        build(1, n, 1);
        while(q--)
        {
            char op[2];
            int a, b, c;
            scanf("%s%d%d", op, &a, &b);
            if(op[0] == 'P')
            {
                scanf("%d", &c);
                update(a, b, c, 1, n, 1);
            }
            else
            {
                int t = query(a, b, 1, n, 1);
                print(t);
            }
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值