poj 2777

来源:互联网 发布:php软件环境是什么 编辑:程序博客网 时间:2024/06/06 03:31

题目

用了二进制中1的个数来统计不同的颜色数

最后在求解时用0来与这个数中的1或,统计颜色个数

仍然是区间更新,区间询问

代码如下:

#include<cstdio>#include<iostream>using namespace std;#define maxn 100010#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1struct Tree{    int l,r,col,lazy;}tree[maxn<<2];int ans;void PushUp(int rt){    tree[rt].col=tree[rt<<1].col|tree[rt<<1|1].col;}void PushDown(int rt){    if(tree[rt].lazy){        tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy;        tree[rt<<1].col=tree[rt<<1|1].col=tree[rt].col;        tree[rt].lazy=0;    }}void build(int l,int r,int rt){    tree[rt].l=l;    tree[rt].r=r;    tree[rt].lazy=0;    if(l==r) return ;    int m=(l+r)>>1;    build(lson);    build(rson);}void update(int x,int y,int col,int rt){    int l,r;    l=tree[rt].l;    r=tree[rt].r;    if(l==x&&r==y){        tree[rt].col=col;        tree[rt].lazy=1;        return ;    }    PushDown(rt);    int m=(l+r)>>1;    if(x<=m) update(x,min(m,y),col,rt<<1);    if(y>m) update(max(m+1,x),y,col,rt<<1|1);    PushUp(rt);}void query(int x,int y,int rt){    int l,r;    l=tree[rt].l;    r=tree[rt].r;    if(l==x&&r==y){        ans=ans|tree[rt].col;        return ;    }    PushDown(rt);    int m=(l+r)>>1;    if(x<=m)  query(x,min(m,y),rt<<1);    if(y>m)  query(max(m+1,x),y,rt<<1|1);}int main(){     int i,j,n,q,x,y,z,cnt,num;     char str[5];    while(scanf("%d%d%d",&n,&num,&q)!=EOF)    {        build(1,n,1);        update(1,n,1,1);        while(q--)        {            scanf("%s",str);            scanf("%d%d",&x,&y);            if (x>y)            {                swap(x,y);            }            if (str[0]=='C')            {                scanf("%d",&z);                update(x,y,1<<(z-1),1);            }            else            {                ans=0;                query(x,y,1);                cnt=0;                for (i=0;i<num;i++)                {                    if ((ans & (1<<i))!=0) cnt++;                }                printf("%d\n",cnt);            }        }    }    return 0;}


原创粉丝点击