Knight Tournament

来源:互联网 发布:mysql base64 解码 编辑:程序博客网 时间:2024/05/16 08:55
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.

Example
Input

4 31 2 11 3 31 4 4Output3 1 4 0 Input8 43 5 43 7 62 8 81 8 1Output0 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.

说白了就是找父节点,然而还必须要有路径压缩,要不然会超时

#include<stdio.h>#include<string.h>const int Max=3e5+10;int pre[Max],ans[Max];int next[Max];//这个题就比较坑啊,一个是每个点只变一次,因为它只能被一个人打败么//所以一开始我想直接不用路径压缩,就可以了,这样就不会出现一个点变化两次的现象了,直接输出pre[]数组就好了//可是这样竟然会超时,也就是说我们必须进行路径压缩,也就是说像以pre[]数组做答案,根本就是不可能的了//于是我们开辟一个新的数组,这样的话,对这个点进行判断,只要这个点的pre[i]=i,就是说明这个人还没有被别人打败过//我们就可以记录下他的值,如果它已经被别人打败过了,那我们不要再管这个人了,毕竟我们不知道他是不是被压缩了路径//他保留的根本就不是第一个打败他的人了//这个题就是找父节点int find_ancestor(int x){    return pre[x]==x ? x:pre[x]=find_ancestor(pre[x]);}int main(){   int n,m;   scanf("%d %d",&n,&m);   for(int i=1;i<=n;i++)   {       pre[i]=i;       next[i]=i+1;   }   int l,r,winner;   for(int i=1;i<=m;i++)   {       scanf("%d %d %d",&l,&r,&winner);       for(int j=l;j<=r;)       {           int fx=find_ancestor(j);           //printf("j:%d root:%d&&\n",j,fx);           if(pre[j]==j)             ans[j]=winner;        //我们可以进行跳跃,但是我们怎么去修改中间的那个winner ,我们就可以直接找到这段区间的赢家        //就可以利用找祖先,直接找到,将他的值改变就好了           ans[fx]=winner;           pre[fx]=winner;           //这个改变next[j],和找到下一个要走的点自己写了好久           //就是先过去,然后改变当前节点的next[],还是说弄一个双胞胎           int temp=j;           j=next[j];//一个直接找下一个要走的点           next[temp]=r+1;//一个改变next[]数组       }   }   for(int i=1;i<=n;i++)   {       if(i==winner)        printf("0 ");       else        printf("%d ",ans[i]);   }   return 0;}
原创粉丝点击