hdu 5023-A Corrupt Mayor's Performance Art

题意: 给出每次更改和查询,询问区间中有多少种不同颜色。注意初始全部为2号颜色

思路:

状压每种颜色,查询时候看1出现的状态位

#include <iostream>
#include <stdio.h>
#include <map>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int maxn=1000005; 
int vis[maxn]; 
long long ans;
struct node
{
    int left,right;
    long long num,lazy;
}tree[maxn*4]; 

void push_down(int i)
{
    if(!tree[i].lazy)
        return ;
    tree[i << 1].num = tree[i << 1 | 1].num = (1LL << tree[i].lazy);
    tree[i << 1].lazy = tree[i << 1 | 1].lazy = tree[i].lazy;
    tree[i].lazy = 0;
}
void push_up(int i)
{
    tree[i].num = tree[i << 1].num | tree[i << 1 | 1].num;

}
void build(int i,int left,int right)
{
    tree[i].lazy=0;
    tree[i].left=left;
    tree[i].right=right;
    if(left==right)
    {
        return ;
    }
    int mid=(tree[i].left+tree[i].right)>>1;
    build(i<<1,left,mid);
    build(i<<1|1,mid+1,right);
}

void update(int i,int left,int right,long long  w)
{
    if(tree[i].left==left&&tree[i].right==right)
    {
        tree[i].lazy=w;
        tree[i].num=(1LL<<w);
        return ;
    }
    push_down(i);
    int mid=(tree[i].left+tree[i].right)>>1;
    if(right<=mid)
        update(i<<1,left,right,w);
    else if(left>mid)
        update(i<<1|1,left,right,w);
    else
    {
        update(i<<1,left,mid,w);
        update(i<<1|1,mid+1,right,w);
    }
    push_up(i);
    return ;
}
void query(int i,int left,int right)
{
    if(tree[i].left==left&&tree[i].right==right )
    {
        ans |= tree[i].num;
        return ;
    }

    push_down(i);
    int mid=(tree[i].left+tree[i].right)>>1;
    if(right<=mid)
    {
        query(i<<1,left,right);
    }
    else if(left>mid)
    {
        query(i<<1|1,left,right);
    }
    else
    {
        query(i<<1,left,mid);
        query(i<<1|1,mid+1,right);
    }

    push_up(i);
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m)){
            if(n==0&&m==0) break;
    build(1,1,n);
    update(1,1,n,2);
    while(m--)
    {
        char op[5];
        int x,y,c;
        scanf("%s",op);
        if(op[0]=='P')
        {
            scanf("%d%d%d",&x,&y,&c);
            update(1,x,y,c);
        }
        else
        {
            ans=0;
            scanf("%d%d",&x,&y);
            query(1,x,y);
            int cnt=0;
            for(int i=1;i<=61;i++) {
                if(ans & (1LL<<i))
                {
                    if(cnt==0)cnt=1;
                    else printf(" ");
                    printf("%d",i);
                }
            }
            printf("\n");
        }
    }

    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值