How Many Answers Are Wrong
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4355 Accepted Submission(s): 1664
Problem Description
TT 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)
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).
![](https://i-blog.csdnimg.cn/blog_migrate/956a59c85e195b555e72de064fd5a563.jpeg)
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.
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.
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.
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
Recommend
gaojie
题目大意:现有一串数组,给定Ai, Bi and Si.我们只能得知数组从ai到bi的和为Si,但可能给定的条件与往前不一致(即错误信息),求最终多少个错误信息 。
本题与poj食物链有点相似,但是有所变化。
首先我们也是一棵树中存放有关系的节点(即任何两节点间的项数和都能确定),为此,我们需要改变输入的数,用前开后闭。如输入1,10,10 、 5,10,5。我们存把0号与10号节点在一棵树中,记10号节点为0号节点的父亲,0号节点的权值为10(项数和),同理处理5,10,5。这样当我们再次输入1,4,5时,因为有一颗树中存在0、4号节点,直接用0号的权值减去4号的权值,之前在查找0,4号节点的根节点时,我们进行权值压缩,他们都为根节点的孩子节点。
所以我觉得难点在于把坐闭右闭的区间化为左开右闭的区间。左闭右开也行,目的就是满足数组项数和区间间的关系。
题目大意:现有一串数组,给定Ai, Bi and Si.我们只能得知数组从ai到bi的和为Si,但可能给定的条件与往前不一致(即错误信息),求最终多少个错误信息 。
本题与poj食物链有点相似,但是有所变化。
首先我们也是一棵树中存放有关系的节点(即任何两节点间的项数和都能确定),为此,我们需要改变输入的数,用前开后闭。如输入1,10,10 、 5,10,5。我们存把0号与10号节点在一棵树中,记10号节点为0号节点的父亲,0号节点的权值为10(项数和),同理处理5,10,5。这样当我们再次输入1,4,5时,因为有一颗树中存在0、4号节点,直接用0号的权值减去4号的权值,之前在查找0,4号节点的根节点时,我们进行权值压缩,他们都为根节点的孩子节点。
所以我觉得难点在于把坐闭右闭的区间化为左开右闭的区间。左闭右开也行,目的就是满足数组项数和区间间的关系。
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
#include<vector>
using namespace std;
int fa[200010];
int sum[200010];
int Find(int x){
if(x != fa[x]){
int tmp = fa[x];
fa[x] = Find(fa[x]);
sum[x] += sum[tmp];
}
return fa[x];
}
int main()
{
int i,j,k,n,m,cnt;
int da,db;
int a,b,val;
while(scanf("%d%d",&n,&m)!=EOF){
for(i=0;i<=n;i++)
{
fa[i] = i;
sum[i] = 0;
}
cnt = 0;
for(i=0;i<m;i++){
scanf("%d%d%d",&a,&b,&val);
a-=1;
da = Find(a);
db = Find(b);
if(da != db){
fa[db] = da;
sum[db] = sum[a] - sum[b] - val;
}
else {
if(sum[a] - sum[b]!=val)
cnt++;
}
}
printf("%d\n",cnt);
}
return 0;
}