HDU - 3038How Many Answers Are Wrong (带权并查集--权为区间和)
Posted wx62a8776062513
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 3038How Many Answers Are Wrong (带权并查集--权为区间和)相关的知识,希望对你有一定的参考价值。
题干:()
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 FFs 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 doesnt 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 doesnt 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.
Whats 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 cant 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. Its 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次询问,给出a到b区间的总和(左右均包含在内),问这n次给出的总和中有几次是和前面已近给出的是矛盾的。其实问矛盾的已经很典型了就是并查集可做。。。
解题报告:
此题的权值为区间的和,所以合并区间(的端点)的同时将区间的和维护一下即可。对于A~B之间的和是S,其实可以理解成B比A-1大S,所以代码中要将区间左端点-1;
AC代码:
const int MAX = 200000 + 5;
using namespace std;
int n,m;
int u,v;
int ans;
int f[MAX];
int w[MAX];
int sum;
int getf( int v)
if(v == f[v] ) return v;//一方面是为了递归函数的出口用的,另一方面加深一下理解,这里是指既然他已经是根节点了,那就不需要更新权值w和父节点f了,所以需要返回。并查集里面还是有很多道道的啊!!有很多更深层的理解和完美的思想在里面,内核思想和能解决的问题应该还远不止这些。 需要以后在做题中慢慢发掘。
int tmp = getf(f[v]);
w[v] = w[v] + w[f[v] ] ;
f[v]=tmp;//或f[v] = getf(f[v])
return f[v];
bool merge(int u,int v,int sum)
int t1=getf(u);
int t2=getf(v);
if(t1 == t2)
return w[v ]-w[u ] == sum;
if(t1!=t2)
f[t2]=t1;//(a--)w[a]表示a到0的和,w[b]表示a+1到b的和;
w[t2]=w[u]-w[v]+sum;//w[t2]表示t1,t2的距离;
return true ;//即 如果当前他俩并不在一个集合当中,说明这一段区间并没有被记录过,所以一定推断不出错误所以一定会返回1,也就是说判断出错误当且仅当他俩已经在一个集合当中了。也就是上面那个if所表达的东西
void init()
for(int i = 0; i<=MAX; i++) //又犯这种错误!!!就初始化到n?!!?!??
w[i] = 0;
f[i] = i;//
int main()
int m,n;
while(~scanf("%d %d",&n,&m) )
init();
ans=0;
while(m--)
scanf("%d %d %d",&u,&v,&sum);//只有输入过u和v的值才能做出判断,对这个区间内的值我们都不能做出判断,所以这题考虑并查集。
u--;//区间(0,b)分为(0,u-1)和(u,v);
if(!merge(u,v,sum) ) ans++;
printf("%d\\n",ans);
return 0 ;
总结:
1.通过做此题,加深了对并查集用处的理解,算是真正有了并查集的思想。
2. 并查集判断的题型中bool型的merge函数很常用。
以上是关于HDU - 3038How Many Answers Are Wrong (带权并查集--权为区间和)的主要内容,如果未能解决你的问题,请参考以下文章
hdu-3038 How Many Answers Are Wrong[并查集]
HDU3038 How Many Answers Are Wrong —— 带权并查集
HDU-3038-How Many Answers Are Wrong
HDU 3038 How Many Answers Are Wrong