D - Knight Tournament(set)
Problem description
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(表示n个骑士,编号为1~n)和m(m行比赛数据),其中l,r,x表示[l,r]中除x编号外都被x打败,最后第m行数据的x是最终的胜利者,其"被打败的编号"为0,要求输出每个骑士(1~n)被打败的骑士编号。注:输入数据已经保证每个骑士都参加战斗。显然题目给的数据范围很大,两重循环就TLE。怎么优化呢?做法:将编号1~n仍在set容器中,采用lower_bound()找到容器中l元素(迭代器)的位置,然后在区间[l,r]中除x外将被打败的骑士编号标记起来(即s[i]=x,s数组中元素初始值要全部清0),再删除容器中已被打败的骑士编号,最后输出每个骑士被打败的骑士编号即可。
AC代码(1871ms):
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5+;
int n,m,l,r,x,cnt,s[maxn];
set<int> st;
set<int>::iterator it,its,tmp[maxn];
int main(){
cin>>n>>m;
for(int i=;i<=n;++i)st.insert(i);//将编号1~n全都扔到set容器中
memset(s,,sizeof(s));//s数组存放下标i被编号x打败,初始值注意清0
while(m--){
cin>>l>>r>>x;cnt=;
its=st.lower_bound(l);//找到不小于l的迭代器的位置
for(it=its;it!=st.end()&&(*it<=r);++it)//[l,r]区间中除x外都已被x打败
if(*it!=x){s[*it]=x;tmp[cnt++]=it;}
for(int i=;i<cnt;++i)//删除已被打败的编号
st.erase(tmp[i]);
}
for(int i=;i<=n;++i)
cout<<s[i]<<(i==n?"\n":" ");
return ;
}
D - Knight Tournament(set)的更多相关文章
- CodeForce 356A Knight Tournament(set应用)
Knight Tournament time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
- HDU 1372 Knight Moves (bfs)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1372 Knight Moves Time Limit: 2000/1000 MS (Java/Othe ...
- Knight Tournament (set)
Hooray! Berl II, the king of Berland is making a knight tournament. The king has already sent the me ...
- Codeforces Round #207 (Div. 1) A. Knight Tournament (线段树离线)
题目:http://codeforces.com/problemset/problem/356/A 题意:首先给你n,m,代表有n个人还有m次描述,下面m行,每行l,r,x,代表l到r这个区间都被x所 ...
- poj2243 Knight Moves(BFS)
题目链接 http://poj.org/problem?id=2243 题意 输入8*8国际象棋棋盘上的两颗棋子(a~h表示列,1~8表示行),求马从一颗棋子跳到另一颗棋子需要的最短路径. 思路 使用 ...
- ZOJ 1091 (HDU 1372) Knight Moves(BFS)
Knight Moves Time Limit: 2 Seconds Memory Limit: 65536 KB A friend of you is doing research on ...
- HDU1372 Knight Moves(BFS) 2016-07-24 14:50 69人阅读 评论(0) 收藏
Knight Moves Problem Description A friend of you is doing research on the Traveling Knight Problem ( ...
- 【UVa】439 Knight Moves(dfs)
题目 题目 分析 没有估价函数的IDA...... 代码 #include <cstdio> #include <cstring> #include <a ...
- 山东省第七届省赛 D题:Swiss-system tournament(归并排序)
Description A Swiss-system tournament is a tournament which uses a non-elimination format. The first ...
随机推荐
- 编译Caffe-Win错误集锦
Caffe在Windows下编译还是遇到不少麻烦的... 1.visual studio 2013 error C2371: 'int8_t' : redefinition; 引入的unistd.h文 ...
- Everything is a file
"Everything is a file" describes one of the defining features of Unix, and its derivatives ...
- 团体程序设计天梯赛-练习集-L1-046. 整除光棍
L1-046. 整除光棍 这里所谓的“光棍”,并不是指单身汪啦~ 说的是全部由1组成的数字,比如1.11.111.1111等.传说任何一个光棍都能被一个不以5结尾的奇数整除.比如,111111就可以被 ...
- 雪花算法生成全局唯一ID
系统中某些场景少不了全局唯一ID的使用,来保证数据的唯一性.除了通过数据库自带的自增id来保证 id 的唯一性,通常为了保证的数据的可移植性会选择通过程序生成全局唯一 id.百度了不少php相关的生成 ...
- Linux:只复制文件权限和所有者
在Linux上,怎么把一个文件的<权限和所有者>赋给另一个文件呢? chown命令可以设置所有者和用户组:chmod可以设置文件的权限.这两个命令都支持使用另一个文件做为引用(RFILE) ...
- js中的数组遍历
js中的数组遍历是项目中经常用到的,在这里将几种方法做个对比. ! for循环:使用评率最高,也是最基本的一种遍历方式. let arr = ['a','b','c','d','e']; for (l ...
- Lua的函数的定义、math数学函数、lua字符串的处理、lua支持的字符串类、模式串中的特殊字符_学习笔记04
Lua的函数的定义.math数学函数 定义函数 function [function name] (param1,param2) [function code] --定义一个函数用来求的两个数字的和 ...
- swift-教你如何实现导航上的UISearchController动画效果。
这个代码片段是我这周我从网上找了各种资料然后经过自己的修改终于弄好了导航的上下动画效果: step1:==>因为这个搜索要有动画效果,所以这个页面必须要有一个导航控制器: //1.自定义创建导航 ...
- python下的线程 进程,以及如何实现并发服务器
在一个CPU(一核)的电脑上, 程序的运行是并发运行的,调度的算法叫时间片轮转法,也叫轮询法 在多CPU(多核)的电脑上,一个CPU跑一个程序,刚程序运行数量小于核心数时,程序是并行的 并发:看上去一 ...
- (9)使用JdbcTemplate【从零开始学Spring Boot】
整体步骤: (1) 在pom.xml加入jdbcTemplate的依赖: (2) 编写DemoDao类,声明为:@Repository,引入JdbcTemplate (3) 编写DemoS ...