CodeForce 356A Knight Tournament(set应用)
3 seconds
256 megabytes
standard input
standard output
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.
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.
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.
4 3
1 2 1
1 3 3
1 4 4
3 1 4 0
8 4
3 5 4
3 7 6
2 8 8
1 8 1
0 8 4 6 4 8 6 1
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<iostream>
#include<set>
#include<cstring>
using namespace std;
const int MAXN = 300010;
int ans[MAXN];
set<int> s;
set<int>::iterator it, it_l, tmp[MAXN];
int main()
{
int n, m, l, r, x, num;
while(cin >> n >> m) {
s.clear();
memset(ans, 0, sizeof(ans));
for(int i = 1; i <= n; i++)
s.insert(i);
for(int i = 0; i < m; i++) {
cin >> l >> r >> x;
it_l = s.lower_bound(l); //寻找開始删除的位置
num = 0;
for(it = it_l; *it <= r && it != s.end(); it++) {
if(*it != x) {
ans[*it] = x;
tmp[num++] = it; //不能直接删,由于每删除一个元素,set会自己主动调整内部结构
}
}
for(int j = 0; j < num; j++)
s.erase(tmp[j]);
}
for(int i = 1; i < n; i++)
cout << ans[i] << " ";
cout << ans[n] << endl;
}
return 0;
}
CodeForce 356A Knight Tournament(set应用)的更多相关文章
- CodeForces - 356A Knight Tournament
http://codeforces.com/problemset/problem/356/A 首先理解题意 每次给出l 和r 在l - r之间还有资格的选手中得出一个胜者 暴力思路: 首先维护还有资 ...
- Knight Tournament 合并区间
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...
- Knight Tournament (set)
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...
- CodeForces - 357C Knight Tournament 伪并查集(区间合并)
Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...
- D - Knight Tournament(set)
Problem description Hooray! Berl II, the king of Berland is making a knight tournament. The king has ...
- codeforces 357C Knight Tournament(set)
Description Hooray! Berl II, the king of Berland is making a knight tournament. The king has already ...
- 【Codeforces 356A】Knight Tournament
[链接] 我是链接,点我呀:) [题意] n个人矩形m场比赛 每场比赛由编号为li~ri且之前没有被淘汰的人进行. 已知第i场的winner是xi winner会把这一场其他所有的人都淘汰. 问你n个 ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament(STL)
脑子又卡了...来一发set的,STL真心不熟. #include <stdio.h> #include <string.h> #include <iostream> ...
- Knight Tournament
Codeforces Round #207 (Div. 1) A:http://codeforces.com/problemset/problem/356/A 题意:给你n匹马,然后有m场比赛.每场比 ...
随机推荐
- jquery Deferred使用经验
这周做了个小活动(http://aoqi.100bt.com/zt-2016duanzi/index.html),刚开始时候没看好需求,逻辑都写一块了 最后各种坑要填补,从中也获取了些经验和教训,下面 ...
- C#多线程的死锁演示
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 查询mysql哪些表正在被锁状态
1.查进程,主要是查找被锁表的那个进程的ID SHOW PROCESSLIST; 2.kill掉锁表的进程ID KILL 10866;//后面的数字即时进程的ID
- perl中 wx返回的json需要encode_utf8($d);
$count is 9 now not support message 51 Wide character in print at /root/scanwx/lib/synccheck.pm line ...
- 关于mysql5.6.13的一个疑问
现在在做一个系统 使用了这么一个查询 select a.id,a.fdate,a.fbillno,e.fname as fwarehousename,a.fnote,c.fname as fsuppl ...
- sql: PL/SQL proc
A PL/SQL block has the following structure: [DECLARE declaration_statements ] BEGIN executable_state ...
- 浮点数在计算机内存中的表示(IEEE 754规定1位是符号位,8位是指数,剩下的23位为有效数字)
本文转载自:阮一峰的博客,http://www.ruanyifeng.com/blog/2010/06/ieee_floating-point_representation.html 张玉彬的博客 h ...
- 硬盘被误格式化或Ghost还原后的数据恢复
硬盘格式化(Ghost还原)后的数据恢复 ---diskgenius使用之数据恢复 问题引出:计算机中病毒后用Ghost版本的winxp安装,由于安装途中选择了把映像安装到硬盘而不是分区,安装好后只剩 ...
- 性能测试之LoadRunner11 破解
1. 下载破解文件lm70.dll和mlr5lprg.dll lm70.dll文件,覆盖x:\Program Files\Mercury\LoadRunner\bin下文件即可. ml ...
- STL algorithm算法is_permutation(27)
is_permutation原型: std::is_permutation equality (1) template <class ForwardIterator1, class Forwar ...