【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]的区间和为 ...
随机推荐
- 014.Nginx跨域配置
一 跨域概述 1.1 同源策略 同源策略是一个安全策略.同源,指的是协议,域名,端口相同.浏览器处于安全方面的考虑,只允许本域名下的接口交互,不同源的客户端脚本,在没有明确授权的情况下,不能读写对方的 ...
- Android Studio(Kotlin)之RecyclerView
RecyclerView应该是ListView的增强版. RecyclerView与ListView的区别(我认为的): RecyclerView的性能比ListView高 RecyclerView支 ...
- abp vnext 开发快速入门 4 跨域设置
由于项目采用的微服务框架,前端与后端交互难免有跨域的问题.abp vnext实现跨域也很简单,只需要设置几处就可以了,这里只讲全局的跨域,至于局部的Action如何跨域请自行搜索.netcore 跨域 ...
- JMS微服务开发示例(一)Hello world
网关部署 1.在网关服务器上,安装.net core 3.1运行环境: 2.到 https://www.cnblogs.com/IWings/p/13354541.html 下载Gateway.zip ...
- 常见的HTTP返回状态值
200 (成功) 服务器已成功处理了请求. 通常,这表示服务器提供了请求的网页. 301 (永久移动) 请求的网页已永久移动到新位置. 服务器返回此响应(对 GET 或 HEAD 请求的响应)时,会自 ...
- Day02_Vue.js与Webpack
Vue.js与Webpack 1 vue.js介绍 1.vue.js是什么? Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设 ...
- Python日期和时间_什么是Tick_什么是时间元组_获取当前时间
Python 日期和时间_什么是 Tick _什么是时间元组: 时间和日期:某年某月某日某时某分某秒 Tick: 时间间隔以 秒 为单位的浮点小数,起始时间为:1970年1月1日0点0分开始 # Ti ...
- PHP unixtojd() 函数
------------恢复内容开始------------ 实例 把 Unix 时间戳转换为儒略日计数: <?phpecho unixtojd();?> 运行实例 » 定义和用法 uni ...
- luogu P2354 [NOI2014]随机数生成器 贪心 卡空间 暴力
LINK:随机数生成器 观察数据范围还是可以把矩阵给生成出来的. 考虑如何求出答案.题目要求把选出的数字从小到大排序后字典序尽可能的小 实际上这个类似于Mex的问题. 所以要从大到小选数字 考虑选择一 ...
- bzoj 题目选做
这里将记录着我在接下来的日子里在bzoj上遇到的各种 毒瘤题目 1.轮状病毒 题目是很没意思的 列出状态 显然无法递推 我简单推了一下加动态加点的状态 嗯发现规律没有那么简单 打表 也不太能发现吧 正 ...