HDU-1698 (line tree _ interval updating_java handwriting)

Source: Internet
Author: User
Just a hook

Time Limit: 4000/2000 MS (Java/others) memory limit: 32768/32768 K (Java/Others)
Total submission (s): 11449 accepted submission (s): 5661


Problem descriptionin the game of Dota, Pudge's meat hook is actually the most horrible thing for most of the heroes. the hook is made up of several consecutive metallic sticks which are of the same length.

Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hook from 1 to n. for each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks or golden sticks.
The total value of the hook is calculated as the sum of values of N metallic sticks. More precisely, the value for each kind of stick is calculated as follows:

For each cupreous stick, the value is 1.
For each silver stick, the value is 2.
For each golden stick, the value is 3.

Pudge wants to know the total value of the hook after creating the operations.
You may consider the original hook is made up of cupreous sticks.


Inputthe input consists of several test cases. The first line of the input is the number of the cases. There are no more than 10 cases.
For each case, the first line contains an integer N, 1 <=n <= 100,000, which is the number of the sticks of Pudge's meat hook and the second line contains an integer Q, 0 <= q <= 100,000, which is the number of the operations.
Next Q lines, each line contains three integers x, y, 1 <= x <= Y <= N, Z, 1 <= z <= 3, which defines an operation: change the sticks numbered from X to Y into the metal kind Z, where z = 1 represents the cupreous kind, Z = 2 represents the silver kind and Z = 3 represents
The golden kind.


Outputfor each case, print a number in a line representing the total value of the hook after the operations. Use the format in the example.


Sample Input

11021 5 25 9 3
 


Sample output

Case 1: The total value of the hook is 24.
 


Source2008 "sunline Cup" national invitational contest


Recommendwangye

Depressed, the line segment tree written in Java times out, and I wrote it for one night, modified and modified it, Baidu and Baidu. I don't want to say more,

Java is relatively slow...

For data structures like this, I think Java is still very sad. I am saying that no one uses Java to submit questions. I think there should be a lot of proficient Java...

Therefore, we recommend that you write less algorithm questions in Java.

Then, one of our teams has exited ACM. Although it is not a team, I have a hard time trying to win, and I don't know what to do,

No one wants to continue, and no one will be confident. But it is not a solution to give up. I think that only one person insists that I will stick to it, and I don't just want to win the prize,

Of course, it is the best to win the prize. I learned from him because I like him (although I have no scores), but it does not affect my liking for him,

No one knows what the future looks like, so I insist on doing what I like. I plan to remove my notebook tomorrow...


Although it is too time-consuming, I suggest you paste the code for a long time.


Import Java. util. vertex; Class node {public int L; Public int R; Public int val; Public int sum; Public node () {val = 1; sum = 1 ;}} // This is a node model. An independent Java can only have one public class and act as an interface. However, during project development, it generally has one Java class, an independent class public class hdu1698 {public static final int maxn = 401111; // equivalent to the global variable public static node [] tree = new node [maxn] in C; // You must declare the size of the space to be given to him. This is similar to int A [100]. You cannot declare only one name. // you have to tell the system how much space you need, so the next new must exist. public static void push Up (int I) {tree [I]. sum = tree [I <1]. sum + tree [I <1 | 1]. SUM; // This is the sum up} public static void Pushdown (int I) {If (tree [I]. val! = 0) {// This is almost confusing to me! I keep thinking that non-0 in C is true, but in Java, you cannot use integer 1 as the true type of Boolean. but in essence, we have to know what we do to save trouble! Let's take! = 0: What's wrong? No matter whether the values returned by the Java and C logical expressions are true or false, you can't help but know .. crying .... // This is the essence of segment update. If the interval does not match, immediately place the Val value in the interval to the child, and then mark the confusion, that is, val = 0; tree [I <1]. val = tree [I <1 | 1]. val = tree [I]. val; tree [I <1]. sum = (tree [I <1]. r-tree [I <1]. L + 1) * tree [I <1]. val; // In fact, writing in this way is still very tedious. It is better to set a range endpoint and // that is, to transmit M = (R-l + 1 ); // M-(M> 1) on the left // m> 1 on the right; tree [I <1 | 1]. sum = (tree [I <1 | 1]. r-tree [I <1 | 1]. L + 1) * tree [I <1 | 1]. val; tree [I]. val = 0; // chaotic} public static void buildtree (int I, int L, int R) {tree [I] = new node (); // be sure to pay attention to the amount of memory you need to tell the system, but it is not enough. You have to copy the space. // otherwise, you just need, but not initialized. how do you use it ??? I gave you a piece of land, so you want to eat rice? Where do you eat rice ???? Tree [I]. L = L; tree [I]. R = r; If (L = r) {return;} int mid = (L + r)> 1; buildtree (I <1, L, mid ); buildtree (I <1 | 1, Mid + 1, R); pushup (I);} public static void Update (int I, int L, int R, int Val) {If (tree [I]. L = L & tree [I]. R = r) {tree [I]. val = val; tree [I]. sum = (R-l + 1) * tree [I]. val; return;} Pushdown (I); // if the preceding execution is not performed, it indicates that it is finished, and the interval is not completely matched. This interval must be disrupted. int mid = (tree [I]. L + tree [I]. r)> 1; if (r <= mid) {Update (I <1, L, R, Val);} else if (L> mid) {Update (I <1 | 1, L, R, Val) ;}else {Update (I <1, L, mid, Val ); update (I <1 | 1, Mid + 1, R, Val);} pushup (I);} public static void main (string [] ARGs) {partition input = new partition (system. in); int t; while (input. hasnext () {T = input. nextint (); For (int K = 1; k <= T; k ++) {int n = input. nextint (); buildtree (1, 1, n); int M = input. nextint (); For (INT I = 0; I <m; I ++) {int left = input. nextint (); int right = input. nextint (); int val = input. nextint (); Update (1, left, right, Val);} system. out. println ("case" + K + ": the total value of the hook is" + tree [1]. sum + ". ");}}}}

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.