【链接】 我是链接,点我呀:)

【题意】

n个人矩形m场比赛
每场比赛由编号为li~ri且之前没有被淘汰的人进行。
已知第i场的winner是xi
winner会把这一场其他所有的人都淘汰。
问你n个人每个人都是被谁给淘汰的.

【题解】

并查集
初始条件f[i] = i,nex[i] = i + 1;
每一轮战斗,让输的人的f[find_father(i)]变成x[i]
但是在执行f[find_father(i)]=x[i]的时候
我们要记录一下ans[find_father(i)] = x[i]
因为到最后并查集经过路径压缩以后,就没有明显的主从关系了
之后,让li~ri里面的每个nex[i]都等于r[i]+1(要先记录下nex[i]再改变nex[i],因为i需要跳到nex[i],进而继续将li~ri这个区间里面的nex[i]都指向r[i]+1,以及进行
合并操作,把对应区间的子winner指向新winner,记录ans值)就好
这样从左到右枚举的时候就会跳过败者了(只会通过find_father(i)找到这一棵子树的根)
最后输出ans[]就好
注意ans[i]=i的话 那个人是最后的winner

【代码】

import java.io.*;
import java.util.*; public class Main { static InputReader in;
static PrintWriter out; public static void main(String[] args) throws IOException{
//InputStream ins = new FileInputStream("E:\\rush.txt");
InputStream ins = System.in;
in = new InputReader(ins);
out = new PrintWriter(System.out);
//code start from here
new Task().solve(in, out);
out.close();
} static int N = (int)3e5;
static class Task{
int n,m;
int nex[],f[],ans[]; int ff(int x) {
if (x==f[x]) return x;
else {
f[x] = ff(f[x]);
return f[x];
}
} public void solve(InputReader in,PrintWriter out) {
nex = new int[N+10];f = new int[N+10];ans = new int[N+10];
for (int i = 1;i <= N;i++) {
nex[i] = i + 1;
f[i] = i;
}
n = in.nextInt();m = in.nextInt();
for (int i = 1;i <= m;i++) {
int li,ri,xi;
li = in.nextInt();ri = in.nextInt();xi = in.nextInt();
for (int j = li;j <= ri;) {
int fa = ff(j);
f[fa] = xi;
ans[fa] = xi;
int temp = nex[j];
nex[j] = ri+1;
j = temp;
}
}
for (int i = 1;i <= n;i++) {
if (ans[i]==i)
out.print(0);
else
out.print(ans[i]);
out.print(" ");
}
}
} static class InputReader{
public BufferedReader br;
public StringTokenizer tokenizer; public InputReader(InputStream ins) {
br = new BufferedReader(new InputStreamReader(ins));
tokenizer = null;
} public String next(){
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
}catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
} public int nextInt() {
return Integer.parseInt(next());
}
}
}

【Codeforces 356A】Knight Tournament的更多相关文章

  1. 【codeforces 415D】Mashmokh and ACM(普通dp)

    [codeforces 415D]Mashmokh and ACM 题意:美丽数列定义:对于数列中的每一个i都满足:arr[i+1]%arr[i]==0 输入n,k(1<=n,k<=200 ...

  2. 1450:【例 3】Knight Moves

    1450:[例 3]Knight Moves  题解 这道题可以用双向宽度搜索优化(总介绍在  BFS ) 给定了起始状态和结束状态,求最少步数,显然是用BFS,为了节省时间,选择双向BFS. 双向B ...

  3. 【codeforces 602E】Kleofáš and the n-thlon

    time limit per test1 second memory limit per test256 megabytes inputstandard input outputstandard ou ...

  4. 【codeforces 752F】Santa Clauses and a Soccer Championship

    time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...

  5. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  6. 【codeforces 707C】Pythagorean Triples

    [题目链接]:http://codeforces.com/contest/707/problem/C [题意] 给你一个数字n; 问你这个数字是不是某个三角形的一条边; 如果是让你输出另外两条边的大小 ...

  7. 【codeforces 709D】Recover the String

    [题目链接]:http://codeforces.com/problemset/problem/709/D [题意] 给你一个序列; 给出01子列和10子列和00子列以及11子列的个数; 然后让你输出 ...

  8. 【codeforces 709B】Checkpoints

    [题目链接]:http://codeforces.com/contest/709/problem/B [题意] 让你从起点开始走过n-1个点(至少n-1个) 问你最少走多远; [题解] 肯定不多走啊; ...

  9. 【codeforces 709C】Letters Cyclic Shift

    [题目链接]:http://codeforces.com/contest/709/problem/C [题意] 让你改变一个字符串的子集(连续的一段); ->这一段的每个字符的字母都变成之前的一 ...

随机推荐

  1. 堆排序的C实现

    这几天有点抵触情绪,看过了快速排序还有一些别的东西,但是一点都不想写有点复杂的代码0 0拖到了今天终于写了前几天就应该自己写一下的堆排序,完全用C语言写的,下面把代码贴一下.很多地方写得并不好,不过已 ...

  2. PCB 帆软FineReport安装,布署,配置

    公司使用帆软FineReport做为报表平台工具也有一年多时间,而FineReport报表平台与Tomcat Web应用服务是站队在java阵营里,因为相信拥抱微软;.net未来发展会越来越好,所以对 ...

  3. E20180120-hm

    derive vt. 得到,导出; 源于,来自; (从…中) 提取; hierarchy  n. [计] 分层,层次; 等级制度; 统治集团; 天使的级别或等级; inheritance  n. 继承 ...

  4. Manacher HDOJ 5371 Hotaru's problem

    题目传送门 /* 题意:求形如(2 3 4) (4 3 2) (2 3 4)的最长长度,即两个重叠一半的回文串 Manacher:比赛看到这题还以为套个模板就行了,因为BC上有道类似的题,自己又学过M ...

  5. 枚举+贪心 HDOJ 4932 Miaomiao's Geometry

    题目传送门 /* 题意:有n个点,用相同的线段去覆盖,当点在线段的端点才行,还有线段之间不相交 枚举+贪心:有坑点是两个点在同时一条线段的两个端点上,枚举两点之间的距离或者距离一半,尽量往左边放,否则 ...

  6. 每天学点linux命令之nc

    nc is NetCat.素以短小精悍著称的网络工具包.主要用来开放的扫描端口(黑客或者OSAdmin的最爱),不同主机之间传输文字 | 文件. http://blog.csdn.net/zhangx ...

  7. Spring.Net学习笔记(八)-设置配置文件参数

    一.开发环境 VS2013 .netframework4.5 spring.net1.3.1 二.项目结构 三.开发过程 1.编写Person类 namespace SpringNetConfigAr ...

  8. 重新学习Java——对象和类(一)

    之前通过记笔记的方法,对于<Java核心技术>这本书的前一章进行了重新的复习,感觉效果很好,比单独看书带来了更好的复习效果,了解了很多以前不是很注意的一些细节,但是在一些自己较为熟悉的地方 ...

  9. javascirpt中的数字在计算机内存储为多少Byte

    JavaScript内部,所有数字都是以64位浮点数形式储存,即使整数也是如此 8 Byte = 64bit Javascript中,由于其变量内容不同,变量被分为基本数据类型变量和引用数据类型变量. ...

  10. 循环插入记录,id每次加1

    sql语句写法: begin for i in 1 .. 100 loop insert into table_name values(....); end loop; commit; end; 例子 ...