【HDU3038】How Many Answers Are Wrong - 带权并查集
描述
TT and FF are ... friends. Uh... very very good friends -________-b
FF is a bad boy, he is always wooing TT to play the following game with him. This is a very humdrum game. To begin with, TT should write down a sequence of integers-_-!!(bored).
Then, FF can choose a continuous subsequence from it(for example the subsequence from the third to the fifth integer inclusively). After that, FF will ask TT what the sum of the subsequence he chose is. The next, TT will answer FF's question. Then, FF can redo this process. In the end, FF must work out the entire sequence of integers.
BoringBoringa very very boring game!!! TT doesn't want to play with FF at all. To punish FF, she often tells FF the wrong answers on purpose.
The bad boy is not a fool man. FF detects some answers are incompatible. Of course, these contradictions make it difficult to calculate the sequence.
However, TT is a nice and lovely girl. She doesn't have the heart to be hard on FF. To save time, she guarantees that the answers are all right if there is no logical mistakes indeed.
What's more, if FF finds an answer to be wrong, he will ignore it when judging next answers.
But there will be so many questions that poor FF can't make sure whether the current answer is right or wrong in a moment. So he decides to write a program to help him with this matter. The program will receive a series of questions from FF together with the answers FF has received from TT. The aim of this program is to find how many answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to do this job. And now he is asking for your help~(Why asking trouble for himself~~Bad boy)
题意
给你 n 个数和 m 个条件,每个条件告诉你一个区间的和,问有多少个给出的条件是错的(第一个条件是正确的)
思路
看不出是并查集系列。
由于这 n 个数不一定都是正数,所以一个条件错误只有这种可能:
\([a,b]\) 给出的和与 \([a,x_1],[x_1 + 1,x_2],...,[x_k +1,b]\) 给出的和的和不同。
于是就用并查集维护一个 \(v_x\) 表示这个 \(x\) 这个点到根的和。
每次给出一个条件 \(l,r,x\),如果 find(l-1) == r,则现在加入的区间 \([l,r]\) 可以拼凑出原来有的区间,于是就判一判 \(v[r]-v[l-1]\) 是否等于 \(x\)。
否则合并 \(l-1\) 和 \(r\),更新 v[find(r)] 为 v[l]+x-v[r]

L,R 分别表示 l,r 的根
代码
#include <cstdio>
const int maxn = 200000 + 10;
int n,m,fa[maxn],v[maxn],ans;
inline int find(int x) {
if (fa[x] == x) return x;
int tmp = fa[x];
fa[x] = find(fa[x]);
v[x] += v[tmp];
return fa[x];
}
int main() {
for (;scanf("%d%d",&n,&m) ^ EOF;ans = 0) {
for (int i = 0;i <= n;i++) fa[i] = i,v[i] = 0;
for (int l,r,x,a,b;m--;) {
scanf("%d%d%d",&l,&r,&x); l--;
a = find(l); b = find(r);
if (a ^ b) {
fa[b] = a;
v[b] = v[l]+x-v[r];
} else if (v[r]-v[l]^x) ans++;
}
printf("%d\n",ans);
}
return 0;
}
【HDU3038】How Many Answers Are Wrong - 带权并查集的更多相关文章
- HDU3038 How Many Answers Are Wrong —— 带权并查集
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3038 How Many Answers Are Wrong Time Limit: 200 ...
- HDU3038 How Many Answers Are Wrong[带权并查集]
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- hdu3038How Many Answers Are Wrong(带权并查集)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3038 题解转载自:https://www.cnblogs.com/liyinggang/p/53270 ...
- HDU3038:How Many Answers Are Wrong(带权并查集)
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- hdu 3038 How Many Answers Are Wrong ( 带 权 并 查 集 )
How Many Answers Are Wrong Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- How Many Answers Are Wrong(带权并查集)
How Many Answers Are Wrong http://acm.hdu.edu.cn/showproblem.php?pid=3038 Time Limit: 2000/1000 MS ( ...
- HDU 3038 How Many Answers Are Wrong(带权并查集)
太坑人了啊,读入数据a,b,s的时候,我刚开始s用的%lld,给我WA. 实在找不到错误啊,后来不知怎么地突然有个想法,改成%I64d,竟然AC了 思路:我建立一个sum数组,设i的父亲为fa,sum ...
- 【带权并查集】【HDU3038】【How Many Answers Are Wrong】d s
这个题看了2天!!!最后看到这篇题解才有所明悟 转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4298091.html ---by 墨染之樱 ...
- HDU-3038 How Many Answers Are Wrong(带权并查集区间合并)
http://acm.hdu.edu.cn/showproblem.php?pid=3038 大致题意: 有一个区间[0,n],然后会给出你m个区间和,每次给出a,b,v,表示区间[a,b]的区间和为 ...
随机推荐
- 写给程序员的机器学习入门 (八) - 卷积神经网络 (CNN) - 图片分类和验证码识别
这一篇将会介绍卷积神经网络 (CNN),CNN 模型非常适合用来进行图片相关的学习,例如图片分类和验证码识别,也可以配合其他模型实现 OCR. 使用 Python 处理图片 在具体介绍 CNN 之前, ...
- JAVA四大名著(程序员必读)
JAVA四大名著: <JAVA语言程序设计(第4版)>-----初级 <JAVA核心技术>卷I----初级.<JAVA核心技术>卷II----中级 <JAVA ...
- laravel 资源控制器方法列表
以 PostController 控制器的每个方法都有对应的请求方式.路由命名.URL.方法名和业务逻辑约定. HTTP请求方式 URL 控制器方法 路由命名 业务逻辑描述 GET post inde ...
- mapstruct 实体转换及List转换,@Mapper注解转换
本文参考 https://blog.csdn.net/u012373815/article/details/88367456 主要是为了自己使用方便查询. 这些都是我平时用到了,大家有什么好方法或者有 ...
- 如何在Linux下的C++文件使用GDB调试
首先在Linux下写好一个.Cpp的文件. #include<stdio.h> #include<stdlib.h> using namespace std; void sho ...
- 使用Spring Validation优雅地校验参数
写得好的没我写得全,写得全的没我写得好 引言 不知道大家平时的业务开发过程中 controller 层的参数校验都是怎么写的?是否也存在下面这样的直接判断? public String add(Use ...
- PHP curl_setopt函数
(PHP 4 >= 4.0.2, PHP 5) curl_setopt — 设置一个cURL传输选项. 说明 bool curl_setopt ( resource $ch , int $opt ...
- PHP printf() 函数
实例 输出格式化的字符串: <?php高佣联盟 www.cgewang.com$number = 9;$str = "Beijing";printf("There ...
- 小甲鱼零基础汇编语言学习笔记第二章之寄存器(CPU工作原理,CPU内部通讯)
这一章主要介绍了CPU中的重要器件——寄存器,整个系列通篇是以8086CPU作为探讨对象,其它更高级的CPU都是在此基础之上进行的升级. 1.一个典型的CPU是由运算器.控制器.寄存器等器件组成, ...
- luogu P1712 [NOI2016]区间 贪心 尺取法 线段树 二分
LINK:区间 没想到尺取法. 先说暴力 可以发现答案一定可以转换到端点处 所以在每个端点从小到大扫描线段就能得到答案 复杂度\(n\cdot m\) 再说我的做法 想到了二分 可以进行二分答案 从左 ...