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 }
hdu 3038 How Many Answers Are Wrong
原文:https://www.cnblogs.com/greenaway07/p/11203452.html