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场比赛.每场比 ...
随机推荐
- Tomcat 7最大并发连接数的正确修改方法(转)
几乎所有的中文网页都介绍,要修改Tomcat的默认最大并发连接数,应该进行如下设置(实际上这些步骤是错误的): -------------------------------------------- ...
- 设置MAVEN_OPTS环境变量
运行mvn命令实际上是执行了Java命令,既然是运行Java,那么运行Java命令可用的参数当然也应该在运行mvn命令时可用.这个时候,MAVEN_OPTS环境变量就能派上用场. 通常需要设置MAVE ...
- python手记(27)
#!/usr/bin/env python #-*- coding: utf-8 -*- import cv2 import numpy as np fn="test2.jpg" ...
- vim: 远程登陆服务器时可能出现的: 方向键,回退符不能正常使用
Solution of:远程登陆服务器时可能出现的: 方向键,回退符不能正常使用 远程登陆服务器,可能出现:方向键,回退符不能正常使用. 解决方法:在家目录下的.cshrc中添加set nocompa ...
- 基于visual Studio2013解决C语言竞赛题之1070删除相同节点
题目 解决代码及点评 /************************************************************************/ /* ...
- FindLetter 类——查找文件中特定的字符,每一行开头为某一个字符,则跳过
/*统计除了>之外的行里面CHED四个字母总数*/ #include<fstream> #include<iostream> #include<cstring> ...
- Cocos2dx引擎10-事件派发
本文介绍Cocos2dx事件(以下简称Event)处理机制中的事件分发模块,在Event发生后,进过一系列处理,最后将会分发Event: 1.dispatchEvent& dispatchTo ...
- 从注册流程 分析如何安全退出多个Activity 多种方式(附DEMO)
前言 由于一个同学问到我如何按照一个流程走好之后回到首页,我以前看到过4个解决方案,后来发现有做个记录和总结的必要,就写了这篇博文.(之前看小强也写过一篇,这里通过自身的分析完整的总结一下以下6种 ...
- 在DLL中封装的VCL窗体Tab键响应的问题
在DLL中的子窗体不会响应Tab按键的,这个时候就需要手动去指定Tab键的操作,但是前提是主窗体要向这个窗体发送一个消息,一个Tab键按下的消息.基本顺序是这样的: 1. 主窗体用Hook技术捕获Ta ...
- spring mvc ModelAndView向前台传值
今天在做项目的时候遇到一个问题,把第一个页面保存的id传到第三个页面中去用,原来是在controller层加了一个全局变量控制的,但是后来发现这个变量实现不了我要的功能,于是查了一下,原来ModelA ...