How Many Answers Are Wrong
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=3038
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.
BoringBoringa 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.
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
特别提醒
这题是多测
题意
有一个长度为N的序列(序列未知),和M条语句,形如x y z,即x位到y位的和为z。求有多少非法语句(非法语句即与前面的语句发生冲突)。
题解
有很多人说这是权值并查集,但也有少部分人说是种类并查集,我觉得权值并查集只是一种结构,思想是种类?其实我也不清楚,但是其实这些都不重要,关键是理解其内涵。
在做这题之前我推荐大家看我的另外两篇博客
1.https://www.cnblogs.com/mmmqqdd/p/11214529.html
2.https://www.cnblogs.com/mmmqqdd/p/11224929.html
感觉第一篇转化的思维还有点难,第二篇就是裸的权值并查集。
那么我们开始讲这题。
知道sum[1,3]和sum[4,5]那么就知道了sum[1,5],我们可以换一种想法,把sum[1,3]看成1->3的距离,题目也就变成了给你很多点的相对距离,为你冲突个数。
这样其实就变成了裸的权值并查集。
但是大家要注意每个点都是距离的即sum[1,1]是有值的,所以我们应该将sum改成左闭右开(当然左开右闭也可以),即给你x y z,应该是sum[x,y+1]=z;这里可以仔细想想,也不一定要一次想明白。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define INF 0x7f7f7f7f
#define N 400050
int n,m,fa[N],val[N];
template<typename T>void read(T&x)
{
ll k=0; char c=getchar();
x=0;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit(0);
while(isdigit(c))x=x*10+c-'0',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
int find(int x)
{
if (x==fa[x])return x;
int y=fa[x]; fa[x]=find(fa[x]);
val[x]+=val[y];
return fa[x];
}
bool merge(int x,int y,int z)
{
int fx=find(x),fy=find(y); if (fx==fy)return 0;
val[fx]=val[y]-val[x]+z;
fa[fx]=fy;
return 1;
}
void work()
{
int ans=0;
read(n); read(m);
for(int i=1;i<=n;i++)fa[i]=i,val[i]=0;
for(int i=1;i<=m;i++)
{
int x,y,z;
read(x); read(y); read(z);
y++;
if (merge(x,y,z))continue;
if (z!=val[x]-val[y])ans++;
}
printf("%d
",ans);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
while(1)work();
}