hdu 5023 线段树
来源:互联网 发布:移动4g网络差怎么办 编辑:程序博客网 时间:2024/05/22 16:13
A Corrupt Mayor's Performance Art
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 1655 Accepted Submission(s): 582
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 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
2014 ACM/ICPC Asia Regional Guangzhou Online
题意: 对区间进行染色,查询l到r区间中有那些颜色并进行输出。
分析: 比较好想的线段树的区间更新。由于颜色比较少,给每个节点都设置一个30的数组表示当前节点的颜色,这样就能快速进行区间更新和区间查询了。记得每次更新的时候要把原先的颜色抹除,因为颜色是覆盖的。 由于可能爆内存,所以用bitset来优化内存。
题意: 对区间进行染色,查询l到r区间中有那些颜色并进行输出。
分析: 比较好想的线段树的区间更新。由于颜色比较少,给每个节点都设置一个30的数组表示当前节点的颜色,这样就能快速进行区间更新和区间查询了。记得每次更新的时候要把原先的颜色抹除,因为颜色是覆盖的。 由于可能爆内存,所以用bitset来优化内存。
#include<bitset>#include<map>#include<vector>#include<cstdio>#include<iostream>#include<cstring>#include<string>#include<algorithm>#include<cmath>#include<stack>#include<queue>#include<set>#define inf 0x3f3f3f3f#define mem(a,x) memset(a,x,sizeof(a))using namespace std;typedef long long ll;typedef pair<int,int> pii;inline int in(){ int res=0;char c; while((c=getchar())<'0' || c>'9'); while(c>='0' && c<='9')res=res*10+c-'0',c=getchar(); return res;}const int N=1000010;struct node{ int l,r,flag; bitset <31> color; void init() { color.reset(); }}a[N<<2];inline void pushUp(int i){ a[i].init(); for(int j=0;j<31;j++) { if(a[i<<1].color[j] || a[i<<1|1].color[j]) a[i].color[j]=1; }}inline void pushDown(int i){ if(a[i].flag) { a[i<<1|1].init(); a[i<<1].init(); a[i<<1].flag=a[i].flag; a[i<<1|1].flag=a[i].flag; a[i<<1].color[a[i].flag]=1; a[i<<1|1].color[a[i].flag]=1; //a[i<<1|1].flag=a[i].flag; a[i].flag=0; }}void build(int l,int r,int i){ a[i].l=l,a[i].r=r; a[i].flag=0; a[i].init(); if(l==r) { a[i].color[2]=1; return; } int mid=(l+r)>>1; build(l,mid,i<<1); build(mid+1,r,i<<1|1); pushUp(i);}void update(int l,int r,int val,int i){ if(a[i].l>=l && a[i].r<=r) { a[i].flag=val; a[i].init(); //loule a[i].color[val]=1; return; } pushDown(i); int mid=(a[i].l+a[i].r)>>1; if(mid>=r) { update(l,r,val,i<<1); } else if(l>mid) { update(l,r,val,i<<1|1); } else { update(l,r,val,i<<1); update(l,r,val,i<<1|1); } pushUp(i);}int ans[31];void query(int l,int r,int i){ if(a[i].l>=l && a[i].r<=r) { for(int j=1;j<31;j++) { if(a[i].color[j]) ans[j]=1; } return; } if(a[i].l>r || a[i].r<l)return; pushDown(i); query(l,r,i<<1); query(l,r,i<<1|1);}int main(){ int n,m; while(~scanf("%d%d",&n,&m) && (n+m)) { build(1,n,1); int x,y,z; char c; for(int i=0;i<m;i++) { getchar(); c=getchar(); if(c=='P') { scanf("%d%d%d",&x,&y,&z); update(x,y,z,1); } else { mem(ans,0); scanf("%d%d",&x,&y); query(x,y,1); int t=0; for(int i=1;i<31;i++) { if(ans[i]) { if(t==0) { printf("%d",i); t++; } else { printf(" %d",i); } } } puts(""); } } } return 0;}
0 0
- hdu 5023 线段树
- hdu 5023 线段树
- hdu 5023 线段树
- hdu 5023 线段树
- hdu 5023(线段树)
- hdu 5023 线段树
- HDU 5023 线段树
- HDU 5023 线段树+状压
- hdu 5023 线段树+状压
- hdu 5023 (线段树 )
- hdu 1754 线段树
- hdu 1754 线段树
- HDU 1698 线段树
- hdu 1754 线段树
- 【线段树】hdu 1754
- hdu-1166 线段树
- HDU-2688 线段树
- HDU 4027 线段树
- 百度地图接口调用
- Maven实战之antrun插件
- 攻下隔壁女生路由器后,我都做了些什么
- Implement Queue using Stacks
- 史蒂夫-乔布斯十大励志名言
- hdu 5023 线段树
- MyEclipse中的页面字体设置
- JDBC与DBUtils的区别
- JAVA web容器
- U3D UGUI学习3 - Text
- javascript 计算两个日期间差的天数
- Python教程
- iOS appdelegate几个方法介绍
- 使用squid做出口防火墙