题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=3038
题目
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
题意
给定M组区间[x,y]和区间的值之和v。假定未与之前的数据矛盾的都为正确,求错误数据的总数。
分析
带权并查集的经典应用。
并查集的本质是森林,每棵树代表一个集合。给树上的边加一个权值,sum[u]表示结点u到其父结点fu=par[u]的距离。再将这个距离与题目联系起来,sum[u]=a[fu]+a[fu+1]+a[fu+2]+…+a[u].数组a为满足题目要求的正确数组。
判断“逻辑矛盾”与否可以根据森林和边权。
如果x和y在一棵树上,那么它们到树根p的距离就必须要满足 :x到树根的距离 + x到y的距离 = y到树根的距离。否则,逻辑矛盾。
而求x到树根的距离,可以在查找根的过程中求出,即x到父结点的距离+x的父节点到x的父结点的父结点的距离+…+某个结点到根的距离 = x到树根的距离。在路径压缩时sum[x]+=sum[fx],par[x]=find(par[x]),最后x的父结点就是根,sum[x]就是x到根的距离。
对于合并操作,即将两棵树合并成一棵树。每给一组x y v,y结点到x的结点距离为v,为了满足v=a[x]+a[x+1]+a[x+2]+…+a[y],需要将x对应树中的编号为x-1.这样结点x-1到结点y的距离就对应到了v。你第5位到第5位的和为10,不可能5号结点和5号结点形成自环吧。(自环就不是树了)
AC代码
//并查集的本质是一个森林,每棵树代表一个集合,树根为集合的代表元。
#include <cstdio>
#include <cstring>
using namespace std;
const int maxn=2e5+100;
int N,M;
int par[maxn];
int sum[maxn];//sum[i]=a[par[i]]+a[par[i]+1]+...a[i];i到父结点的距离
void init()
{
for(int i=0;i<=N;i++) par[i]=i;
memset(sum,0,sizeof(sum));
}
int find(int x)
{
if(x!=par[x])
{
int tmp=par[x];
par[x]=find(par[x]);//路径压缩,将x的父结点改成x的父节点的父结点
sum[x]+=sum[tmp];//x结点到其新父结点的距离=x到原父结点距离+x的原父结点到x的原父结点的父结点的距离
}
return par[x];
}
int ans;
void unit(int x,int y,int v)
{
int fx=find(x),fy=find(y);
if(fx==fy)
{
if(sum[x]+v!=sum[y]) ans++;
return ;
}
if(fx<fy)//以结点编号小的作为父结点
{
par[fy]=fx;
sum[fy]=sum[x]+v-sum[y];
}
else
{
par[fx]=fy;
sum[fx]=sum[y]-v-sum[x];
}
}
int main()
{
while(~scanf("%d%d",&N,&M))
{
init();
ans=0;
while(M--)
{
int x,y,v;
scanf("%d%d%d",&x,&y,&v);
unit(x-1,y,v);
}
printf("%d\n",ans);
}
return 0;
}