您的位置:首页 > 其它

hdu--5023 A Corrupt Mayor's Performance Art(线段树+区间更新+位运算)

2016-11-05 15:50 435 查看
Description

Corruptgovernorsalwaysfindwaystogetdirtymoney.Paintsomething,thenselltheworthlesspaintingatahighpricetosomeonewhowantstobribehim/heronanauction,thisseemedasafewayformayorXtomakemoney.

BecausealotofpeoplepraisedmayorX'spainting(ofcourse,Xwasamayor),mayorXbelievedmoreandmorethathewasaverytalentedpainter.SoonmayorXwasnotsatisfiedwithonlymakingmoney.Hewantedtobeafamouspainter.Sohejoinedthelocalpaintingassociates.Otherpaintershadtoelecthimasthechairmanoftheassociates.Thenhispaintingsoldatbetterprice.

ThelocalmiddleschoolfromwhichmayorXgraduated,wantedtobeatmayorX'shorsefart(InChineseEnglish,beatingone'shorsefartmeansflatteringonehard).Theybuiltawall,andinvitedmayorXtopaintonit.MayorXwasveryhappy.Buthereallyhadnoideaaboutwhattopaintbecausehecouldonlypaintveryabstractpaintingswhichnobodyreallyunderstand.MayorX'ssecretarysuggestedthathecouldmakethisthingnotonlyapainting,butalsoaperformanceartwork.

Thiswasthesecretary'sidea:

ThewallwasdividedintoNsegmentsandthewidthofeachsegmentwasonecun(cunisaChineselengthunit).Allsegmentswerenumberedfrom1toN,fromlefttoright.Therewere30kindsofcolorsmayorXcouldusetopaintthewall.Theynamedthosecolorsascolor1,color2....color30.Thewall'soriginalcolorwascolor2.EverytimemayorXwouldpaintsomeconsecutivesegmentswithacertainkindofcolor,andhedidthisformanytimes.Tryingtomakehisperformanceartfancy,mayorXdeclaredthatatanymoment,ifsomeoneaskedhowmanykindofcolorswerethereonanyconsecutivesegments,hecouldgivethenumberimmediatelywithoutcounting.

ButmayorXdidn'tknowhowtogivetherightanswer.Yourfriend,Mr.Wwasansecretofficerofanti-corruptionbureau,hehelpedmayorXonthisproblemandgainedhistrust.DoyouknowhowMr.Qdidthis?

Input

Thereareseveraltestcases.

Foreachtestcase:

Thefirstlinecontainstwointegers,NandM,meaningthatthewallisdividedintoNsegmentsandthereareMoperations(0<N<=1,000,000;0<M<=100,000)

ThenMlinesfollow,eachrepresentinganoperation.Therearetwokindsofoperations,asdescribedbelow:

1)Pabc
a,bandcareintegers.ThisoperationmeansthatmayorXpaintedallsegmentsfromsegmentatosegmentbwithcolorc(0<a<=b<=N,0<c<=30).

2)Qab
aandbareintegers.Thisisaqueryoperation.Itmeansthatsomeoneaskedthathowmanykindsofcolorsweretherefromsegmentatosegmentb(0<a<=b<=N).

Pleasenotethattheoperationsaregivenintimesequence.

TheinputendswithM=0andN=0.

Output

Foreachqueryoperation,printallkindsofcoloronthequeriedsegments.Forcolor1,print1,forcolor2,print2...etc.Andthiscolorsequencemustbeinascendingorder.

SampleInput

510
P123
P234
Q23
Q13
P354
P127
Q13
Q34
P558
Q15
00


SampleOutput

4
34
47
4
478
题意:给出点[1,n]和更新查询次数m,P是对区间[a,b]上的点染色成c,Q是查询区间[a,b]中有哪些颜色,从小到大输出。
思路:建树,用ins对区间进行更新并进行懒惰标记,然后用sea进行查询。由于颜色有30种,所以可以用int的二进制进行储存。
注意:当建树较小时会TLE而不是MLE或RE,让我纠结了好久
AC代码:


1#include<cstdio>
2#include<cstring>
3#include<iostream>
4#include<algorithm>
5#include<queue>
6#include<vector>
7#include<map>
8#definelllonglong
9usingnamespacestd;
10constintmaxn=1000000+10;
11structnote
12{
13intl,r,ans;
14boolt;
15}a[maxn<<2];//注意:maxn<<2
16intbuild(intl,intr,intk)
17{
18a[k].l=l,a[k].r=r,a[k].ans=1<<2,a[k].t=0;
19if(l==r)
20{
21return0;
22}
23intmid=(l+r)>>1;
24build(l,mid,k<<1);
25build(mid+1,r,k<<1|1);
26return0;
27}
28intpushdown(intk)
29{
30a[k<<1].ans=a[k<<1|1].ans=a[k].ans;
31a[k].t=0;
32a[k<<1].t=a[k<<1|1].t=1;
33return0;
34}
35intins(intl,intr,intk,intd)
36{
37if(a[k].l==l&&a[k].r==r)
38{
39a[k].t=1;
40a[k].ans=1<<d;
41return0;
42}
43if(a[k].t)pushdown(k);
44intmid=(a[k].l+a[k].r)>>1;
45if(a[k<<1].r>=r)ins(l,r,k<<1,d);
46elseif(a[k<<1|1].l<=l)ins(l,r,k<<1|1,d);
47else
48{
49ins(l,mid,k<<1,d);
50ins(mid+1,r,k<<1|1,d);
51}
52a[k].ans=a[k<<1].ans|a[k<<1|1].ans;
53return0;
54}
55intsea(intl,intr,intk)
56{
57if(l==a[k].l&&r==a[k].r)
58{
59returna[k].ans;
60}
61if(a[k].t)pushdown(k);
62intmid=(a[k].l+a[k].r)>>1,ans;
63if(a[k<<1].r>=r)ans=sea(l,r,k<<1);
64elseif(a[k<<1|1].l<=l)ans=sea(l,r,k<<1|1);
65else
66{
67ans=sea(l,mid,k<<1)|sea(mid+1,r,k<<1|1);
68}
69returnans;
70}
71intmain()
72{
73intn,m,a,b,c;
74chars[10];
75while(~scanf("%d%d",&n,&m))
76{
77if(n==0&&m==0)
78break;
79build(1,n,1);
80while(m--)
81{
82scanf("%s",s);
83if(s[0]=='P')
84{
85scanf("%d%d%d",&a,&b,&c);
86ins(a,b,1,c);
87}
88else
89{
90intans,i;
91scanf("%d%d",&a,&b);
92ans=sea(a,b,1);
93for(i=1;i<=30;i++)//注意:i<=30
94if((ans>>i)&1)
95{
96printf("%d",i);
97break;
98}
99for(i++;i<=30;i++)
100if((ans>>i)&1)
101printf("%d",i);
102printf("\n");
103}
104}
105}
106return0;
107}


ViewCode



                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐