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 nm (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?≤?nli?≤?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

Input

4 31 2 11 3 31 4 4

Output

3 1 4 0 

Input

8 43 5 43 7 62 8 81 8 1

Output

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?"\n":" ");
21     return 0;
22 }

原文地址:https://www.cnblogs.com/acgoto/p/9157528.html

时间: 2024-10-13 00:28:23

D - Knight Tournament的相关文章

Knight Tournament 伪并查集(区间合并)

Knight Tournament 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 pea

codeforces 357C Knight Tournament(set)

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.

CodeForces - 356A Knight Tournament

http://codeforces.com/problemset/problem/356/A 首先理解题意 每次给出l 和r  在l - r之间还有资格的选手中得出一个胜者 暴力思路: 首先维护还有资格的选手的集合 用一个数组 表示 这个选手被谁击败 每次遍历 l - r 然后把不是胜者 且 还在集合中的选手踢出 并更新这个选手的数组值 最终 输出这个数组即可 这样会TLE 1. 如果用数组维护这个集合的话 每次遍历都是这样就是O(n^2) -->> 所以用set维护这个集合 2.使用set后

Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)

题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所击败了(l<=x<=r),被击败的人立马退出游戏让你最后输出每个人是被谁击败的,最后那个胜利者没被 人击败就输出0 思路:他的每次修改的是一个区间的被击败的人,他而且只会记录第一次那个被击败的人,用线段树堕落标记的话他会记录最后一次的,所以我们倒着来修改, 然后因为那个区间里面还包含了自己,在线段

区间跳跃问题

区间跳跃问题 有时我们会碰到需要跳着区间处理的问题,这个时候目前我接触到的有两种思路: 一是利用\(set\)来处理,因为\(set\)可以从序列中删除元素,利用好一个或者多个\(set\),就可以完美地解决问题. 二是利用并查集来处理,如何实现跳点呢,看跳的方向是单向还是双向,决定建立几个并查集,维护即可. 下面拿两个例题来解释一下. CF 356A Knight Tournament 这个题大意就是有\(n\)个骑士,每次给出一个区间和一个\(x\),区间内的所有骑士如何还没有被打败,就会被

URAL 1551. Sumo Tournament(数学啊 )

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1551 1551. Sumo Tournament Time limit: 1.0 second Memory limit: 64 MB A sumo tournament is held in Tokyo, in which 2N sportsmen take part. In each encounter there is a winner, and the loser drops out

POJ 1915 Knight Moves

Knight Moves Description Background Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fast. Can you beat him? The Problem Your task is to write a program to calculate the mini

POJ 1915 Knight Moves [BFS]

Knight Moves Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 26844   Accepted: 12663 Description Background  Mr Somurolov, fabulous chess-gamer indeed, asserts that no one else but him can move knights from one position to another so fas

Rock-Paper-Scissors Tournament[HDU1148]

Rock-Paper-Scissors TournamentTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2178 Accepted Submission(s): 693 Problem DescriptionRock-Paper-Scissors is game for two players, A and B, who each choo