How Many Answers Are Wrong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 20324 Accepted Submission(s): 7002

Problem Description
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)

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个区间,要你推断哪些区间一定是错误的。
思路:合并区间,检查区间的值和所给的值。

第一次遇见带权并查集,所谓并查集就是在原有的关系上求等价关系,而带权并查集不仅仅要求等价关系,还要统计区间权值。
这个题还需要将路径压缩,因为条件只给的x->y的权值,和x、y的权值,以及x的根节点和y的根节点的权值,如果不将路径压缩那么问题会变得很复杂。首先讲一下路径压缩,画个图示意:

在一棵深度为2的树上,我们将深度为2上面的根节点直接与根节点相连,这就是所谓的路径压缩,因为这只是深度为2所以感觉没什么,但是设想一下深度100+的树将他压缩的效果,简直不要太好,(特殊问题特殊求解)。
路径压缩代码:

int FIND_X(int x)//路径压缩+求解沿路权值之和 
{
	if(x!=parent[x]){
		parent[x]=FIND_X(parent[x]);//路径压缩 
	}
	return parent[x];
}

然后理解然路径压缩后,我们将每个节点赋予权值,在路径压缩的基础上直接求出该节点到根节点的权值,画个图示意:

给出代码

int FIND_X(int x)//路径压缩+求解沿路权值之和 
{
	if(x!=parent[x]){
		int i=parent[x];
		parent[x]=FIND_X(parent[x]);//路径压缩 
		sum[x]+=sum[i];//沿路权值之和 
	}
	return parent[x];
}

其实就说先记录上一个节点的编号然后递归求和。

先上ac代码:

#include<iostream>
#define MAXSIZE 200000
using namespace std;
int parent[MAXSIZE];
int sum[MAXSIZE];//sum[i]代表sum[i]到sum[i]的上一个节点的权值

int FIND_X(int x)//路径压缩+求解沿路权值之和 
{
	if(x!=parent[x]){
		int i=parent[x];
		parent[x]=FIND_X(parent[x]);//路径压缩 
		sum[x]+=sum[i];//沿路权值之和 
	}
	return parent[x];
}
int main()
{
	int ans;
	int n,m;
	while(cin>>n>>m){
	for(int i=0;i<=n;i++){//初始化 
		parent[i]=i;
		sum[i]=0;
	}
	ans=0;
	while(m--)
	{
		int begin,end,value;
		cin>>begin>>end>>value;
		begin--;//闭区间展开成左开右闭区间
		int x=FIND_X(begin);
		int y=FIND_X(end);
		if(x==y){
			if(sum[begin]-sum[end]!=value){
				ans++;
			}
		}else{
			parent[x]=y;//更新根节点 
			sum[x]=value+sum[end]-sum[begin];//求出x的根节点到end的权值 
		} 
	}
	cout<<ans<<endl;
}
} 

重点是这个判断也是全文画龙点睛之处,下面进行讲解:

if(x==y){
	if(sum[begin]-sum[end]!=value)
				ans++;
	}else{
			parent[x]=y;//更新根节点 
			sum[x]=value+sum[end]-sum[begin];//求出x的根节点到end的权值 
} 

根据前面2组样例得到如下图:

当第2组样例1 3 32输入后,得到如下图

那么可以得到sum[10]的计算公式sum[x]=value+sum[end]-sum[begin],如果这个图还不好理解那么把这个图变化一下:

我们求的也就是px到py的值,根据传递性可以知道px=value+sum[end]-sum[begin]
我再把值带进去画个图:

这里的py实际上是3自己,因为3还没有和任何元素合并,不过为了一眼就能看出表达式才这么画的。
当两个元素的根节点相同时,只需要判断sum[begin]-sum[end]是不是等于value就能知道题目中错误的个数了。
画个图:(根据前面4组样例)


以上就是全部了,可能写的不是很好有什么意见可以评论交流,这个题目我认为出的非常好,教会了我许多东西,比如将并查集离散化,(因为我之前都是按照父亲存储的,原来根据题目的要求可以去掉一些没必要的东西,可以,很不错),还学会的路径压缩+带权并查集的处理(合并那里也需要出来)。