HDU 3038 how Many Answers Are wrong? : Take the right and check the collection

Source: Internet
Author: User
Tags integer

Link:

http://acm.hdu.edu.cn/showproblem.php?pid=3038

Topic:

Problem Description

TT and FF are ... friends. Uh ... very very good friends-________-b FF is-a bad boy, and he's 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 inclu sively). After that, FF would ask TT what the sum of the subsequence he chose. The next, TT'll 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 in all. To punish FF, she often tells FF the wrong answers on purpose. The bad boy isn't a fool man. FF detects some answers are incompatible. Of course, these are contradictions make it difficult to calculate the sequence. However, TT is a nice and lovely girl. She doesn ' t have the heart to is hard on FF. To save time, she guarantees this answers are all right if there is no logical mistakes indeed. What ' More, if FF finds a answer to is wrong, he'll ignore it when judging next answers. But there is so many questions that poor FF can ' t make sure whether the current AnsweR is right or wrong in a moment. So him decides to write a program to the him with this matter. The program would receive a series of questions from FF together with the answers FF has received from TT. The aim is answers are wrong. Only by ignoring the wrong answers can FF work out the entire sequence of integers. Poor FF has no time to does this job. And now it 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 this sum from Ai to Bi is Si. It ' s guaranteed that 0 < Ai <= Bi <= N. You can assume this any sum of the subsequence is fit in 32-bit integer.

Output

More Wonderful content: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

A single line with an 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

Analysis and Summary:

and Hdu 3047 Zjnu Stadium (with the right to check the collection) the same problem.

Code:

#include <cstdio>  
    
const int N = 200005;  
int n,m,f[n],rank[n];  
    
inline void init () {for  
    (int i=0; i<=n; ++i)  
        f[i]=i,rank[i]=0;  
}  
int find (int x) {  
    if (x==f[x]) return f[x];  
    int fa=f[x];  
    F[X] = find (f[x]);  
    RANK[X] + = Rank[fa];  
    return f[x];  
}  
BOOL Union (int x,int y,int c) {  
    int a=find (x), B=find (y);  
    if (a==b) {  
        if (Rank[x]+c!=rank[y]) return false;  
        return true;  
    }  
    F[B] = A;  
    RANK[B] = rank[x]-rank[y]+c;  
    return true;  
}  
int main () {  
    int a,b,c;  
    while (~SCANF ("%d%d", &n,&m)) {  
        int cnt=0;  
        Init ();  
        for (int i=0; i<m; ++i) {  
            scanf ("%d%d%d", &a,&b,&c);  
            if (! Union (A-1,b,c)) {  
                ++cnt  
            }  
        }  
        printf ("%d\n", CNT);  
    }  
    return 0;  
}

Author: csdn Blog shuangde800

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.