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 ...
随机推荐
- 存档:Telerik Test Studio的摸索笔记
http://www.51testing.com/?uid-170604-action-spacelist-starttime-1328025600-endtime-1330531200 http:/ ...
- css基础四
过渡属性 下面的表格列出了所有的转换属性: 属性 描述 CSS transition 简写属性,用于在一个属性中设置四个过渡属性. 3 transition-property 规定应用过渡的 CSS ...
- Xilinx 7系列FPGA部分重配置【2】
在之前的“Xilinx 7系列FPGA部分重配置[1]”中已经较为详细地记录了分别在工程模式(Project Mode)和非工程模式(Non-Project Mode)下.使用7系列的Xilinx F ...
- codeforces 466B Wonder Room(思维,暴力)
题目 参考了别人的博客,百度来的博客 #include<iostream> #include<string> #include<stdio.h> #include& ...
- 移动端调试 vConsole
<head> <script src="path/to/vconsole.min.js"></script> <script> va ...
- linux文件的特殊权限及隐藏权限
基础知识 相信大家应该都知道linux的文件基本权限,使用ls -l命令可以显示文件的基本权限,"-rwxrwxrwx.",第一位表示文件的属性(是文件- ,目录d等),后面每隔 ...
- 基于Homestead搭建PHP项目开发环境(适合Zend Framework,Laravel,Yii,thinkphp等)
参考: https://framework.zend.com/bl...参考: https://laravel.com/docs/5.5/... 第一步:软件的下载和安装 软件1:VirtualBox ...
- Problem 22
Problem 22 Using names.txt (https://projecteuler.net/problem=22)(right click and 'Save Link/Target A ...
- 阿里云oss用做文件存储工具类
package com.fsk.fsksystem.util; import java.io.IOException; import java.io.InputStream; import java. ...
- 0209利用innobackupex进行简单数据库的备份
利用innobackupex进行简单数据库的备份yum install perl-DBIyum install perl-DBD-MySQLyum install perl-Time-HiResyum ...