Knight Tournament 合并区间
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.
Example
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
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.
难怪总是超时,没判断完一个元素应该让他等于区间尾部的元素的下一个元素的编号才对
而不是等于尾部元素
代码:
#include <iostream>
#include <cstdio>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
int main()
{
int a[]={},b[],n,m,x,y,z,temp;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++)
b[i]=i+;
for(int k=;k<m;k++)
{
scanf("%d%d%d",&x,&y,&z);
if(x>y)swap(x,y);
for(int i=x;i<z;i=temp)
{
temp=b[i];
if(!a[i])a[i]=z;
b[i]=z;
}
for(int i=z+;i<=y;i=temp)
{
temp=b[i];
if(!a[i])a[i]=z;
b[i]=b[y];///注意这一步 应该等于y得下一个 而不是等于y
}
}
for(int i=;i<=n;i++)
printf("%d ",a[i]);
}
Knight Tournament 合并区间的更多相关文章
- CodeForces - 357C Knight Tournament 伪并查集(区间合并)
		
Knight Tournament Hooray! Berl II, the king of Berland is making a knight tournament. The king has a ...
 - CodeForce 356A  Knight Tournament(set应用)
		
Knight Tournament time limit per test 3 seconds memory limit per test 256 megabytes input standard ...
 - 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 ...
 - [LeetCode] Merge Intervals 合并区间
		
Given a collection of intervals, merge all overlapping intervals. For example, Given [1,3],[2,6],[8, ...
 - lintcode:合并区间
		
题目: 合并区间 给出若干闭合区间,合并所有重叠的部分. 样例 给出的区间列表 => 合并后的区间列表: [ [ [1, 3], [1, 6], [2, 6], => [8, 10], [ ...
 - LeetCode(56):合并区间
		
Medium! 题目描述: 给出一个区间的集合,请合并所有重叠的区间. 示例 1: 输入: [[1,3],[2,6],[8,10],[15,18]] 输出: [[1,6],[8,10],[15,18] ...
 - HRBUST - 1818 石子合并 区间dp入门
		
有点理解了进阶指南上说的”阶段,状态和决策“ /* 区间dp的基础题: 以区间长度[2,n]为阶段,枚举该长度的区间,状态dp[l][r]表示合并区间[l,r]的最小费用 状态转移方程dp[l][r] ...
 - 【LeetCode】数组--合并区间(56)
		
写在前面 老粉丝可能知道现阶段的LeetCode刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
 
随机推荐
- CentOS Gnome字体不清晰
			
需要安装字体:dejavu-sans-mono-fonts The package you need to install is dejavu-sans-mono-fonts. This is the ...
 - windows批处理命令
			
前言 批处理文件(batch file)包含一系列 DOS命令,通常用于自动执行重复性任务.用户只需双击批处理文件便可执行任务,而无需重复输入相同指令.编写批处理文件非常简单,但难点在于确保一切按顺序 ...
 - 解决dos窗口乱码问题
			
大家有没有遇到这样的情况,看着就糟心 打开dos窗口, 输入命令 chcp 936 (936表示中文编码GBK, 也可以设置其他编码), 回车一下执行. 鼠标右键 -> 属性 (关键一步): ...
 - XML和Schema
			
2017-11-03 19:33:56 XML:Extensible Markup Language,也就是可扩展标记语言.XML工具使处理和转化信息变得十分容易和方便. XML和HTML格式是古老的 ...
 - JVM中对象的内存布局与访问定位
			
一.对象的内存布局 已主流的HotSpot虚拟机来说, 在HotSpot虚拟机中,对象在内存中存储的布局可以分为3块区域:对象头(Header).实例数据(Instance Data)和对齐填 ...
 - 20161226xlVBA演示文稿替换文字另存pdf
			
Const ModelText As String = "机构名称" Const ModelName As String = "测试文件.pptx" Sub N ...
 - hdu-2227-dp+bit
			
Find the nondecreasing subsequences Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/3 ...
 - iOS UI-团购案例(通过xib文件自定义UITableViewCell)
			
一.Model #import <Foundation/Foundation.h> @interface Goods : NSObject @property (nonatomic, co ...
 - eclipse设置条件断点
			
1. 在Breakpoints页面,选中断点然后右键,选择"Breakpoint Properties" 2. 勾选Conditional,并输入条件.这样,当name等于&quo ...
 - 从零开始学习Vue(二)
			
思维方式的变化 WebForm时代, Aspx.cs 取得数据,绑定到前台的Repeater之类的控件.重新渲染整个HTML页面.就是整个页面不断的刷新;后来微软打了个补丁,推出了AJAX控件,比如U ...