题意:给出一些区间[l,r]的和为S,问有多少个是矛盾的
思路:对给出的区间[l,r]将左边减一变成(l-1,r]合并,把数值小的当做根节点更新即可,有一点不懂的是只会用递归来更新,用while循环一直wa...
#include <cstdio>
#include <queue>
#include <cstring>
#include <iostream>
#include <cstdlib>
#include <algorithm>
#include <vector>
#include <map>
#include <string>
#include <set>
#include <ctime>
#include <cmath>
#include <cctype>
using namespace std;
#define maxn 200005
#define LL long long
int cas=1,T;
int pre[maxn];
int sum[maxn];
int Find(int x)
{
if (x!=pre[x])
{
int r = pre[x];
pre[x]=Find(pre[x]);
sum[x]+=sum[r];
}
return pre[x];
}
int n;
void init()
{
for (int i = 0;i<=maxn;i++)
{
pre[i]=i;
sum[i]=0;
}
}
int main()
{
int m;
while (scanf("%d%d",&n,&m)!=EOF)
{
init();
int ans = 0;
for (int i = 0;i<m;i++)
{
int l,r,s;
scanf("%d%d%d",&l,&r,&s);
l--;
int ra =Find(l),rb=Find(r);
if (ra==rb)
{
if (sum[r]-sum[l]!=s)
ans++;
}
else
{
pre[rb]=ra;
sum[rb]=sum[l]-sum[r]+s;
}
}
printf("%d\n",ans);
}
//freopen("in","r",stdin);
}