hdu 5023 线段树
来源:互联网 发布:淘宝重复铺货怎么办 编辑:程序博客网 时间:2024/05/22 12:48
<h1 style="color: rgb(26, 92, 200); text-align: center; font-family: 'Times New Roman';">A Corrupt Mayor's Performance Art</h1><span style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;"><strong><span style="font-family: Arial; font-size: 12px; color: green;">Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 100000/100000 K (Java/Others)Total Submission(s): 752 Accepted Submission(s): 284</span></strong></span><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">Problem Description</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;">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?</div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url(http://acm.hdu.edu.cn/images/panel-bottom.png) 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">Input</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;">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 ba 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.</div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url(http://acm.hdu.edu.cn/images/panel-bottom.png) 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">Output</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;">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.</div><div class="panel_bottom" style="height: auto; margin: 0px; font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center; background: url(http://acm.hdu.edu.cn/images/panel-bottom.png) 0% 0% no-repeat;"> </div><br style="font-family: 'Times New Roman';font-size:14px; text-align: -webkit-center;" /><div class="panel_title" align="left" style="height: 38px; padding: 0px 14px; color: rgb(124, 169, 237); font-size: 18px; font-family: Arial; font-weight: bold; background: url(http://acm.hdu.edu.cn/images/panel-title.png) 0% 100% no-repeat transparent;">Sample Input</div><div class="panel_content" style="height: auto; margin: 0px; padding: 0px 20px; font-size: 14px; font-family: 'Times New Roman'; background: url(http://acm.hdu.edu.cn/images/panel-content.png) repeat-y;"><pre style="word-wrap: break-word; white-space: pre-wrap; margin-top: 0px; margin-bottom: 0px;"><div style="font-family: 'Courier New', Courier, monospace;">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</div>
Sample Output
43 44 744 7 8
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<set>#define MAX 1000005#define lson s,mid,n<<1#define rson mid+1,e,n<<1|1using namespace std;int tree[MAX<<2];int add[MAX<<2];set<int>q;void pushdown(int n){ if(add[n]!=0) { add[n<<1]=add[n<<1|1]=1; add[n]=0; tree[n<<1]=tree[n<<1|1]=tree[n]; }}void build(int s,int e,int n){ tree[n]=2; add[n]=0; if(s==e) return ; int mid=(s+e)>>1; build(lson); build(rson);}void modify(int a,int b,int c,int s,int e,int n ){ if(a<=s&&e<=b) { tree[n]=c; add[n]=1; return ; } pushdown(n); ///向下更新 lazy思想 否则会超时 int mid=(s+e)>>1; if(b<=mid) modify(a,b,c,lson); else if(mid<a) modify(a,b,c,rson); else { modify(a,mid,c,lson); modify(mid+1,b,c,rson); }}void solve(int a,int b,int s,int e,int n){ if((a<=s&&e<=b&&add[n]!=0)||(s==e)) { q.insert(tree[n]); return ; } pushdown(n); int mid=(s+e)>>1; if(b<=mid) solve(a,b,lson); else if(mid<a) solve(a,b,rson); else { solve(a,mid,lson); solve(mid+1,b,rson); }}int main(){ int n,m,b,c,d; char a[4]; while(scanf("%d%d",&n,&m)!=EOF,n||m) { build(1,n,1); while(m--) { scanf("%s",a); if(a[0]=='P') { scanf("%d%d%d",&b,&c,&d); modify(b,c,d,1,n,1); } else if(a[0]=='Q') { scanf("%d%d",&b,&c); q.clear(); solve(b,c,1,n,1); set<int>::iterator w; for(w=q.begin(); w!=q.end(); w++) { if(w!=q.begin()) printf(" "); printf("%d",*w); } printf("\n"); } } } return 0;}/*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*/
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 线段树
- HDU - 5025 Saving Tang Monk
- 今日第一题 uva11384
- Vector 和 ArrayList去重
- cocos2d-X之精灵角色(CCSprite)
- Spring中AOP
- hdu 5023 线段树
- http://baike.baidu.com/view/79315.htm?fr=aladdin#2 求最大公倍数
- 网络赛 Saving Tang Monk(HDU 5025)
- 同步和异步
- 如何阅读文献
- 动态规划(dynamic program)&& 最长公共子序列(LCS)
- VC6.0读取BMP图像信息到BYTE数组
- java学习笔记16
- 分享一下我使用的一些工具(未完成)