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刷题将按照某一个特定的专题进行,之前的[贪心算法]已经结束,虽然只有三个题却包含了简单,中等,困难这三个维度,今天介绍的是第二个专题[数组] 数组( ...
随机推荐
- http-server 基于nodejs的http服务器
http-server所用场景: 作为前端的同学来说,想要运行一段代码,但又没有必要使用tomcat或是Apache http server,这个时候,一个简单的轻量的http-server就能搞定. ...
- English trip -- VC(情景课)2 B Classroom objects
Vocabulary focus 核心词汇 Listen and repeat. 听并跟读 1. a dictionary 2. paper 3. a pen 4. a ruler 5. a stap ...
- php--------php库生成二维码和有logo的二维码
php生成二维码和带有logo的二维码,上一篇博客讲的是js实现二维码:php--------使用js生成二维码. 今天写的这个小案例是使用php库生成二维码: 效果图: 使用了 php ...
- Confluence 6 使用 LDAP 授权连接一个内部目录 - 用户 Schema 设置
请注意:这部分仅在拷贝用户登录(Copy User on Login)功能被启用后可见. 其他用户 DN(Additional User DN) 这个值被用在进行用户查找和载入的时候来针对 base ...
- 1 python基础知识
一.python简介 编译型:将所有的源码先编译成机器型语言,并保存为二进制文件,然后一次性执行c c++ go swift 解释型:将代码一行一行边编译边解释python javascript ph ...
- C#下实现的K-Means优化[1]-「离群点检测」
资源下载 #本文PDF版下载 C#下实现的K-Means优化[1]-「离群点检测」 前言 在上一篇博文中,我和大家分享了「C # 下实现的多维基础K-MEANS聚类」的[C#下实现的基础K-MEANS ...
- 秒杀多线程第五篇 经典线程同步 关键段CS
本文首先介绍下如何使用关键段,然后再深层次的分析下关键段的实现机制与原理. 关键段CRITICAL_SECTION一共就四个函数,使用很是方便.下面是这四个函数的原型和使用说明. 函数功能:初始化 函 ...
- jsp jsp九个内置对象
每个JSP页面在第一次被访问的时候,WEB容器都会把请求交给JSP引擎(即一个JAVA)程序.JSP引擎先将JSP翻译成一个_jspServlet实质也是一次servlet,然后按照 Servl ...
- 推荐八款.Net优秀的开源CMS 内容管理系统
1,老牌内容管理系统SiteServer CMS 推荐指数:5 SiteServer CMS 是.NET平台的CMS系统,也是一款拥有十年历史与广泛知名度的CMS系统,2017年5月初迈出了自成立以 ...
- python元组()小括号
names = ('jack','rose','tom','jerry','james','jerry') print(names) print(names[0]) #使用元组中的元素 print(n ...