How ManyAnswers Are Wrong
TimeLimit: 2000/1000 MS (Java/Others) Memory Limit:32768/32768 K (Java/Others)
Total Submission(s): 12756 Accepted Submission(s): 4544
ProblemDescription
TT and FF are ... friends. Uh... very verygood 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 ofintegers-_-!!(bored).
Then, FF can choose a continuous subsequence from it(for example thesubsequence from the third to the fifth integer inclusively). After that, FFwill ask TT what the sum of the subsequence he chose is. The next, TT willanswer FF's question. Then, FF can redo this process. In the end, FF must workout the entire sequence of integers.
Boring~~Boring~~a very very boring game!!! TT doesn't want to play with FF atall. 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. Ofcourse, 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 onFF. To save time, she guarantees that the answers are all right if there is nological mistakes indeed.
What's more, if FF finds an answer to be wrong, he will ignore it when judgingnext answers.
But there will be so many questions that poor FF can't make sure whether thecurrent answer is right or wrong in a moment. So he decides to write a programto help him with this matter. The program will receive a series of questionsfrom FF together with the answers FF has received from TT. The aim of thisprogram is to find how many answers are wrong. Only by ignoring the wronganswers can FF work out the entire sequence of integers. Poor FF has no time todo this job. And now he is asking for your help~(Why asking trouble forhimself~~Bad boy)
Input
Line 1: Two integers, N and M (1 <= N<= 200000, 1 <= M <= 40000). Means TT wrote N integers and FF askedher M questions.
Line 2..M+1: Line i+1 contains three integer: Ai, Bi and Si. Means TT answeredFF 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.
Output
A single line with a integer denotes howmany answers are wrong.
SampleInput
10 5
1 10 100
7 10 28
1 3 32
4 6 41
6 6 1
SampleOutput
1
Source
2009Multi-University Training Contest 13 - Host by HIT
Recommend
gaojie | Wehave carefully selected several similar problems for you: 3033 3035 3036 3037 3031
算法实现:
题目描述:某一系列三元组x,y,c,表示序列中ax+ax+1+...+ay=c。但是其中有一些是错误的,也就是与前面的信息出现了冲突,比如给了1,2,6和3,4,5接着却给了1,4,100。找出所有错误的三元组的数量
跟这题思想一样
思路:用fa[i]记录父节点,dis[i]记录与父节点的差值,如果x与y在不同的集合中则可以自由合并,如果在同一集合中那就有可能出现矛盾。判断d[y]-d[x]==c是否成立,成立的话自然只需无视,不成立的话就表明出现矛盾。注意Find路径压缩查找当中节点到根结点路径上的每一个点除了fa要修改,dis也要累计。另外,由于题中给的区间都是全闭的,会出现a=b的情况,为了方便起见,记录区间时变为左开右闭,即a--
代码实现:
#include<bits/stdc++.h>
using namespace std;;
#define N 200005
int fa[N],dis[N];
int find(int x)
{
if(x==fa[x])
return x;
int t=fa[x];
fa[x]=find(fa[x]);
dis[x]+=dis[t]; //节点a到根的距离
return fa[x];
}
int main()
{
int n,m;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(int i=1;i<=n;i++)
{fa[i]=i;dis[i]=0;}
int ans=0;
for(int i=1;i<=m;i++)
{
int a,b,x;
scanf("%d%d%d",&a,&b,&x);
a=a-1;
int r1=find(a),r2=find(b);
if(r1==r2)
{
if(dis[b]-dis[a]!=x) ans++;
}
else
{
fa[r2]=r1;
dis[r2]=dis[a]+x-dis[b];//求r2与父亲节点的距离
}
}
printf("%d\n",ans);
}
return 0;
}