HDU - 5023 A Corrupt Mayor's Performance Art 线段树 按位记录颜色

A Corrupt Mayor's Performance Art

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 100000/100000 K (Java/Others)
Total Submission(s): 2984    Accepted Submission(s): 1070


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?
 

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.
 

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 10P 1 2 3P 2 3 4Q 2 3Q 1 3P 3 5 4P 1 2 7Q 1 3Q 3 4P 5 5 8Q 1 50 0
 

Sample Output
 
  
43 44 744 7 8
 

Source
 

Recommend

hujie


题解:


题目大致的意思是: 有一个画板可以用至多30种颜色进行绘画.初始画板的颜色全为2

对于操作1 P l r x: 将[l,r] 区间全部染色为x颜色

对于操作2 Q l r : 查询[l,r] 区间中有对少种颜色


对于操作1 : 线段树的区间覆盖

操作2         : 由于需要统计区间中的不同颜色个数 

我们设col[rt] >= 1 表示整个区间都是一种颜色. 直接记录该颜色 

col[rt] = 0 表示该区域没有颜色或者有很多颜色, 向下继续判断其儿子节点的颜色


代码:

/**
* 初始化的颜色为2!!
* 区间查询时 如果该节点有颜色则直接记录,没有颜色或者有很多颜色就继续向儿子节点查询!
* 暴搜思路!数据强点会超时
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+ 10;
vector< int> ans;
bool vis[ 35];
struct SegTree
{
int color[maxn<< 2];
void push_down( int rt) {
if(color[rt] == 0) return;
color[rt<< 1] = color[rt<< 1| 1] = color[rt];
color[rt] = 0;
}
void build( int l, int r, int rt) {
color[rt] = 2;
if(l == r) return;
int mid = (l+r)>> 1;
build(l,mid,rt<< 1);
build(mid+ 1,r,rt<< 1| 1);
}
void update( int ql, int qr, int val, int l, int r, int rt) {
if(ql == l && qr == r) {
color[rt] = val;
return;
}
push_down(rt);
int mid = (l+r)>> 1;
if(qr <= mid) update(ql,qr,val,l,mid,rt<< 1);
else if(ql > mid) update(ql,qr,val,mid+ 1,r,rt<< 1| 1);
else {
update(ql,mid,val,l,mid,rt<< 1);
update(mid+ 1,qr,val,mid+ 1,r,rt<< 1| 1);
}
}
void query( int ql, int qr, int l, int r, int rt) {
if(ql == l && qr == r) {
if(color[rt]) {
if(!vis[color[rt]]) vis[color[rt]] = 1;
return;
}
}
if(l == r) return;
push_down(rt);
int mid = (l+r)>> 1;
if(qr <= mid) query(ql,qr,l,mid,rt<< 1);
else if(ql > mid) query(ql,qr,mid+ 1,r,rt<< 1| 1);
else {
query(ql,mid,l,mid,rt<< 1);
query(mid+ 1,qr,mid+ 1,r,rt<< 1| 1);
}
}
}seg;
int main()
{
int n,q;
char op[ 10];
int l,r,col;
while( ~scanf( "%d%d",&n,&q),n+q)
{
seg. build( 1,n, 1);
while(q--)
{
scanf( "%s",op);
if(op[ 0] == 'P') {
scanf( "%d%d%d",&l,&r,&col);
seg. update(l,r,col, 1,n, 1);
}
else if(op[ 0] == 'Q'){
ans. clear();
scanf( "%d%d",&l,&r);
memset(vis, 0, sizeof(vis));
seg. query(l,r, 1,n, 1);
bool flag = 1;
for( int i= 1;i<= 30;i++) if(vis[i]) {
ans. push_back(i);
}
if(!ans. size()) {
//puts("");
continue;
}
for( int i= 0;i<ans. size()- 1;i++) printf( "%d ",ans[i]);
printf( "%d \n ",ans[ans. size()- 1]);
}
}
}
return 0;
}

 

其实写完我们应该意识到这个算法其实是超时的.

假设查询时[l,r] 区间有很多颜色,且每个单位线段就是一种颜色.这样查询的时间是O(n)的

显然对于q条操作来说, 时间是O(qn) 是会超时的


那么怎么使得单次查询的时间复杂度严格达到O(logn)呢

我们发现颜色总共才只有30种颜色

那么我们的sum[rt] 可以用每一位记录下当前区间拥有哪些颜色

用 | (或) 运算合并两个儿子区间节点的颜色


代码:

/**
* 区间更新的时候 push_up(rt) 将rt的两个儿子或到rt上,因为才只有30种颜色 2^30是没有问题的.
*/
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6+ 10;
vector< int> arr;
struct SegTree
{
int col[maxn<< 2],lz[maxn<< 2];
void push_up( int rt) {
col[rt] = 0;
col[rt] |= col[rt<< 1];
col[rt] |= col[rt<< 1| 1];
}
void push_down( int rt) {
if(!lz[rt]) return;
lz[rt<< 1] = lz[rt<< 1| 1] = lz[rt];
col[rt<< 1] = col[rt<< 1| 1] = lz[rt];
lz[rt] = 0;
}
void build( int l, int r, int rt) {
if(l == r) {
lz[rt] = 0;
col[rt] = 1<< 2;
return ;
}
push_down(rt);
int mid = (l+r)>> 1;
build(l,mid,rt<< 1);
build(mid+ 1,r,rt<< 1| 1);
push_up(rt);
}
void update( int ql, int qr, int val, int l, int r, int rt) {
if(ql == l && qr == r) {
col[rt] = 1<<val;
lz[rt] = 1<<val;
return ;
}
push_down(rt);
int mid = (l+r)>> 1;
if(qr <= mid) update(ql,qr,val,l,mid,rt<< 1);
else if(ql > mid) update(ql,qr,val,mid+ 1,r,rt<< 1| 1);
else {
update(ql,mid,val,l,mid,rt<< 1);
update(mid+ 1,qr,val,mid+ 1,r,rt<< 1| 1);
}
push_up(rt);
}
int query( int ql, int qr, int l, int r, int rt) {
if(ql == l && qr == r) {
return col[rt];
}
push_down(rt);
int mid = (l+r)>> 1;
if(qr <= mid) return query(ql,qr,l,mid,rt<< 1);
if(ql > mid) return query(ql,qr,mid+ 1,r,rt<< 1| 1);
return query(ql,mid,l,mid,rt<< 1) | query(mid+ 1,qr,mid+ 1,r,rt<< 1| 1);
}
}seg;
int main()
{
int n,q;
char op[ 10];
int l,r,col;
while( ~scanf( "%d%d",&n,&q),n+q)
{
seg. build( 1,n, 1);
while(q--)
{
scanf( "%s",op);
if(op[ 0] == 'P') {
scanf( "%d%d%d",&l,&r,&col);
seg. update(l,r,col, 1,n, 1);
}
else if(op[ 0] == 'Q'){
arr. clear();
scanf( "%d%d",&l,&r);
int ans = seg. query(l,r, 1,n, 1);
//printf("%d\n",ans);
ans >>= 1;
int t = 1;
while(ans) {
if(ans & 1) {
arr. push_back(t);
}
t++;ans>>= 1;
}
for( int i= 0;i<arr. size()- 1;i++) printf( "%d ",arr[i]);
printf( "%d \n ",arr[arr. size()- 1]);
}
}
}
return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_38013346/article/details/80542720