【HDU - 3038】How 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 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.?
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 1Sample Output
1題目大意:
? ? ? ?有n次詢問,給出a到b區間的總和(左右均包含在內),問這n次給出的總和中有幾次是和前面已近給出的是矛盾的。其實問矛盾的已經很典型了就是并查集可做。。。
解題報告:
? ?此題的權值為區間的和,所以合并區間(的端點)的同時將區間的和維護一下即可。對于A~B之間的和是S,其實可以理解成B比A-1大S,所以代碼中要將區間左端點-1;
AC代碼:
#include<iostream> #include<cstdio> #include<algorithm> 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 - 3038】How Many Answers Are Wrong (带权并查集--权为区间和)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 安卓最强芯第二代骁龙8加持 小米13曝光
- 下一篇: 中国恒大突然被申请清盘!有何影响?