题干:()

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.

Boring~~Boring~~a 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)

Input

Line 1: Two integers, N and M (1 <= N <= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF asked her M questions.

Line 2..M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answered FF that the sum from Ai to Bi is Si. It's guaranteed that 0 < Ai <= Bi <= N.

You can assume that any sum of subsequence is fit in 32-bit integer.

Output

A single line with a integer denotes how many answers are wrong.

Sample Input

10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1

Sample Output

1

题目大意:

有n次询问,给出a到b区间的总和(左右均包含在内),问这n次给出的总和中有几次是和前面已近给出的是矛盾的。其实问矛盾的已经很典型了就是并查集可做。。。

解题报告:

此题的权值为区间的和,所以合并区间(的端点)的同时将区间的和维护一下即可。对于A~B之间的和是S,其实可以理解成B比A-1大S,所以代码中要将区间左端点-1;

AC代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
const int MAX = 200000 + 5;
using namespace std;
int n,m;
int u,v;
int ans;
int f[MAX];
int w[MAX];
int sum;
int getf( int v) {if(v == f[v] ) return v;//一方面是为了递归函数的出口用的,另一方面加深一下理解,这里是指既然他已经是根节点了,那就不需要更新权值w和父节点f了,所以需要返回。并查集里面还是有很多道道的啊!!有很多更深层的理解和完美的思想在里面,内核思想和能解决的问题应该还远不止这些。 需要以后在做题中慢慢发掘。int tmp = getf(f[v]);w[v] = w[v] + w[f[v] ] ;f[v]=tmp;//或f[v] = getf(f[v])return f[v];
} bool merge(int u,int v,int sum) {int t1=getf(u);int t2=getf(v);if(t1 == t2) {return w[v ]-w[u ] == sum;}if(t1!=t2) {f[t2]=t1;//(a--)w[a]表示a到0的和,w[b]表示a+1到b的和; w[t2]=w[u]-w[v]+sum;//w[t2]表示t1,t2的距离; return true ;//即 如果当前他俩并不在一个集合当中,说明这一段区间并没有被记录过,所以一定推断不出错误所以一定会返回1,也就是说判断出错误当且仅当他俩已经在一个集合当中了。也就是上面那个if所表达的东西 }}
void init() {for(int i = 0; i<=MAX; i++) {//又犯这种错误!!!就初始化到n?!!?!?? w[i] = 0;f[i] = i;//}
}
int main()
{int m,n;while(~scanf("%d %d",&n,&m) ) {init();ans=0;while(m--) {scanf("%d %d %d",&u,&v,&sum);//只有输入过u和v的值才能做出判断,对这个区间内的值我们都不能做出判断,所以这题考虑并查集。 u--;//区间(0,b)分为(0,u-1)和(u,v); if(!merge(u,v,sum) ) ans++;}printf("%d\n",ans);} return 0 ;
}

总结:

1.通过做此题,加深了对并查集用处的理解,算是真正有了并查集的思想。

2. 并查集判断的题型中bool型的merge函数很常用。

【HDU - 3038】How Many Answers Are Wrong (带权并查集--权为区间和)相关推荐

  1. 【HDU - 3172】Virtual Friends(带权并查集--权为集合元素个数)

    题干: These days, you can do all sorts of things online. For example, you can use various websites to ...

  2. How Many Answers Are Wrong HDU - 3038(带权并查集经典题,满满的都是注释)

    How Many Answers Are Wrong HDU - 3038  点击打开链接 题意:现在有n个数(你并不知道这n个数是什么),m次查询,每次查询给出u,v,w.表示从第u个数到第v个数的 ...

  3. How Many Answers Are Wrong HDU - 3038(带权并查集)

    TT and FF are - friends. Uh- very very good friends -________-b FF is a bad boy, he is always wooing ...

  4. Valentine's Day Round hdu 5176 The Experience of Love [好题 带权并查集 unsigned long long]

    传送门 The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Ja ...

  5. HDU 3047 Zjnu Stadium (带权并查集)

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=3047 题目: Problem Description In 12th Zhejiang College ...

  6. HDU 5176 The Experience of Love 带权并查集

    The Experience of Love Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  7. 带权并查集 HDU - 3047

    题意: 一圈座位有n个,给出m组序号之间的关系,比如,1 2 150 代表2号坐在1号位置序号+150,看m组数据有多少组冲突的. 思路: 带权并查集模板. #include<stdio.h&g ...

  8. 【POJ - 1703】Find them, Catch them(带权并查集之--种类并查集 权为与父节点关系)

    题干: Find them, Catch them Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 36176   Accep ...

  9. POJ 1984 Navigation Nightmare 【经典带权并查集】

    任意门:http://poj.org/problem?id=1984 Navigation Nightmare Time Limit: 2000MS   Memory Limit: 30000K To ...

最新文章

  1. 搜索引擎ElasticSearchV5.4.2系列二之ElasticSearchV5.4.2+kibanaV5.4.2+x-packV5.4.2安装
  2. 文件节点的linux指令,Java工程师必学的Linux命令(一)文件与目录管理
  3. C/Cpp / 构造函数种类
  4. java iconsolefactory_java – 如何设置IOConsole的Caret
  5. 约瑟夫问题公式及代码实现
  6. There is no Action mapped for namespace / and action name .
  7. Java 8 异步 API、循环、日期,用好提高生产力!
  8. Hi3536 uboot引导内核全过程
  9. iphone绕过id_破解苹果安卓手机屏幕锁ID锁 极品神器
  10. 通信中间件技术之DDS
  11. 页面视觉稳定性之优化CLS
  12. Dubbo太难了,我决定加入Spring Cloud阵营了...
  13. cadence 软件导出ad 文件出错
  14. 杀不死的人狼——我读《人月神话》(三)
  15. 牛客网练习,某公司2017秋招。叫车 2017年
  16. PCB贴片元件封装焊盘设计尺寸标准
  17. 小米运维实习生被开除:竟是因为没有用到高防服务器
  18. 宝塔安装php成功无显示,宝塔面板安装 LAMP 或 LNMP 后没有成功的原因
  19. DL4J中文文档/开始/速查表
  20. CPA二十--关联方关系的判断标准(转载)

热门文章

  1. [Java学习资料] [成长之路]
  2. 推荐系统——协同过滤
  3. js层级选择框样式_【JavaWeb】85:jQuery的各种选择器
  4. python如何输入空行_在python中,如何在接受用户输入时跳过空行?
  5. C语言 第五章 选择结构 答案,c语言第五章 选择结构程序设计(习题册答案).doc
  6. 360浏览器打不开网页_苹果移动端、PC端safari浏览器打不开网页的解决方案!
  7. linux内核编译后 make: 警告:检测到时钟错误.,系统时钟 make: 警告:检测到时钟错误。您的创建可能是不完整的。...
  8. UE4 多个static mesh合并成一个static mesh
  9. Tiny6410上安装debian基本系统的过程
  10. S3C2440中断解析和基于WINCE操作系统的中断分析(整理于网络,用于按键中断使用)