A Corrupt Mayor's Performance Art
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 1673 Accepted Submission(s): 590
Problem Description
Corrupt governors always find ways to get dirty money. Paint something, then sell the worthless painting at a high price to someone who wants to bribe him/her on an auction, this seemed a safe way for mayor X to make money.
Because a lot of people praised mayor X's painting(of course, X was a mayor), mayor X believed more and more that he was a very talented painter. Soon mayor X was not satisfied with only making money. He wanted to be a famous painter. So he joined the local painting associates. Other painters had to elect him as the chairman of the associates. Then his painting sold at better price.
The local middle school from which mayor X graduated, wanted to beat mayor X's horse fart(In Chinese English, beating one's horse fart means flattering one hard). They built a wall, and invited mayor X to paint on it. Mayor X was very happy. But he really had no idea about what to paint because he could only paint very abstract paintings which nobody really understand. Mayor X's secretary suggested that he could make this thing not only a painting, but also a performance art work.
This was the secretary's idea:
The wall was divided into N segments and the width of each segment was one cun(cun is a Chinese length unit). All segments were numbered from 1 to N, from left to right. There were 30 kinds of colors mayor X could use to paint the wall. They named those colors as color 1, color 2 .... color 30. The wall's original color was color 2. Every time mayor X would paint some consecutive segments with a certain kind of color, and he did this for many times. Trying to make his performance art fancy, mayor X declared that at any moment, if someone asked how many kind of colors were there on any consecutive segments, he could give the number immediately without counting.
But mayor X didn't know how to give the right answer. Your friend, Mr. W was an secret officer of anti-corruption bureau, he helped mayor X on this problem and gained his trust. Do you know how Mr. Q did this?
Because a lot of people praised mayor X's painting(of course, X was a mayor), mayor X believed more and more that he was a very talented painter. Soon mayor X was not satisfied with only making money. He wanted to be a famous painter. So he joined the local painting associates. Other painters had to elect him as the chairman of the associates. Then his painting sold at better price.
The local middle school from which mayor X graduated, wanted to beat mayor X's horse fart(In Chinese English, beating one's horse fart means flattering one hard). They built a wall, and invited mayor X to paint on it. Mayor X was very happy. But he really had no idea about what to paint because he could only paint very abstract paintings which nobody really understand. Mayor X's secretary suggested that he could make this thing not only a painting, but also a performance art work.
This was the secretary's idea:
The wall was divided into N segments and the width of each segment was one cun(cun is a Chinese length unit). All segments were numbered from 1 to N, from left to right. There were 30 kinds of colors mayor X could use to paint the wall. They named those colors as color 1, color 2 .... color 30. The wall's original color was color 2. Every time mayor X would paint some consecutive segments with a certain kind of color, and he did this for many times. Trying to make his performance art fancy, mayor X declared that at any moment, if someone asked how many kind of colors were there on any consecutive segments, he could give the number immediately without counting.
But mayor X didn't know how to give the right answer. Your friend, Mr. W was an secret officer of anti-corruption bureau, he helped mayor X on this problem and gained his trust. Do you know how Mr. Q did this?
Input
There are several test cases.
For each test case:
The first line contains two integers, N and M ,meaning that the wall is divided into N segments and there are M operations(0 < N <= 1,000,000; 0<M<=100,000)
Then M lines follow, each representing an operation. There are two kinds of operations, as described below:
1) P a b c
a, b and c are integers. This operation means that mayor X painted all segments from segment a to segment b with color c ( 0 < a<=b <= N, 0 < c <= 30).
2) Q a b
a and b are integers. This is a query operation. It means that someone asked that how many kinds of colors were there from segment a to segment b ( 0 < a<=b <= N).
Please note that the operations are given in time sequence.
The input ends with M = 0 and N = 0.
For each test case:
The first line contains two integers, N and M ,meaning that the wall is divided into N segments and there are M operations(0 < N <= 1,000,000; 0<M<=100,000)
Then M lines follow, each representing an operation. There are two kinds of operations, as described below:
1) P a b c
a, b and c are integers. This operation means that mayor X painted all segments from segment a to segment b with color c ( 0 < a<=b <= N, 0 < c <= 30).
2) Q a b
a and b are integers. This is a query operation. It means that someone asked that how many kinds of colors were there from segment a to segment b ( 0 < a<=b <= N).
Please note that the operations are given in time sequence.
The input ends with M = 0 and N = 0.
Output
For each query operation, print all kinds of color on the queried segments. For color 1, print 1, for color 2, print 2 ... etc. And this color sequence must be in ascending order.
Sample Input
5 10 P 1 2 3 P 2 3 4 Q 2 3 Q 1 3 P 3 5 4 P 1 2 7 Q 1 3 Q 3 4 P 5 5 8 Q 1 5 0 0
Sample Output
4 3 4 4 7 4 4 7 8
//主要思路是标记节点内的颜色是否为纯 不纯为1 查询的时候不存向儿子搜索直到存为止
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <algorithm>
#include <map>
#include <queue>
using namespace std;
const int maxn=1000000+10;
const int inf=(1<<30);
int res[100],cnt;
struct Node
{
int l,r,co,mark; //mark为0值相同 不同值为1
int flag; //flag 延迟标记
int mid()
{
return (l+r)/2;
}
}tree[maxn*4];
void Pushdown(int rt)
{
tree[2*rt].co=tree[rt*2+1].co=tree[rt].co;
tree[2*rt].flag=tree[rt*2+1].flag=1;
tree[rt].flag=0;
tree[2*rt].mark=tree[rt*2+1].mark=0;
}
void Buildtree(int rt,int l,int r,int val)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].flag=0;
tree[rt].co=val;
tree[rt].mark=0;
if(l!=r)
{
Buildtree(2*rt,l,(l+r)/2,val);
Buildtree(2*rt+1,(l+r)/2+1,r,val);
}
}
void Update(int rt,int l,int r,int val)
{
if(tree[rt].l==l&&tree[rt].r==r)
{
tree[rt].co=val;
tree[rt].mark=0;
tree[rt].flag=1;
return;
}
if(tree[rt].flag)
Pushdown(rt);
if(r<=tree[rt].mid())
Update(2*rt,l,r,val);
else if(l>tree[rt].mid())
Update(2*rt+1,l,r,val);
else
{
Update(2*rt,l,tree[rt].mid(),val);
Update(2*rt+1,tree[rt].mid()+1,r,val);
}
tree[rt].mark=(tree[rt*2].mark||tree[2*rt+1].mark||tree[2*rt].co!=tree[2*rt+1].co);
if(tree[2*rt].mark==0&&tree[rt*2+1].mark==0&&tree[2*rt].co==tree[rt*2+1].co)
tree[rt].co=tree[2*rt].co; //需特判 当两个儿子的颜色相同并且为纯时父亲颜色要改变
}
void Query(int rt,int l,int r)
{
int c=tree[rt].co;
if(tree[rt].l==l&&tree[rt].r==r&&!tree[rt].mark)
{
res[c]++;
return;
}
if(tree[rt].flag)
Pushdown(rt);
if(r<=tree[rt].mid())
Query(2*rt,l,r);
else if(l>tree[rt].mid())
Query(2*rt+1,l,r);
else
{
Query(2*rt,l,tree[rt].mid());
Query(2*rt+1,tree[rt].mid()+1,r);
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)&&(n+m))
{
Buildtree(1,1,n,2);
while(m--)
{
char c;
getchar();
scanf("%c",&c);
if(c=='P')
{
int l,r,val;
scanf("%d%d%d",&l,&r,&val);
Update(1,l,r,val);
}
else if(c=='Q')
{
int l,r,flag=0;
cnt=0;
memset(res,0,sizeof(res));
scanf("%d%d",&l,&r);
Query(1,l,r);
for(int i=0;i<31;i++)
{
if(res[i]!=0&&!flag)
{
printf("%d",i);
flag=1;
}
else if(res[i]!=0)
{
printf(" %d",i);
}
}
printf("\n");
}
}
}
return 0;
}
第二种:
//线段树+状态压缩
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <cmath>
#include <algorithm>
#include <map>
#include <set>
#include <queue>
using namespace std;
const int maxn=1000000+10;
const int inf=(1<<30);
set <int> s;
set <int>::iterator it;
int res[50];
struct Node
{
int l,r,mark; //mark 延迟标记
int val; //val为这个节点所存的颜色
int mid()
{
return (l+r)/2;
}
}tree[maxn*4];
void Buildtree(int rt,int l,int r)
{
tree[rt].l=l;
tree[rt].r=r;
tree[rt].mark=0;
tree[rt].val=1<<1; //开始颜色为2
if(l!=r)
{
Buildtree(2*rt,l,(l+r)/2);
Buildtree(2*rt+1,(l+r)/2+1,r);
}
}
void Pushdown(int rt)
{
tree[2*rt].val=tree[rt*2+1].val=tree[rt].val;
tree[2*rt].mark=tree[rt*2+1].mark=1;
tree[rt].mark=0;
}
void Update(int rt,int l,int r,int val)
{
if(tree[rt].l==l&&tree[rt].r==r)
{
tree[rt].val=1<<val; //颜色更新
tree[rt].mark=1;
return;
}
if(tree[rt].mark)
Pushdown(rt);
if(r<=tree[rt].mid())
Update(2*rt,l,r,val);
else if(l>tree[rt].mid())
Update(2*rt+1,l,r,val);
else
{
Update(2*rt,l,tree[rt].mid(),val);
Update(2*rt+1,tree[rt].mid()+1,r,val);
}
tree[rt].val=(tree[2*rt].val|tree[2*rt+1].val); //父节点的颜色=两个字节点的按位和
}
void Query(int rt,int l,int r)
{
if(tree[rt].l==l&&tree[rt].r==r)
{
s.insert(tree[rt].val);
return;
}
if(tree[rt].mark) //注意标记
Pushdown(rt);
if(r<=tree[rt].mid())
Query(2*rt,l,r);
else if(l>tree[rt].mid())
Query(2*rt+1,l,r);
else
{
Query(2*rt,l,tree[rt].mid());
Query(2*rt+1,tree[rt].mid()+1,r);
}
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m)&&(n+m))
{
Buildtree(1,1,n);
while(m--)
{
getchar(); //绝逼不能忘。。
char ch;
scanf("%c",&ch);
if(ch=='P')
{
int l,r,val;
scanf("%d%d%d",&l,&r,&val);
Update(1,l,r,val-1);
}
else if(ch=='Q')
{
int l,r,flag=0;
s.clear();
memset(res,0,sizeof(res));
scanf("%d%d",&l,&r);
Query(1,l,r);
for(it=s.begin();it!=s.end();it++)
for(int i=0;i<31;i++)
if((*it)&1<<i)
res[i+1]++;
for(int i=0;i<31;i++)
{
if(res[i]&&!flag)
{
printf("%d",i);
flag=1;
}
else if(res[i])
printf(" %d",i);
}
printf("\n");
}
}
}
return 0;
}