【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]的区间和为 ...
随机推荐
- 解决win10安装flask-mysqldb报错 Python2.7
win10上安装的pycharm,在pycharm创建的py2.7虚拟环境中安装flask-sqlalchemy 执行pip install flask-mysqldb报错 error: Micros ...
- 利用Serverless应用搭建Hexo博客
本文将介绍如何使用火爆的Serverless应用,15分钟快速搭建Hexo博客.以腾讯云提供的Serverless应用–云开发为例: 步骤1:安装 CloudBase CLI 以及本地部署 Hexo ...
- npm 使用报错合集
1.Unexpected end of JSON input while parsing near '...e,"directories":{},"d' 这个问题经常会出 ...
- 通过C语言获取MAC地址(转)
原文:http://blog.chinaunix.net/uid-25885064-id-3125167.html #include <sys/ioctl.h>#include <n ...
- 一周一个中间件-ES搜索引擎
---toc: truetitle: 一周一个中间件-ES搜索引擎date: 2019-09-19 18:43:36tags: - 中间件 - 搜索引擎--- ## 前言 > 在众多搜索引擎中, ...
- laravel 缓存相关常用操作
//----------设置缓存----------- //Cache::put($key,$val,$minutes); 如果$key已存在,则覆盖原有值 Cache::put('name', '张 ...
- Qt子类化后qss设置背景色无效的问题
1.问题背景 在某个类中,用到了一个组合的widget,有按钮进度条等,类似于视频播放器按钮控制区和精度条(参考了很多feiyangqingyun的文章,感谢),调试正常后整理代码,为了提高代码可读性 ...
- Python exec 内置语句
描述 exec 执行储存在字符串或文件中的Python语句,相比于 eval,exec可以执行更复杂的 Python 代码.岭组词 https://www.cgewang.com/post/2205 ...
- PHP 太空船运算符(组合比较符)
PHP 7 新增加的太空船运算符(组合比较符)用于比较两个表达式 $a 和 $b,如果 $a 小于.等于或大于 $b时,它分别返回-1.0或1. 实例 <?php // 整型比较 print( ...
- Python的基本运用(一)
1.a**b 表示a的b次方. 2.def something(a,b): 定义函数,注意 python的缩进 . 3.print (a)与print a 的区别,python3中不支持print ...