HDU[3038] How Many Answers Are Wrong 【边带权并查集】

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.

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个数,有m次询问和回答,每次询问给出一个区间,获得这区间里数的和为s的回答,求共有多少次的回答是错误的,也就是该次回答和前面的信息矛盾。

分析:

这里使用并查集,因为并查集实际上是维护了一棵树,我们可以以区间的端点为点建立一个集合,他们的根就是能到达的最左端,如果都有相同的最左端那么就可以判断一下是否有矛盾产生。 

比如上面这个图, 我们已经知道了AB的长度和AC的长度,如果下面再来一个CB,我们就可以知道C的最左端是A,B的最左端也是A,那么就可以判断一个AC+CB的长度是不是等于AB的长度就可以了。

这里需要注意几个点:

1、 对区间[l, r]进行记录时,由于我们记录的是从根端点到本端点所有值的和,那么求区间的和就是sum[r]-sum[l-1](可以结合前缀和理解),实际上是对 (l-1, r]操作,所以左端点应该是l - 1。

2、 并查集中的树节点与节点之间的边我们给他一个权值,就是相邻节点表示的端点之间的元素和。

3、 在合并操作中,对我们需要更新sum[ffb](由于ffb作为子节点连接到了ffa上),动态更新的公式是sum[ffb] = sum[a - 1] - sum[b] + s,为了理解这个式子,我们进行如下讨论: 
-- 更新sum[ffb]的目的是维护被合并的树(ffb)相对于合并树(ffa)之间的势差,ffb树上的其他节点的sum值会在下一次find函数的调用过程中被更新。 
-- 可知sum[b]保存结点b与结点ffb(对应树的根)之间的势差;sum[a-1]保存结点a-1与结点ffa(对应树的根)之间的势差;s是更新信息中结点a-1与结点b之间的势差 

-- 所以sum[ffb]的值为从结点ffa到结点a-1(sum[a-1]),加上从结点a-1到结点b(s),最后减去从结点ffb到结点b(sum[b])

4、 注意这一题是多组输入。

具体解释见代码。

//#include <bits/stdc++.h>
#include <iostream>
#include <cstring>

using namespace std;

const int maxn=200005;

int fa[maxn],sum[maxn];
int n,m,ans;

int find(int x){
	if(fa[x]==x)  return x;
	int root=find(fa[x]);
	sum[x]+=sum[fa[x]];	//路径压缩过程中,边权值的更新 
	return fa[x]=root;
}

void merge(int a,int b,int s){
	int ffa=find(a-1),ffb=find(b);
	fa[ffb]=ffa;
	sum[ffb]=sum[a-1]+s-sum[b];
} 

void init(){
	for(int i=0;i<=n;i++){	//这里的初始化要注意 0 也要初始化,因为涉及到 a-1 
		fa[i]=i;
	}
	memset(sum,0,sizeof(sum));
}

int main(){
	int a,b,s;
	while(cin>>n>>m){
		ans=0;
		init();
		for(int i=1;i<=m;i++){
			cin>>a>>b>>s;
			int ffa=find(a-1),ffb=find(b);
			if(ffa==ffb){
				if(s!=sum[b]-sum[a-1])  ans++;	//错误的答案不去更新原本的信息 
			}
			else{
				merge(a,b,s);
			}
		}	
		cout<<ans<<endl;
	} 
	return 0;
} 
发布了30 篇原创文章 · 获赞 5 · 访问量 900

猜你喜欢

转载自blog.csdn.net/qq_42840665/article/details/102329431
今日推荐