• HDU 1698 Just a Hook (线段树模板题-区间求和)


    Just a Hook

    In 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 performing the operations.
    You may consider the original hook is 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 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.
     
    Output
    For 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
    1
    10
    2
    1 5 2
    5 9 3
     
    Sample Output
    Case 1: The total value of the hook is 24.
    题目大意:Hook有一个由n个扣子组成的长链,初始值都为铜质,求q次改变之后长链的总值,每一次改变可以将一个扣子改成银质或金质。
    思路:即一个区间修改查询的线段树模板题啦,有q次的区间修改,最后查询一次1-n的总和即可
     
     1 #include<iostream>
     2 #include<algorithm>
     3 #include<cstring>
     4 #include<cstdio>
     5 
     6 using namespace std;
     7 const int maxn = 100005;
     8 int sum[maxn<<2],add[maxn<<2];
     9 int n,q,T;
    10 void pushup(int rt){
    11     sum[rt] = sum[rt<<1]+sum[rt<<1|1];
    12 }
    13 void pushdown(int rt, int ln, int rn)
    14 {
    15     if(add[rt])
    16     {
    17         sum[rt << 1] = ln * add[rt];
    18         sum[rt << 1 | 1] = rn * add[rt];
    19         add[rt << 1] = add[rt];
    20         add[rt << 1 | 1] = add[rt];
    21         add[rt] = 0;
    22     }
    23     return;
    24 }
    25 void build(int l,int r,int rt){
    26     add[rt] = 0;
    27     if(l==r){
    28         sum[rt] = 1;
    29         return ;
    30     }
    31     int mid = (l+r)>>1;
    32     build(l,mid,rt<<1);
    33     build(mid+1,r,rt<<1|1);
    34     pushup(rt);
    35 }
    36 void update(int l,int r,int rt,int L,int R,int C)
    37 {
    38     if(L<=l&&r<=R){
    39         sum[rt] = (r-l+1)*C;
    40         add[rt] = C;
    41         return ;
    42     }
    43     int mid = (l+r)>>1;
    44     pushdown(rt,mid-l+1,r-mid);
    45     if(L<=mid)update(l,mid,rt<<1,L,R,C);
    46     if(R>mid)update(mid+1,r,rt<<1|1,L,R,C);
    47     pushup(rt);
    48 }
    49 int query(int l,int r,int rt,int L,int R)
    50 {
    51     if(L<=l&&r<=R)return sum[rt];
    52     int mid = (l+r)>>1;
    53     pushdown(rt,mid-l+1,r-mid);
    54     int ans = 0;
    55     if(L<=mid) ans += query(l,mid,rt<<1,L,R);
    56     if(R>mid) ans += query(mid+1,r,rt<<1|1,L,R);
    57     return ans;
    58 }
    59 int main()
    60 {
    61     scanf("%d",&T);
    62     for(int t=1;t<=T;t++){
    63         scanf("%d",&n);
    64         build(1,n,1);
    65         scanf("%d",&q);
    66         int l,r,x;
    67         while(q--){
    68             scanf("%d%d%d",&l,&r,&x);
    69             update(1,n,1,l,r,x);
    70         }
    71         printf("Case %d: The total value of the hook is %d.
    ",t,query(1,n,1,1,n));
    72     }
    73     return 0;
    74 }
  • 相关阅读:
    谷歌浏览器(Google Chrome)开发调试详细介绍
    Js调试技巧
    Spring框架集成FreeMarker
    Spring MVC + freemarker实现半自动静态化
    Django学习路4_数据库添加元素,读取及显示到网页上
    数据库设计基础知识
    Django创建简单数据库
    list 和 [ ] 的功能不相同
    Django坑_01
    爬虫流程复习3
  • 原文地址:https://www.cnblogs.com/wangrunhu/p/9587907.html
Copyright © 2020-2023  润新知