How Many Answers Are WrongTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15260 Accepted Submission(s): 5361 Problem Description TT and FF are ... friends. Uh... very very good friends -________-bFF 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.
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
Source 2009 Multi-University Training Contest 13 - Host by HIT
Recommend gaojie | We have carefully selected several similar problems for you: 3033 3035 3036 3037 3031 |
【题意】
给出区间[1,n],下面有m组数据,l r v区间[l,r]之和为v,每输入一组数据,判断此组条件是否与前面冲突 ,最后输出与前面冲突的数据的个数。
【解题思路】
乍一看好像和并查集没有什么关系,怪我对并查集还是理解得不够深入……
分享一篇超级棒的题解:https://www.cnblogs.com/liyinggang/p/5327055.html
这题主要是关于向量偏移,可以直接找到根节点与子节点的关系。
这题我们利用一个sum[]数组保存从某点到其祖先节点距离。
1.从上图我们可以看出,当roota!=rootb时 如果将roota并入rootb,那么是不是 roota->rootb = b->rootb - b->roota
然后我们可以知道 b->roota = a->roota - a->b
所以最后可以推出 roota ->rootb = b->rootb + a->b - a->roota
而roota的根节点是rootb,所以 roota->rootb = sum[roota],然后依次推出得到 sum[roota] = -sum[a]+sum[b]+v
2.如果roota==rootb 是不是 a和b的根节点已经相同了?所以我们只要验证 a->b是否与题中的长度一致了。
所以 a->b = a->root - b->root
然后得到表达式 v = sum[a]-sum[b] (一定要记住这里的sum都是相对于根节点的,sum的更新在路径压缩的时候更新了)
【代码】
#include<bits/stdc++.h>
using namespace std;
const int maxn=200005;
int pre[maxn],sum[maxn];
int findroot(int x)
{
if(x!=pre[x])
{
int t=pre[x];
pre[x]=findroot(pre[x]);
sum[x]+=sum[t];
}
return pre[x];
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
int ans=0;
memset(sum,0,sizeof(sum));
for(int i=0;i<=n;i++)
pre[i]=i;
while(m--)
{
int a,b,s;
scanf("%d%d%d",&a,&b,&s);
a--;//[a,b] = [root,b]-[root,a-1],所以a--,所以前面pre数组要从0开始初始化
int fa=findroot(a);
int fb=findroot(b);
if(fa!=fb)
{
pre[fa]=fb;
sum[fa]=-sum[a]+sum[b]+s;
}
else
{
if(sum[a]-sum[b]!=s)
ans++;
}
}
printf("%d\n",ans);
}
return 0;
}