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场比赛.每场比 ...
随机推荐
- spring mvc 与 jquery ajax
在 Spring mvc3中,响应.接受 JSON都十分方便. 使用注解@ResponseBody可以将结果(一个包含字符串和JavaBean的Map),转换成JSON. 使用 @RequestBod ...
- 基于visual Studio2013解决C语言竞赛题之1010计算
题目 解决代码及点评 /************************************************************************/ ...
- poj1830
高斯消元求秩,难在构造方程. ; ; i < equ; i++) { ; j < var + ; j++) { cout & ...
- Java EE登陆界面生成随机数防止恶意注册或者登录
package cn.com; import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.a ...
- 项目.c文件和.h文件关系
"如有不正确之处,请指出,谢谢" --Mood 对于一个项目,我们应该要很好的处理众多的.c和.h文件. 1.通过头文件调用库功能:#inclu ...
- ubuntu12.04 安装 opencv 2.4.8(非源代码编译)
一:安装所须要的各种库,如GTK3.xx 安装GCC:sudo apt-get install build-essential 安装CMakesudo apt-get install cmake su ...
- [置顶] ARM-Linux下WEB服务器Boa的移植、配置和运行测试
Linux下使用的轻量级WEB服务器主要有:lighttpd.thttpd.shttpd和boa等等,而Boa是使用最为广泛的轻量级WEB服务器之一(当然,阿帕奇是世界使用排名第一的Web服务器软件) ...
- BZOJ 3110([Zjoi2013]K大数查询-区间第k大[段修改,在线]-树状数组套函数式线段树)
3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec Memory Limit: 512 MB Submit: 418 Solved: 235 [ Submit][ ...
- C语言sendto()函数-经socket传送数据以及recvfrom函数《转》
相关函数:send, sendmsg, recv, recvfrom, socket 头文件:#include <sys/types.h> #include <sys/socke ...
- Java使用HttpURLConnection上传文件
从普通Web页面上传文件非常easy.仅仅须要在form标签叫上enctype="multipart/form-data"就可以,剩余工作便都交给浏览器去完毕数据收集并发送Http ...