Hangzhou Electric HDU ACM 1698 Just a Hook (segment tree interval update delay mark)

Source: Internet
Author: User

Welcome to the "Love Programming" of the college Entrance Examination--the computer College of Hangzhou University
Just a HookTime limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 20889 Accepted Submission (s): 10445

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 to several consecutive metallic sticks which is of the same length.

Now Pudge wants to do some operations on the hook.

Let us number the consecutive metallic sticks of the hooks from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver s Ticks 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 of the 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 performing the operations.
Consider the original hook is a 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 is no more than cases.
For each case, the first line contains a integer N, 1<=n<=100,000, which is the number of the sticks of Pudge ' s MEA T Hook and the second line contains a 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:c Hange the sticks numbered from X to Y into the metal kind Z, where z=1 represents the Cupreous kind, z=2 represents the SI Lver Kind and z=3 represents the golden kind.

Outputfor, 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.

#include <iostream> #include <sstream> #include <algorithm> #include <cstdio> #include < string.h> #include <cctype> #include <string> #include <cmath> #include <vector> #include <stack> #include <queue> #include <map> #include <set>using namespace std;const int inf=100000;    The int val[inf+1];struct node//struct represents the node {int total;    int left;    int right;    int mark;//was last updated} tree[inf<<2];int create (int root,int left, int. right)//build {tree[root].left=left;    tree[root].mark=0;    Tree[root].right=right;    if (left==right) return tree[root].total=val[left];    int A, b,middle= (left+right) >>1;    A=create (Root<<1,left,middle);    B=create (Root<<1|1,middle+1,right); return tree[root].total=a+b; Assigning total to total during backtracking}void update_mark (int root) {if (Tree[root].mark) {//assumes that the delay is marked and that it is necessary to find the updated segment in the descendants of root. No matter if you find it or not, the node will "implement" the total value of this node.        and causes the delay tag to move down. Tree[root].total=tree[root].mark* (tree[root].right-tREE[ROOT].LEFT+1);        if (tree[root].left!=tree[root].right) tree[root<<1].mark=tree[root<<1|1].mark=tree[root].mark;    tree[root].mark=0; }}int Calculate (int root,int left,int right) {Update_mark (root);//recursive to each node to verify if there is a delay mark if (tree[root].left>right| |    Tree[root].right<left) return 0;    if (left<=tree[root].left&&tree[root].right<=right) return tree[root].total;    int A, B;    A=calculate (Root<<1,left,right);    B=calculate (Root<<1|1,left,right); return a+b;}    int update (int root,int left,int right,int val) {Update_mark (root); if (tree[root].left>right| |    Tree[root].right<left) return tree[root].total;        if (tree[root].left>=left&&tree[root].right<=right) {tree[root].mark=val;    Return tree[root].total=val* (tree[root].right-tree[root].left+1);    } int A, b;    A=update (Root<<1,left,right,val);    B=update (Root<<1|1,left,right,val); Return Tree[root].total=a+b;}    int main () {int T;    cin>>t;    int c=0;        while (t--) {int n,q,x,y,z;        cin>>n;        for (int i=1; i<=n; i++) val[i]=1;        cin>>q;        Create (1,1,n);            for (int i=0; i<q; i++) {scanf ("%d%d%d", &x,&y,&z);        Update (1,X,Y,Z);    } printf ("Case%d:the total value of the hook is%d.\n", ++c,tree[1].total); } return 0;}

Hangzhou Electric HDU ACM 1698 Just a Hook (segment tree interval update delay mark)

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.