Hdoj 1698 Just a Hook

Source: Internet
Author: User
Tags cas

Original question:
In 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.

Input
The 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.

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

Sample Input
1
10
2
1 5 2
5 9 3

Sample Output
Case 1:the total value of the hook is 24.
English:
Hatred can be thrown out of a chain, with a chain of n knots. The knots on the chain can have three kinds of copper, silver, gold, respectively, 1, 2, 3, respectively. Initially this chain is all copper, now give you m interval replacement operation, the length of a paragraph to replace the chain of another material. And finally ask you what the value of all the chains are.

#include <bits/stdc++.h> using namespace std;
#define Lson l,m,rt<<1 #define RSON m+1,r,rt<<1|1 const int maxn=100001; int col[maxn<<2],sum[maxn<<2];//substitution token, sum void pushup (int rt) {Sum[rt]=sum[rt<<1]+sum[rt<<1|1
];
        } void pushdown (int rt,int m) {if (Col[rt]) {col[rt<<1]=col[rt<<1|1]=col[rt];
        Sum[rt<<1]= (M (m>>1)) *COL[RT];
        sum[rt<<1|1]= (m>>1) *col[rt];
    col[rt]=0;
    }} void Build (int l,int R,int RT) {col[rt]=0;
    Sum[rt]=1;
    if (l==r) return;
    int m= (L+R) >>1;
    Build (Lson);
    Build (Rson);
Pushup (RT);
        } void Update (int l,int r,int c,int l,int r,int RT) {if (l<=l&&r<=r) {col[rt]=c;
        sum[rt]=c* (r-l+1);
    Return
    } pushdown (rt,r-l+1);
    int m= (L+R) >>1;
    if (l<=m) update (L,r,c,lson);
    if (r>m) update (L,r,c,rson);
Pushup (RT); } int main () {Ios::sync_wIth_stdio (FALSE);
    int t,n,m;
    cin>>t;
        for (int cas=1;cas<=t;cas++) {cin>>n>>m;
        Build (1,n,1);
            while (m--) {int a,b,c;
            cin>>a>>b>>c;
        Update (a,b,c,1,n,1); } cout<< "Case" <<cas<< ": the total value of the hook is" <<sum[1]<< "."
    <<endl;
} return 0;
 }

Answer:
The first line segment tree interval query problem, the code completely refer to the online recommend HH template, originally looked at the Rujia of the petition, the results found not very good understanding =_=.
The method used to understand how to use the delay tag to change the segment tree interval is probably the same thing. If every time you modify or manipulate an interval [l,r], then the sub-range of the segment tree and also needs to be modified, and the modified is an interval section, almost involves the entire line tree modification, time complexity is very high. The approximate idea of the delay tag method used here is that each time a change is made on the current large interval, the sub-interval is not moved for the time being, and if there is a query operation or another change operation involves the current interval, the last non-modified sub-range is modified, This is also why the query operation queries (which do not have a query) will also pass the change action to the sub-range. The
Explanation can refer to this blog, very good.
Http://blog.sina.com.cn/s/blog_a2dce6b30101l8bi.html

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.