【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]的区间和为 ...
随机推荐
- C++语法小记---多重继承
多重继承 工程中不建议使用多继承,因为多继承带来的问题比带来的便利多,已被放弃 问题一:多重继承的对象,向上获取指针时,有不同的地址 ----无法解决 问题二:菱形继承问题,导致成员冗余 ----虚继 ...
- 拿下Netty这座城,从现在开始!
你好,我是彤哥,技术公号主"彤哥读源码"的运营者. 其实,我刚学习Netty的时候,也是很迷茫的,直到有一天,一个同事收到了阿里的offer,他要去阿里做中台了,临走前他偷偷地告诉 ...
- Android Studio(Kotlin)之RecyclerView
RecyclerView应该是ListView的增强版. RecyclerView与ListView的区别(我认为的): RecyclerView的性能比ListView高 RecyclerView支 ...
- 多云架构下,JAVA微服务技术选型实例解析
[摘要] 本文介绍了基于开源自建和适配云厂商开发框架两种构建多云架构的思路,以及这些思路的优缺点. 微服务生态 微服务生态本质上是一种微服务架构模式的实现,包括微服务开发SDK,以及微服务基础设施. ...
- web自动化 -- 框架
一.框架源码 https://github.com/jiangnan27/Autotest_UI_Open 二.框架大概介绍 Python3 + selenium3 + pytest5.3 + a ...
- 保姆级教程,如何发现 GitHub 上的优质项目?
先看再点赞,给自己一点思考的时间,微信搜索[沉默王二]关注这个靠才华苟且的程序员.本文 GitHub github.com/itwanger 已收录,里面还有一线大厂整理的面试题,以及我的系列文章. ...
- Day02_WebCrawler(网络爬虫)
学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"webcrawler"获取视频和教程资料! b站在线视 ...
- PHP array_values() 函数
实例 返回数组中所有的值(不保留键名): <?php$a=array("Name"=>"Peter","Age"=>&qu ...
- c# Dictionary的使用
创建: Dictionary<string, OverCaseData> dataDic = new Dictionary<string, OverCaseData>() ...
- C/C++编程笔记:编写完成了一个C/C++程序,如何做一个界面出来?
最简单的方法是用vc6新建一个Win32 Application空工程,然后添加一个cpp文件,输入 (注意添加对话框资源,并且在对话框上添加一个文本框) #include #include &quo ...