hdu5023 A Corrupt Mayor's Performance Art(线段树+区间更新)

来源:互联网 发布:会计事务所审计软件 编辑:程序博客网 时间:2024/04/30 17:17

hdu5023

题目

P :A到B区间图上第C种颜色;
Q:A到B所有点存在的颜色种类;

思路

因为只有30种颜色,可以用位运算进行状态压缩,线段树就是区间更新,区间查询。

代码

#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#define lson l, m , rt << 1#define rson m + 1 ,r , rt << 1 | 1using namespace std;typedef long long ll;const int maxn=1000010;int n,m;ll col[maxn<<2];ll sum[maxn<<2];void PushDown(int rt){    if(col[rt])    {        col[rt<<1]=col[rt];        col[rt<<1|1]=col[rt];        sum[rt<<1]=col[rt];        sum[rt<<1|1]=col[rt];        col[rt]=0;    }}void PushUp(int rt){    sum[rt]=sum[rt<<1]|sum[rt<<1|1];}void build(int l,int r,int rt){    col[rt]=0;    if(l==r)    {        sum[rt]=2;        return;    }    int m=(l+r)>>1;    build(lson);    build(rson);    PushUp(rt);}void update(int c,int L,int R,int l,int r,int rt){    if(L<=l&&r<=R)    {        col[rt]=1<<(c-1);        sum[rt]=1<<(c-1);        return;    }    PushDown(rt);    int m=(l+r)>>1;    if(L<=m) update(c,L,R,lson);    if(R>m) update(c,L,R,rson);    PushUp(rt);}ll query(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R)    {        return sum[rt];    }    ll ans=0;    PushDown(rt);    int m=(l+r)>>1;    if(L<=m) ans|=query(L,R,lson);    if(R>m) ans|=query(L,R,rson);    return ans;}int main(){    while(scanf("%d %d",&n,&m)!=EOF)    {        if(n==0&&m==0)            break;        build(1,n,1);        char op;        int a,b,c;        for(int i=0; i<m; i++)        {            getchar();            scanf("%c",&op);            if(op=='P')            {                scanf("%d %d %d",&a,&b,&c);                update(c,a,b,1,n,1);            }            else if(op=='Q')            {                scanf("%d %d",&a,&b);                ll sum=query(a,b,1,n,1);                int cnt=0;                for(int j=1; j<=30; j++)                {                    if(sum&(1<<(j-1)))                    {                        if(cnt!=0)                            printf(" %d",j);                        else                            printf("%d",j);                        cnt++;                    }                }                printf("\n");            }        }    }    return 0;}
0 0