日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【HDU - 3038】How Many Answers Are Wrong (带权并查集--权为区间和)

發(fā)布時(shí)間:2023/12/10 编程问答 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【HDU - 3038】How Many Answers Are Wrong (带权并查集--权为区间和) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題干:()

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 1

Sample Output

1

題目大意:

? ? ? ?有n次詢問,給出a到b區(qū)間的總和(左右均包含在內(nèi)),問這n次給出的總和中有幾次是和前面已近給出的是矛盾的。其實(shí)問矛盾的已經(jīng)很典型了就是并查集可做。。。

解題報(bào)告:

? ?此題的權(quán)值為區(qū)間的和,所以合并區(qū)間(的端點(diǎn))的同時(shí)將區(qū)間的和維護(hù)一下即可。對于A~B之間的和是S,其實(shí)可以理解成B比A-1大S,所以代碼中要將區(qū)間左端點(diǎn)-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;//一方面是為了遞歸函數(shù)的出口用的,另一方面加深一下理解,這里是指既然他已經(jīng)是根節(jié)點(diǎn)了,那就不需要更新權(quán)值w和父節(jié)點(diǎn)f了,所以需要返回。并查集里面還是有很多道道的啊!!有很多更深層的理解和完美的思想在里面,內(nèi)核思想和能解決的問題應(yīng)該還遠(yuǎn)不止這些。 需要以后在做題中慢慢發(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 ;//即 如果當(dāng)前他倆并不在一個集合當(dāng)中,說明這一段區(qū)間并沒有被記錄過,所以一定推斷不出錯誤所以一定會返回1,也就是說判斷出錯誤當(dāng)且僅當(dāng)他倆已經(jīng)在一個集合當(dāng)中了。也就是上面那個if所表達(dá)的東西 }} 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的值才能做出判斷,對這個區(qū)間內(nèi)的值我們都不能做出判斷,所以這題考慮并查集。 u--;//區(qū)間(0,b)分為(0,u-1)和(u,v); if(!merge(u,v,sum) ) ans++;}printf("%d\n",ans);} return 0 ; }

總結(jié):

? ? ?1.通過做此題,加深了對并查集用處的理解,算是真正有了并查集的思想。

? ? ?2. 并查集判斷的題型中bool型的merge函數(shù)很常用。

? ?

總結(jié)

以上是生活随笔為你收集整理的【HDU - 3038】How Many Answers Are Wrong (带权并查集--权为区间和)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。