How Many Answers Are Wrong HDU - 3038 disjoint-set weighting (path)

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) 

InputLine 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. 
OutputA 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 

that Italy: the size of the number of columns n, m times a data, each data series is given from the a-th to b-th and is c, ask how much data these data are
wrong.

Ideas: The number of each segment imagine a left segment of this number, the next number to the right, so to open the array size n + 1, with these disjoint-set to an ancestor, open a store these numbers into the array ancestor the distance. See in particular the code

Code:
 1 #include <cstdio>
 2 #include <fstream>
 3 #include <algorithm>
 4 #include <cmath>
 5 #include <deque>
 6 #include <vector>
 7 #include <queue>
 8 #include <string>
 9 #include <cstring>
10 #include <map>
11 #include <stack>
12 #include <set>
13 #include <sstream>
14 #include <iostream>
15 #define mod 998244353
16 #define eps 1e-6
17 #define LL Long Long
 18 is  #define INF 0x3f3f3f3f
 . 19  the using  namespace STD;
 20 is  
21 is  // ancestry stored fa corresponding index
 22  @ distance num stored at the ancestor node to the subscript 
23 is  int fa [ 200005 ], num [ 200005 ];
 24  // initialization 
25  void the init ( int n)
 26 is  {
 27      // because there are n line, then there are n + 1 points 
28      for ( int I = 0 ; I <= + n . 1 ; I ++ )
 29      {
 30         // ancestors of each point for their initial condition 
31 is          FA [I] = I;
 32          // each point where the ancestor in the initial distance of 0 
33 is          NUM [I] = 0 ;
 34 is      }
 35  }
 36  // found ancestor, and optimized compression path of 
37 [  int find ( int S)
 38 is  {
 39      // If the ancestor for themselves return 
40      IF (FA [S] == S)
 41 is      {
 42 is          return S;
 43 is      }
 44 is      // recursively to find ancestors 
45      int TEMP = find (FA [S]);
 46 is     // The distance from their ancestors to add to the ancestors ancestors ancestors 
47      NUM [S] + = NUM [FA [S]];
 48      // compression path optimization 
49      FA [S] = TEMP;
 50      // Returns ancestors 
51 is      return FA [S];
 52 is  }
 53 is  int main ()
 54 is  {
 55      int n-, m;
 56 is      the while (Scanf ( " % D% D " !, & n-, & m) = the EOF)
 57 is      {
 58          // initialization data 
59          the init (n-);
 60          int A, B, C;
 61 is          int ANS =0 ;
 62 is          for ( int I = 0 ; I <m; I ++ )
 63 is          {
 64              Scanf ( " % D% D% D " , & a, & b, & C);
 65              // plus one as from a to b is a line segment from the start point to the end point of the line segment b 
66              b ++ ;
 67              // find a and b are the furthest ancestor 
68              int X = find (a);
 69              int Y = find (b);
 70              // if not two ancestral equal distance from each point to the two merging ancestor 
71 is              IF (X! = Y)
 72              {
 73 is                  //Will raise an ancestor ancestral b and b is the ancestor of ancestors a ancestors 
74                  FA [Y] = X;
 75                  // as the ancestor b applied on a ancestors, it is a to b from ancestors of
 76                  @ b a distance a plus the distance a to the ancestors, or to the distance b b b plus ancestor ancestors ancestors a distance
 77                  // thus a distance b ancestor ancestors = b a distance a plus the distance b minus a distance ancestor b ancestors 
78                  NUM [Y] = NUM [a] + the C- NUM [b];
 79              }
 80              the else 
81              {
 82                  // b to b subtracting a distance ancestors ancestor is the distance to a distance from a to b
 83                  @ distance c is determined whether a to b
 84                  // not said this sentence is wrong c 
85                  iF (c! = NUM [B] - NUM [A])
 86                  {
87                      yrs ++ ;
88                  }
 89              }
 90          }
 91          printf ( " % d \ n " , year);
92      }
 93 }

 

Guess you like

Origin www.cnblogs.com/mzchuan/p/11597348.html