题意: 给出每次更改和查询,询问区间中有多少种不同颜色。注意初始全部为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");
}
}
}
}