您的位置:首页 > 其它

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
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;
}





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