Hdu-3038-how Many Answers Are wrong

Source: Internet
Author: User
 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 thAt the 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 would be so many questions this poor FF can ' t make sure whether the current answer are 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
A single line with an integer denotes how many answers are wrong.

Sample Input

5
1
7
1 3
4 6 6 6 1

Sample Output

1

the words : give you n number, m interval, each interval represents different values, to find out that there are several intervals to give the value is wrong.
Solving Ideas : Expand and check the set , just look at the topic first feeling is to use line lines tree to do, but in and check the set inside. It took a long time to understand the puzzle. Let's start with the assumption and the merging of the collection, we want to select a small number of one end of the merge. The

assumes that the root of a is x, the root of B is Y, and
if x < Y, so you have to put Y on top of the reset a (note that the following is the calculation of the vector)
Yx = bx-by;
bx = Ax-ab;
So yx = ax-ab-by;
If x > y, so you want to put Y on top of the reset a (note that the vector is calculated below)
xy = BY-BX;
bx = Ax-ab;
So xy = By-ax + AB
If x = = y

Then ax-by = AB (this is used to determine which intervals in the M interval are incorrect)

#include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace
Std
const int MAXN = 200000 + 10;
int PAR[MAXN];

int SUM[MAXN];
    int find (int x) {if (x = = Par[x]) {return x;
    int t = par[x];
    PAR[X] = find (par[x]);
    SUM[X] + = sum[t];
return par[x];
    BOOL Unite (int x, int y, int v) {int rootx = find (x);
    int RootY = find (y);
        if (Rootx = = RootY && sum[x]-sum[y]!= v) {return false;//description is error interval} if (Rootx > RootY) {
        Par[rootx] = RootY;
    Sum[rootx] = v + sum[y]-sum[x];
        else {Par[rooty] = Rootx;
    Sum[rooty] = sum[x]-v-sum[y];
return true;
        int init (int n) {for (int i = 0; I <= N; i++) {par[i] = i;
    Sum[i] = 0;
    int main () {int n, m;
        while (~SCANF ("%d%d", &n, &m)) {init (n);
        int x, y, V;
        int ans = 0; for (int i = 0; i < m; i++){scanf ("%d%d%d", &x, &y, &v);
            x--;
            if (!unite (x, Y, v)) {ans++;
    } cout << ans << endl;
 }
}

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.