Codeforces-356A(STL运用)

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
input

4 3
1 2 1
1 3 3
1 4 4

output

3 1 4 0 

input

8 4
3 5 4
3 7 6
2 8 8
1 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个骑士,他们将要进行m场比赛来决出谁是最强者,如果输了,就会退场,在最后决出胜负时,输出每个骑士是被谁淘汰的,最终的优胜者请输出0。
解题思路:这个题我们也可以使用线段树来做,我没怎么想(我太菜了,想不粗来,[嗷嗷大哭]),我们可以使用STL中的set容器。
我们先介绍一下set叭。
set的底层是红黑树,并且在set中相同的元素只会存在一个,并且会自动排序。
下面是set的一些函数:
set st: 定义一个存储template类型数据的set容器
insert():将一个元素加入到set中
erase():如果只有一个参数,例如:st.erase(x),删除set中值为x的数据;
如果有两个参数,例如:st.erase(it1,it2),删除set中位于[it1,it2)的值。(PS:it1和it2为迭代器)。
find(x):找到set中值为x所处的位置。
count():返回set中的元素个数。
lower_bound(x):返回第一个大于或等于x的元素的迭代器。
upper_bound(x):返回最大的小于或等于x的元素的迭代器。

然后我们就可以写代码啦。

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <cmath>
#include <set>
using namespace std;
#define int long long
const int maxn=4e5+7;
int n,k,mod;
int a[maxn],len,vis[maxn];
set<int> st;
signed main() {
	int n,m;
	scanf("%lld%lld",&n,&m);
	for(int i=1; i<=n; i++) {
		st.insert(i);
	}
	while(m--) {
		int l,r,x;
		scanf("%lld%lld%lld",&l,&r,&x);
		set<int>::iterator it=st.lower_bound(l);
		set<int>::iterator it1=it;
		for(;it!=st.end()&& *it<=r;it++){
			a[*it]=x;
		}
		st.erase(it1,it);
		a[x]=0;
		st.insert(x);
	}
	for(int i=1; i<=n; i++) {
		if(i!=1)printf(" ");
		printf("%lld",a[i]);
	}
	printf("\n");
	template
}

你可能感兴趣的:(STL)