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场比赛.每场比 ...
随机推荐
- 《Swift Programming Language 》——Swift中怎样使用继承(Inheritance)
一个类能够继承(inherit)还有一个类的方法(methods),属性(property)和其他特性.当一个类继承其他类时,继承类叫子类(subclass),被继承类叫超类(或父类,supercla ...
- C#拖曳控件加载,bll报错问题
C#拖曳控件加载,bll报错问题,加载时实例如化bll时加上一个判断 if (!(GetService(typeof(IDesignerHost)) != null || Sys ...
- 点击得到QTableWidget中任意位置QPushButton的行列信息
http://www.qtcn.org/bbs/read-htm-tid-51835.html http://www.qtcn.org/bbs/simple/?t43841.html 比如(点击每行最 ...
- PHP开发经验中介(thinkphp3.2使用技巧)
1.在模板中截取字符串 {$vo.create_date|mb_substr=###,0,10,'utf-8'}
- guava之Joiner 和 Splitter(转)
最近在给客户准备一个Guava的分享,所以会陆续的更新关于Guava更多的细节分享.本文将记录Guava中得字符串处理Joiner(连接)和Splitter(分割)处理. Joiner 首先我们来看看 ...
- cocos2d-x游戏开发系列教程-坦克大战游戏加载地图的编写
上节课写了关卡选择场景,那么接下来写关卡内容,先写最基本的地图的加载 我们新建一个场景类,如下所示: class CityScene : public cocos2d::CCLayer { publi ...
- psl/sql本地与远程连接配置
一:下载Oracleclient 下载地址:http://www.oracle.com/technetwork/database/features/instant-client/index-09748 ...
- Linux-C语言中gettimeofday()函数的使用方法(转载)
1.简介: 在C语言中可以使用函数gettimeofday()函数来得到时间.它的精度可以达到微妙 2.函数原型: #include<sys/time.h> int gettimeofda ...
- Solarized Colorscheme for IntelliJ IDEA
Solarized Colorscheme for IntelliJ IDEA Original Solarized color scheme developed by Ethan Schoonove ...
- Computational Geometry Template
顿时觉得神清气爽!! #include <iostream> #include <math.h> #define eps 1e-8 #define zero(x) (((x)& ...