HDU 3038 how many answers are wrong (and query the set path compression)

Source: Internet
Author: User
How many answers are wrong

Time Limit: 2000/1000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)

Total submission (s): 1609 accepted submission (s): 646

Problem descriptiontt and FF are... friends. Uh... 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 shocould 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 nth integer within sively ). 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 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 empty 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
Answers FF has received ed 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 51 10 1007 10 281 3 324 6 416 6 1

Sample output

1

There are n numbers and M pieces of information, and none of them contain 3 numbers a B c, indicating the sum of numbers A to B is C.
If article I information does not conflict with the previous I-1 information, it is considered true, otherwise it is false. Ask the number of false information entries.

Train of Thought: and query set path compression

How can I use and check the solution? First, do a little processing and set --. Why? The question is the sum of the values in the closed range [a, B ].

Change it to a semi-open and semi-closed interval (A --, B]. The semi-open and semi-closed intervals have excellent properties: (a, B] + (B, c] = (a, c]

Let's look at this m piece of information. How can we interpret A, B, and C? We can interpret it as: A is related to B, and their relationship has a weight value c

Store the relationship in the form of query set. The relationship weights between elements in the same set are either known or can be calculated.

The reason is very simple (a, B] + (B, c] = (A, C]. Then, when a new piece of information about AI, Bi, and CI comes, judge whether AI and Bi are in a collection. If not, place AI and Bi

If yes, determine whether Ci is equal to the relational weights of AI and Bi.

Now, the general idea is clear. The remaining question is, how can we maintain the relationship weight? It is obviously troublesome to deploy the two-to-one algorithm for data storage, and the complexity of the time space is not allowed.

These weights are based on the relationship and need to be checked. Create a weight [] array. weight [a] indicates the link weight between A and its current boss per [.

If A and B are related, the weights of the relations between A and B are equal to those of weight [B]-weight [a] (B>)

From: http://blog.csdn.net/Magic_insky/article/details/9904469

Import Java. io. *; import Java. util. *; public class main {static int n, m; static int Patten [] = new int [200002]; static int sum [] = new int [1000000]; static int num; public static void main (string [] ARGs) {consumer SC = new consumer (New bufferedinputstream (system. in); While (SC. hasnext () {n = SC. nextint (); M = SC. nextint (); // check the set initialization for (INT I = 1; I <= N; I ++) {Patten [I] = I;} arrays. fill (sum, 0); num = 0; For (INT I = 0; I <m; I ++) {int A = SC . Nextint (); int B = SC. nextint (); int c = SC. nextint (); A --; // The answer to the question is a closed interval [a, B]. What we need is an open interval (a, B). Therefore, we need to subtract Union (A, B, c);} system. out. println (Num) ;}/// check the set and static void Union (int A, int B, int c) {int Pa = find (); int Pb = find (B); If (Pa = Pb) {If (sum [B]-sum [a])! = C) num ++;} else {Patten [Pb] = PA; sum [Pb] = sum [a]-sum [B] + C ;}} // query the static int find (int x) {If (x = Patten [x]) return X; int T = Patten [x]; patten [x] = find (t); // path compression sum [x] + = sum [T]; return Patten [x] ;}}

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.