描述

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;
}

最新文章

  1. 哪些HTML5特性值得期待
  2. Windows操作系统优化(Win7版) - 进阶者系列 - 学习者系列文章
  3. 使用Sqlserver更新锁防止数据脏读
  4. Apache Storm 的历史及经验教训&mdash;&mdash;Nathan Marz【翻译】
  5. 分子模拟软件Schrodinger Suites 2015安装
  6. Activator.CreateInstance 方法 (Type) 的用法
  7. 再次探讨C++的动态绑定和静态绑定
  8. Linux环境快速搭建RocketMQ双Master模式
  9. [个人翻译]GitHub指导文件(GitHub Guides[Hello World])
  10. Ubuntu安装telent服务器时出现:apt-get:Package has no installation
  11. mysql的学习笔记(九)
  12. leetcode — gas-station
  13. 【数据库】MySql分割字符串
  14. 11g新特性-自动sql调优(Automatic SQL Tuning)
  15. p1518 The Tamworth Two
  16. Oracle表空间状态
  17. k8s-YAML配置文件
  18. 制作openstack使用的Ubuntu镜像
  19. 利用Github搭建自己的博客
  20. stenciljs 学习十二 官方doc 路由使用的例子

热门文章

  1. 多国正在遭遇新型勒索病毒Petya侵袭
  2. 大型Java进阶专题(九) 设计模式之总结
  3. 附002.Nginx全系列大总结
  4. jmeter配置原件之使用CSV Data Set Config参数化
  5. 面试题五十七:和为s的数字
  6. pycharm 退出虚拟环境
  7. MapReduce之Combiner合并
  8. SpringBoot动态注入Bean
  9. 5-Pandas之常用的描述性统计函数、汇总函数
  10. CF453A Little Pony and Expected Maximum 期望dp