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 N N and M ( 1 < = N < = 200000 , 1 < = M < = 40000 ) M (1 <= N <= 200000, 1 <= M <= 40000) M(1<=N<=200000,1<=M<=40000). Means TT wrote N N N integers and FF asked her M M M questions.
Line 2… M + 1 M+1 M+1: Line i + 1 i+1 i+1 contains three integer: A i Ai Ai, B i Bi Bi and Si. Means TT answered FF that the sum from A i Ai Ai to B i Bi Bi is S i Si Si. It’s guaranteed that 0 < A i < = B i < = N 0 < Ai <= Bi <= N 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个节点,以 ( s t a r t , e n d , s u m (start,end,sum (start,end,sum(区间总和) ) m )m )m个区间;我们需要判断的是有多少个假区间;
例如:
1
→
3
+
4
→
6
+
7
→
10
=
32
+
41
+
28
=
101
1\rightarrow 3+4\rightarrow 6+7\rightarrow 10=32+41+28=101
1→3+4→6+7→10=32+41+28=101;
与
1
→
10
=
100
1\rightarrow 10=100
1→10=100矛盾;所以有一处错误;
思路:不妨利用两个数组 f , g , f f,g,f f,g,f 数组记录每个子节点的根节点, g g g 数组记录每个节点到自己根节点的距离,根据上面的例子很容易想到用并查集。
如果 a a a 和 b b b 不属于统一父节点,这时把 a a a 合并到 b b b 上,这时要给 a a a 树的跟节点赋值,关键是附上一个什么值?由于 a a a 点和 b b b 点的权值 s u m [ a ] sum[a] sum[a] 和 s u m [ b ] sum[b] sum[b] 都是相对跟节点的距离,所以分析 a a a, b b b 之间的相对距离,可以得到 g [ g[ g[根节点 ] = g [ a ] − g [ b ] + c ] = g[a]-g[b]+c ]=g[a]−g[b]+c。 注意到这时,对 a a a 树,只跟新了跟节点的权值, 那么其它结点的跟,已经在查找的那一步里面实行了。
因为
g
[
]
g[]
g[] 保存的是到根节点的距离,由于根节点的权值改变了,所以递归中每个点到根节点的值也会改变
向量表示的公式:
g
[
(
g[(
g[(根节点
)
]
=
g
[
(
s
t
a
r
t
)
]
−
g
[
(
e
n
d
−
1
)
]
+
s
u
m
)]=g[(start)]-g[(end-1)]+sum
)]=g[(start)]−g[(end−1)]+sum;
代码如下:
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
const int maxx=200010;
int f[maxx],g[maxx];
void init()
{
memset(f,-1,sizeof(f));
memset(g,0,sizeof(g));
}
int getf(int v)
{
if(f[v]==-1)
return v;
int u=getf(f[v]);
g[v]+=g[f[v]];
return f[v]=u;
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
init();
int ans=0;
for(int i=0; i<m; i++)
{
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
a--;
int t1=getf(a);
int t2=getf(b);
if(t1!=t2)
{
f[t2]=t1;
g[t2]=g[a]-g[b]+c;
}
else if(g[b]-g[a]!=c)
ans++;
}
printf("%d\n",ans);
}
return 0;
}
实践是检验真理的唯一标准