1. 程式人生 > >騎士之戰(指標)

騎士之戰(指標)

騎士之戰

時間限制: 1 Sec  記憶體限制: 128 MB

題目描述

有n位騎士想要通過淘汰賽決出他們當中最強大的一個。所有的騎士由1到n編號,他們總共進行了m場比賽,在第i場比賽中,所有編號在li到ri之間且尚未出局的騎士進行了一場比賽,決出了獲勝者xi,其他參加比賽的騎士就出局了;我們稱這些騎士被騎士xi打敗了。m場比賽過後,只有一位騎士還沒有出局,他就是最終的獲勝者,我們希望知道其他所有騎士分別是被誰打敗了。

 

輸入

第一行包含兩個正整數n和m,表示騎士和比賽的數量。
接下來m行,每行三個數l,r和x,表示參與比賽的騎士編號範圍,以及獲勝者的編號。

 

 

輸出

輸出n個由空格分隔的整數,第i個表示打敗騎士i的騎士的編號。如果騎士i是獲勝者,就在對應位置輸出0。

 

樣例輸入

複製樣例資料

5 3
2 4 3
1 3 1
1 5 5

樣例輸出

5 3 1 3 0

提示

第一場比賽中,騎士3打敗了騎士2和4。 第二場比賽中,騎士1打敗了騎士3。 第三場比賽中,騎士5打敗了騎士1。 最終的勝利者是騎士5。

對於30%的資料,n ≤ 1000;
對於100%的資料,1 ≤ m < n ≤ 3×106, 1 ≤ l ≤ x ≤ r ≤ n,保證l到r之間至少有兩位尚未出局的騎士,且騎士x一定尚未出局。

用指標複雜度為O(n);

/**/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cctype>
#include <iostream>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
#include <string>
#include <stack>
#include <queue>

typedef long long LL;
using namespace std;

int n, m;
int ans[3000005];

struct node
{
	int l, r;
}a[3000005];

int main()
{
	//freopen("in.txt", "r", stdin);
	//freopen("out.txt", "w", stdout);

	scanf("%d %d",&n, &m);
	for (int i = 1; i <= n; i++) a[i].l = i - 1, a[i].r = i + 1;
	int l, r, x;
	for (int i = 1; i <= m; i++){
		scanf("%d %d %d", &l, &r, &x);
		int cnt = a[x].l;
		while(cnt >= l){
			ans[cnt] = x;
			a[a[cnt].l].r = a[cnt].r;
			a[a[cnt].r].l = a[cnt].l;
			cnt = a[cnt].l;
		}
		cnt = a[x].r;
		while(cnt <= r){
			ans[cnt] = x;
			a[a[cnt].l].r = a[cnt].r;
			a[a[cnt].r].l = a[cnt].l;
			cnt = a[cnt].r;
		}
	}
	printf("%d", ans[1]);
	for (int i = 2; i <= n; i++) printf(" %d", ans[i]);
	printf("\n");

	return 0;
}
/**/