CodeForces - 357C Knight Tournament 伪并查集(区间合并)
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.
开始想到并查集,其实只要记录他的上一个值一次就是祖先了,所以不需要更新根节点。重点是区间合并,next起到跳跃作用,【x,y】区间中【x,z)跳到z,(z,y】跳到y+1。分别合并隔离出中间点z。
#include<stdio.h> int f[],b[],next[]; int main()
{
int n,m,x,y,z,i,j;
scanf("%d%d",&n,&m);
for(i=;i<=n;i++){
f[i]=i;
next[i]=i+;
}
for(i=;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
for(j=x;j<z;){
if(b[j]==&&j!=z){
b[j]=;
f[j]=z;
}
int t=j;
j=next[j];
next[t]=z;
}
for(j=z;j<=y;){
if(b[j]==&&j!=z){
b[j]=;
f[j]=z;
}
int t=j;
j=next[j];
next[t]=next[y];
}
}
for(i=;i<=n;i++){
if(i!=) printf(" ");
if(f[i]==i) printf("");
else printf("%d",f[i]);
}
return ;
}
CodeForces - 357C Knight Tournament 伪并查集(区间合并)的更多相关文章
- codeforces 357C Knight Tournament(set)
Description Hooray! Berl II, the king of Berland is making a knight tournament. The king has already ...
- POJ-1733 Parity game(带权并查集区间合并)
http://poj.org/problem?id=1733 题目描述 你和你的朋友玩一个游戏.你的朋友写下来一连串的0或者1.你选择一个连续的子序列然后问他,这个子序列包含1的个数是奇数还是偶数.你 ...
- HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=3038 大致题意: 有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为 ...
- POJ1456贪心(set或者并查集区间合并)
题意: 给你n商品,每个商品有自己的价值还有保质期,一天最多只能卖出去一个商品,问最大收益是多少? 思路: 比较好想的贪心,思路是这样,每一次我们肯定拿价值最大的,至于在那天拿 ...
- Codeforces 1166F 并查集 启发式合并
题意:给你一张无向图,无向图中每条边有颜色.有两种操作,一种是询问从x到y是否有双彩虹路,一种是在x到y之间添加一条颜色为z的边.双彩虹路是指:如果给这条路径的点编号,那么第i个点和第i - 1个点相 ...
- BZOJ2733[HNOI2012]永无乡——线段树合并+并查集+启发式合并
题目描述 永无乡包含 n 座岛,编号从 1 到 n,每座岛都有自己的独一无二的重要度,按照重要度可 以将这 n 座岛排名,名次用 1 到 n 来表示.某些岛之间由巨大的桥连接,通过桥可以从一个岛 到达 ...
- BZOJ 3673: 可持久化并查集(可持久化并查集+启发式合并)
http://www.lydsy.com/JudgeOnline/problem.php?id=3673 题意: 思路: 可持久化数组可以用可持久化线段树来实现,并查集的查询操作和原来的一般并查集操作 ...
- BZOJ 4668: 冷战 并查集启发式合并/LCT
挺好想的,最简单的方法是并查集启发式合并,加暴力跳父亲. 然而,这个代码量比较小,比较好写,所以我写了 LCT,更具挑战性. #include <cstdio> #include < ...
- [HDU 3712] Fiolki (带边权并查集+启发式合并)
[HDU 3712] Fiolki (带边权并查集+启发式合并) 题面 化学家吉丽想要配置一种神奇的药水来拯救世界. 吉丽有n种不同的液体物质,和n个药瓶(均从1到n编号).初始时,第i个瓶内装着g[ ...
随机推荐
- python3短信接口使用
import http.client from urllib import parse host = "106.ihuyi.com" sms_send_uri = "/w ...
- Drawing Images and Text
using System;using UIKit;using Foundation;using CoreGraphics;namespace GraphicsAnimation{ public cla ...
- mysql-connector-java与mysql版本的对应
记录下mysql-connector-java与mysql版本的对应关系,已方便以后参考,这是最新版本对应, 时间:2017年5月23日 官网文档地址: https://dev.mysql.com/d ...
- easyui Combotree 怎么加载数据 支持多选
1.开发环境vs2012 mvc4 c# 2.HTML前端代码 <%@ Page Language="C#" AutoEventWireup="true" ...
- objective-c的代码块block
一.block 1.bock是由于^开头,括号里面填写参数类型. 标准代码块: 返回值 (^代码块名称) (参数类型) = ^(参数) {方法体}; 2.我们的块即可以定义在函数内或者对象 ...
- webpack v3 结合 react-router v4 做 dynamic import — 按需加载(懒加载)
为什么要做dynamic import? dynamic import不知道为什么有很多叫法,什么按需加载,懒加载,Code Splitting,代码分页等.总之,就是在SPA,把JS代码分成N个页面 ...
- 常用的ES6
1 let 和 const 作用域: 只在声明指令的块级作用域内有效.① let所声明的变量可以改变,值和类型都可以改变,没有限制. let a = 123 a = 456 // 正确,可以改变 le ...
- 创建一个catkin工作空间
先确定自己的环境变量是否设置正确 export | grep ROS 若出现如下的,说明是正确的 declare -x ROSLISP_PACKAGE_DIRECTORIES="" ...
- 在Eclipse Java EE编译器中修改Web项目的发布名称
在工程目录上右键, 选properties, 弹出属性窗口, 选中Web Project Settings, 在右边的Context root中修改保存即可 死马当做活马医 在你的工程目录下找到.se ...
- 【Selenium】跳转问题
/** * rewrite the get method, adding user defined log</BR> * 地址跳转方法,使用WebDriver原生get方法,加入失败重试的 ...