ACM->CF Knight Tournament

来源:互联网 发布:python树莓派编程pdf 编辑:程序博客网 时间:2024/05/16 10:48
Knight Tournament
Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit Status Practice CodeForces 357C
Appoint description: 

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 rihave 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.

Sample Input

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

/*线段树*/ #include <iostream>#include <cstdio>using namespace std;#define MAXN 3 * 100000 + 10struct Node//定义节点 {int l, r, col;};int ans;class SGtree//定义线段树 {Node node[4 * MAXN];public:void Maketree(int i, int l, int r);void Update(int i, int x, int y, int val);void Query(int i, int x);}tree;void SGtree::Maketree(int i, int l, int r){node[i].l = l;node[i].r = r;if (l == r){node[i].col = 0;return ;}int mid = (l + r) >> 1;Maketree(i * 2, l, mid);Maketree(i * 2 + 1, mid + 1, r);}void SGtree::Update(int i, int x, int y, int val){int l = node[i].l;int r = node[i].r;if (l == x && r == y)//更新 {if (!node[i].col){node[i].col = val;}return ;}if (node[i].col)//以前更新过,直接返回 {return ;}int mid = (l + r) >> 1;if (y <= mid){Update(i * 2, x, y, val);}else if (x > mid){Update(i * 2 + 1, x, y, val);}else{Update(i * 2, x, mid, val);Update(i * 2 + 1, mid + 1, y, val);}}void SGtree::Query(int i, int x){int l = node[i].l;int r = node[i].r;if (node[i].col){ans = node[i].col;//查找最早的更新 }if (l == x && r == x){return ;}int mid = (l + r) >> 1;if (x <= mid){Query(i * 2, x);}else{Query(i * 2 + 1, x);}}void input(){int n, m;int x, y, val;scanf("%d %d", &n, &m);tree.Maketree(1, 1, n);//建树 for (int i = 0; i < m; i++){scanf("%d %d %d", &x, &y, &val);if (x == val)//更新,不更新自己 {tree.Update(1, x + 1, y, val);}else if (y == val){tree.Update(1, x, y - 1, val);}else{tree.Update(1, x, val - 1, val);tree.Update(1, val + 1, y, val);}}for (int i = 1; i <= n; i++)//输出 {ans = 0;tree.Query(1, i);printf("%d", ans);printf(" ");}cout << endl;}int main(){input();return 0;}


原创粉丝点击