HDU 1698 Just a Hook

Source: Internet
Author: User

Just a HookTime limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 23147 Accepted Submission (s): 11604


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.

Source2008 "Sunline Cup" National Invitational Contest
Recommendwangye
The title means that given a number N, the interval 1~n, the initial element of the interval is all 1, and then input x, Y, Z, which indicates that the element from interval x to Y becomes z, and the total interval is summed after operation.is the simple interval update, the set of templates is good.
#include <algorithm> #include <iostream> #include <cstdio> #include <cstring>using namespace STD; #define Lson l,m,rt<<1#define Rson m+1,r, (rt<<1) |1#define root 1,n,1#define Mid (l+r) >>1#define LL long longconst int MX = 100000 + 10;int k; LL Add[mx*4]; LL sum[mx*4];void pushup (int rt) {sum[rt]=sum[rt<<1]+sum[rt<<1|1];}    void build (int l,int R,int RT) {add[rt]=0;        if (l==r) {//scanf ("%lld", &sum[rt]);        Sum[rt]=1;    return;    } int m=mid;    Build (Lson);    Build (Rson); Pushup (RT);}        void pushdown (int rt,int m) {if (Add[rt]) {add[rt<<1]=add[rt];        add[rt<<1|1]=add[rt];        Sum[rt<<1]=add[rt]* (M-(m>>1));        sum[rt<<1|1]=add[rt]* (m>>1);    add[rt]=0;        }}void Update (int l,int r,ll c,int l,int r,int RT) {if (l<=l && r<=r) {add[rt]=c;        sum[rt]=c* (r-l+1);    return;    } pushdown (rt,r-l+1);    int m=mid;if (l<=m) update (L,r,c,lson);    if (r>m) update (L,r,c,rson); Pushup (RT);}    LL query (int l,int r,int l,int r,int RT) {if (l<=l && r<=r) return SUM[RT];    Pushdown (rt,r-l+1);    int m=mid;    LL ret=0;    if (l<=m) ret+=query (L,r,lson);    if (r>m) ret+=query (L,r,rson); return ret;}    int main () {int t,n,q,x,y,z,cas=1;    scanf ("%d", &t);        while (t--) {scanf ("%d%d", &n,&q);        Build (root);            while (q--) {scanf ("%d%d%d", &x,&y,&z);        Update (X,Y,Z,ROOT);    } printf ("Case%d:the total value of the hook is%lld.\n", Cas++,query (1,n,root)); } return 0;}

This problem, because the final request is the interval sum, so do not use line tree can also be done.
#include <cstdio>const int MX = 100000+10;int Num[mx][3];int main () {    int t,q,n,sum,val,cas=1;    scanf ("%d", &t);    while (t--) {        scanf ("%d%d", &n,&q);        for (int j=1; j<=q; j + +)            scanf ("%d%d%d", &num[j][0],&num[j][1],&num[j][2]);        sum=0;        for (int k=1; k<=n; k++) {            val=1;            for (int j=q; j>=1; j--)                if (num[j][0]<=k && k<=num[j][1]) {//Search for the update interval where k is located, update if present, no v=1                    invariant VAL=NUM[J][2];                 If the last surface of the update interval is found, stop, because the front will be overwritten by the break                    ;                }            Sum+=val;        }        printf ("Case%d:the total value of the hook is%d.\n", cas++,sum);    }    return 0;}

The general idea of the above code is: To determine whether each point in an update interval, in the update, not not processing, because the front will be covered by the back, so we consider from behind, backwards to do.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

HDU 1698 Just a Hook

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.