D - Knight Tournament
Posted acgoto
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D - Knight Tournament相关的知识,希望对你有一定的参考价值。
Problem description
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the message to all knights in the kingdom and they in turn agreed to participate in this grand event.
As for you, you‘re just a simple peasant. There‘s no surprise that you slept in this morning and were late for the tournament (it was a weekend, after all). Now you are really curious about the results of the tournament. This time the tournament in Berland went as follows:
- There are n knights participating in the tournament. Each knight was assigned his unique number — an integer from 1 to n.
- The tournament consisted of m fights, in the i-th fight the knights that were still in the game with numbers at least li and at most ri have fought for the right to continue taking part in the tournament.
- After the i-th fight among all participants of the fight only one knight won — the knight number xi, he continued participating in the tournament. Other knights left the tournament.
- The winner of the last (the m-th) fight (the knight number xm) became the winner of the tournament.
You fished out all the information about the fights from your friends. Now for each knight you want to know the name of the knight he was conquered by. We think that the knight number b was conquered by the knight number a, if there was a fight with both of these knights present and the winner was the knight number a.
Write the code that calculates for each knight, the name of the knight that beat him.
Input
The first line contains two integers n, m (2?≤?n?≤?3·105; 1?≤?m?≤?3·105) — the number of knights and the number of fights. Each of the following m lines contains three integers li,?ri,?xi (1?≤?li?<?ri?≤?n; li?≤?xi?≤?ri) — the description of the i-th fight.
It is guaranteed that the input is correct and matches the problem statement. It is guaranteed that at least two knights took part in each battle.
Output
Print n integers. If the i-th knight lost, then the i-th number should equal the number of the knight that beat the knight number i. If the i-th knight is the winner, then the i-th number must equal 0.
Examples
4 3
1 2 1
1 3 3
1 4 4
3 1 4 0
8 4
3 5 4
3 7 6
2 8 8
1 8 1
0 8 4 6 4 8 6 1
Note
Consider the first test case. Knights 1 and 2 fought the first fight and knight 1 won. Knights 1 and 3 fought the second fight and knight 3 won. The last fight was between knights 3 and 4, knight 4 won.
解题思路:题目的意思就是给出n(表示n个骑士,编号为1~n)和m(m行比赛数据),其中l,r,x表示[l,r]中除x编号外都被x打败,最后第m行数据的x是最终的胜利者,其"被打败的编号"为0,要求输出每个骑士(1~n)被打败的骑士编号。注:输入数据已经保证每个骑士都参加战斗。显然题目给的数据范围很大,两重循环就TLE。怎么优化呢?做法:将编号1~n仍在set容器中,采用lower_bound()找到容器中l元素(迭代器)的位置,然后在区间[l,r]中除x外将被打败的骑士编号标记起来(即s[i]=x,s数组中元素初始值要全部清0),再删除容器中已被打败的骑士编号,最后输出每个骑士被打败的骑士编号即可。
AC代码(1871ms):
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int maxn = 3e5+5; 4 int n,m,l,r,x,cnt,s[maxn]; 5 set<int> st; 6 set<int>::iterator it,its,tmp[maxn]; 7 int main(){ 8 cin>>n>>m; 9 for(int i=1;i<=n;++i)st.insert(i);//将编号1~n全都扔到set容器中 10 memset(s,0,sizeof(s));//s数组存放下标i被编号x打败,初始值注意清0 11 while(m--){ 12 cin>>l>>r>>x;cnt=0; 13 its=st.lower_bound(l);//找到不小于l的迭代器的位置 14 for(it=its;it!=st.end()&&(*it<=r);++it)//[l,r]区间中除x外都已被x打败 15 if(*it!=x){s[*it]=x;tmp[cnt++]=it;} 16 for(int i=0;i<cnt;++i)//删除已被打败的编号 17 st.erase(tmp[i]); 18 } 19 for(int i=1;i<=n;++i) 20 cout<<s[i]<<(i==n?" ":" "); 21 return 0; 22 }
以上是关于D - Knight Tournament的主要内容,如果未能解决你的问题,请参考以下文章
CodeForces - 356A Knight Tournament
Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
SDNU 1506.D.Swiss-system tournament(并列排序)
第七届山东省省赛D Swiss-system tournament(归并排序)
Educational Codeforces Round 110 (Rated for Div. 2)- D.Playoff Tournament -建树维护