hdu 3038 How Many Answers Are Wrong

T 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) 

InputLine 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. 
OutputA 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

这里借用一下https://blog.csdn.net/sunmaoxiang/article/details/80959300的图
为什么sum[gx]=sum[y]-sum[x]+s,我感觉用这张图,从fy出发沿y、x到达fx,其中逆向为负数,完全按照理解向量的方式就明白了
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <stdio.h>
 5 #include <cstring>
 6 #include <string>
 7 #include <cstdlib>
 8 #include <queue>
 9 #include <stack>
10 #include <set>
11 #include <vector>
12 #include <map>
13 #include <list>
14 #include <iomanip>
15 #include <fstream>
16 
17 using namespace std;
18 typedef long long ll;
19 int fa[200007],sum[200007];
20 //一开始还以为是赤裸裸的em打表题。结果是审题错误orz,并不知道区间内具体的数字啊。。。 
21 int cnt;
22 int get(int x)
23 {
24     int root;
25     if(fa[x]!=x)
26     {
27         root=fa[x];
28         fa[x]=get(fa[x]);
29         sum[x]+=sum[root];
30     }
31 
32     return fa[x];
33 }
34 
35 int main()
36 {
37     int n,m,x,y,s;
38     while(scanf("%d%d",&n,&m)!=EOF)
39     {
40         cnt=0;
41         for(int i=0;i<=n;++i)
42         {
43             fa[i]=i;
44             sum[i]=0;
45         }
46         while(m--)
47         {
48             scanf("%d%d%d",&x,&y,&s);
49             x--;//因为它inclusive端点 
50             int gx=get(x),gy=get(y);
51             if(gx==gy&&(sum[x]-sum[y])!=s)//区间[x,y]的和就可以写成sum[x] - sum[y] 
52                 cnt++;
53             else{//不知道其相互关系,默认说法是对的,作为后来的参考 
54                 fa[gx]=gy;
55                 sum[gx]=sum[y]-sum[x]+s;//注意区分gx x gy y 
56             }
57         } 
58         
59         printf("%d\n",cnt);
60     }
61 
62     return 0;
63 }
View Code

 

posted @ 2019-07-17 20:23  鹤花之歌  阅读(142)  评论(0编辑  收藏  举报