CodeForces 356A_(set应用,线段树)
2016-09-04 20:42
411 查看
A.KnightTournament
timelimitpertest
3seconds
memorylimitpertest
256megabytes
input
standardinput
output
standardoutput
Hooray!BerlII,thekingofBerlandismakingaknighttournament.Thekinghasalreadysentthemessagetoallknightsinthekingdomandtheyinturnagreedtoparticipateinthisgrandevent.
Asforyou,you'rejustasimplepeasant.There'snosurprisethatyousleptinthismorningandwerelateforthetournament(itwasaweekend,afterall).Nowyouarereallycuriousabouttheresultsofthetournament.ThistimethetournamentinBerlandwentasfollows:
Therearenknightsparticipatinginthetournament.Eachknightwasassignedhisuniquenumber—anintegerfrom1ton.
Thetournamentconsistedofmfights,inthei-thfighttheknightsthatwerestillinthegamewithnumbersatleastliandatmostrihavefoughtfortherighttocontinuetakingpartinthetournament.
Afterthei-thfightamongallparticipantsofthefightonlyoneknightwon—theknightnumberxi,hecontinuedparticipatinginthetournament.Otherknightsleftthetournament.
Thewinnerofthelast(them-th)fight(theknightnumberxm)becamethewinnerofthetournament.
Youfishedoutalltheinformationaboutthefightsfromyourfriends.Nowforeachknightyouwanttoknowthenameoftheknighthewasconqueredby.Wethinkthattheknightnumberbwasconqueredbytheknightnumbera,iftherewasafightwithbothoftheseknightspresentandthewinnerwastheknightnumbera.
Writethecodethatcalculatesforeachknight,thenameoftheknightthatbeathim.
Input
Thefirstlinecontainstwointegersn,m(2 ≤ n ≤ 3·105;1 ≤ m ≤ 3·105)—thenumberofknightsandthenumberoffights.Eachofthefollowingmlinescontainsthreeintegersli, ri, xi(1 ≤ li < ri ≤ n;li ≤ xi ≤ ri)—thedescriptionofthei-thfight.
Itisguaranteedthattheinputiscorrectandmatchestheproblemstatement.Itisguaranteedthatatleasttwoknightstookpartineachbattle.
Output
Printnintegers.Ifthei-thknightlost,thenthei-thnumbershouldequalthenumberoftheknightthatbeattheknightnumberi.Ifthei-thknightisthewinner,thenthei-thnumbermustequal0.
Examples
input
output
input
output
timelimitpertest
3seconds
memorylimitpertest
256megabytes
input
standardinput
output
standardoutput
Hooray!BerlII,thekingofBerlandismakingaknighttournament.Thekinghasalreadysentthemessagetoallknightsinthekingdomandtheyinturnagreedtoparticipateinthisgrandevent.
Asforyou,you'rejustasimplepeasant.There'snosurprisethatyousleptinthismorningandwerelateforthetournament(itwasaweekend,afterall).Nowyouarereallycuriousabouttheresultsofthetournament.ThistimethetournamentinBerlandwentasfollows:
Therearenknightsparticipatinginthetournament.Eachknightwasassignedhisuniquenumber—anintegerfrom1ton.
Thetournamentconsistedofmfights,inthei-thfighttheknightsthatwerestillinthegamewithnumbersatleastliandatmostrihavefoughtfortherighttocontinuetakingpartinthetournament.
Afterthei-thfightamongallparticipantsofthefightonlyoneknightwon—theknightnumberxi,hecontinuedparticipatinginthetournament.Otherknightsleftthetournament.
Thewinnerofthelast(them-th)fight(theknightnumberxm)becamethewinnerofthetournament.
Youfishedoutalltheinformationaboutthefightsfromyourfriends.Nowforeachknightyouwanttoknowthenameoftheknighthewasconqueredby.Wethinkthattheknightnumberbwasconqueredbytheknightnumbera,iftherewasafightwithbothoftheseknightspresentandthewinnerwastheknightnumbera.
Writethecodethatcalculatesforeachknight,thenameoftheknightthatbeathim.
Input
Thefirstlinecontainstwointegersn,m(2 ≤ n ≤ 3·105;1 ≤ m ≤ 3·105)—thenumberofknightsandthenumberoffights.Eachofthefollowingmlinescontainsthreeintegersli, ri, xi(1 ≤ li < ri ≤ n;li ≤ xi ≤ ri)—thedescriptionofthei-thfight.
Itisguaranteedthattheinputiscorrectandmatchestheproblemstatement.Itisguaranteedthatatleasttwoknightstookpartineachbattle.
Output
Printnintegers.Ifthei-thknightlost,thenthei-thnumbershouldequalthenumberoftheknightthatbeattheknightnumberi.Ifthei-thknightisthewinner,thenthei-thnumbermustequal0.
Examples
input
43 121 133 144
output
3140
input
84 354 376 288 181
output
08464861 n个骑士,m场战斗,知道m场战斗的(l,r,x)(从l到r中未出局的骑士中x胜出),求每个骑士被谁战胜。 1).STL_SET的应用
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> #include<set> usingnamespacestd; #defineMAXN300005 set<int>s; set<int>::iteratorit_l,it,tmp[MAXN]; intans[MAXN]; intmain() { intn,m,l,r,x,num; while(cin>>n>>m) { s.clear(); memset(ans,0,sizeof(ans)); for(inti=1;i<=n;i++) s.insert(i); for(inti=0;i<m;i++) { cin>>l>>r>>x; it_l=s.lower_bound(l); num=0; for(it=it_l;*it<=r&&it!=s.end();it++) { if(*it!=x) { ans[*it]=x; tmp[num++]=it; } } for(intj=0;j<num;j++) s.erase(tmp[j]); } for(inti=1;i<n;i++) cout<<ans[i]<<""; cout<<ans <<endl; } return0; }
2).线段树。最开始用单点更新超时,看题解说是倒着区间更新。
#include<stdio.h> #include<string.h> #include<math.h> #include<iostream> #include<algorithm> usingnamespacestd; #definelsonl,mid,rt<<1 #definersonmid+1,r,rt<<1|1 intn,m; structNode { intlazy; intl,r; intcon; }tree[300005<<2]; voidpushdown(intrt) { if(tree[rt].lazy>0) { tree[rt<<1].lazy=tree[rt<<1|1].lazy=tree[rt].lazy; tree[rt<<1].con=tree[rt<<1|1].con=tree[rt].lazy; tree[rt].lazy=0; } } voidbuild(intl,intr,intrt) { tree[rt].l=l; tree[rt].r=r; tree[rt].lazy=0; tree[rt].con=0; if(l==r) return; intmid=(l+r)/2; build(lson); build(rson); } intquery(intx,intl,intr,intrt) { if(l==r) { returntree[rt].lazy; } pushdown(rt); intmid=(r+l)>>1; if(x>mid) query(x,rson); else query(x,lson); } voidupdate(intL,intR,intl,intr,intrt,intcon) { if(L==l&&R==r) { tree[rt].lazy=con; return; } pushdown(rt); intmid=(l+r)/2; if(L>mid) update(L,R,rson,con); elseif(R<=mid) update(L,R,lson,con); else { update(L,mid,lson,con); update(mid+1,R,rson,con); } } intl[300005],r[300004],con[300005]; intmain() { scanf("%d%d",&n,&m); build(1,n,1); for(inti=0;i<m;i++) scanf("%d%d%d",&l[i],&r[i],&con[i]); for(inti=m-1;i>=0;i--) { if(con[i]>l[i]) update(l[i],con[i]-1,1,n,1,con[i]); if(con[i]<r[i]) update(con[i]+1,r[i],1,n,1,con[i]); } for(inti=1;i<=n;i++) { printf("%d",query(i,1,n,1)); if(i==n) printf("\n"); else printf(""); } return0; }
相关文章推荐
- Tomcat7-Connector(连接器)学习
- 反射的基本应用
- 华为上机笔试 难题
- tensorflow的OS X构建机器学习环境
- Java流
- C#设计模式学习小结之四 状态模式
- linux环境下如何快速升级你的Node.js
- C# 通过ConfigurationManager获取配置信息
- 如何做论文报告
- c#之MVC模式
- Linux下core dump调试--gdb
- 阿里前端编程题1
- CSS Sprites技术的优缺点
- 用两张图片实现的2D动画效果(翻转)
- Python_正则表达式
- 在kali linux之下 下载并解压的文件名呈现乱码 解决方案
- time.h
- Activity和Intent机制学习笔记
- 【Effective Java】2、构造参数过多的时候
- SCP 命令