zoukankan      html  css  js  c++  java
  • hdu1698 Just a Hook(线段树+区间改值+区间求和)

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 54923    Accepted Submission(s): 25566


    Problem Description
    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.
     
    线段树区间修改、查询的模板,区间修改时直接改值
     1 #include <cstdio>
     2 #include <cstring>
     3 using namespace std;
     4 
     5 const int MAXN=1e5+5;
     6 int st[MAXN<<2],lazy[MAXN<<2];///lazy懒标记数组
     7 
     8 void pushup(int rt){
     9     st[rt]=st[rt<<1]+st[rt<<1|1];
    10 }
    11 
    12 void pushdown(int l,int r,int rt){
    13     if(lazy[rt]==0)return;
    14     lazy[rt<<1]=lazy[rt];
    15     lazy[rt<<1|1]=lazy[rt];
    16     int mid=(l+r)>>1;
    17     st[rt<<1]=lazy[rt]*(mid-l+1);
    18     st[rt<<1|1]=lazy[rt]*(r-mid);
    19     lazy[rt]=0;
    20 }
    21 
    22 void build(int l,int r,int rt){
    23     lazy[rt]=0;
    24     if(l==r){
    25         st[rt]=1;
    26         return;
    27     }
    28     int mid=(l+r)>>1;
    29     pushdown(l,r,rt);
    30     build(l,mid,rt<<1);
    31     build(mid+1,r,rt<<1|1);
    32     pushup(rt);
    33 }
    34 
    35 void update(int L,int R,int val,int l,int r,int rt){
    36     if(L<=l&&r<=R){
    37         lazy[rt]=val;
    38         st[rt]=(r-l+1)*val;
    39         return;
    40     }
    41     pushdown(l,r,rt);
    42     int mid=(l+r)>>1;
    43     if(L<=mid)update(L,R,val,l,mid,rt<<1);
    44     if(R>mid)update(L,R,val,mid+1,r,rt<<1|1);
    45     pushup(rt);
    46 }
    47 
    48 int query(int L,int R,int l,int r,int rt){
    49     if(L<=l&&r<=R)return st[rt];
    50     pushdown(l,r,rt);
    51     int mid=(l+r)>>1,res=0;
    52     if(L<=mid)res+=query(L,R,l,mid,rt<<1);
    53     if(R>mid)res+=query(L,R,mid+1,r,rt<<1|1);
    54     pushup(rt);
    55     return res;
    56 }
    57 
    58 int main(){
    59     int t,n,q,x,y,z,ca=0;
    60     scanf("%d",&t);
    61     while(t--){
    62         scanf("%d%d",&n,&q);
    63         build(1,n,1);
    64         while(q--){
    65             scanf("%d%d%d",&x,&y,&z);
    66             update(x,y,z,1,n,1);
    67         }
    68         printf("Case %d: The total value of the hook is %d.
    ",++ca,st[1]);
    69     }
    70 }
  • 相关阅读:
    文件管理
    字符编码
    字典练习,统计字符串中单词出现次数
    字典有关练习:购物车
    列表及列表操作方法
    字符串及用法
    变量,程序交互,基本数据类型
    /usr/bin/ld: i386:x86-64 architecture of input file `command.o' is incompatible with i386 output
    混合云存储系统开发总结
    小记6月27
  • 原文地址:https://www.cnblogs.com/ChangeG1824/p/11508236.html
Copyright © 2011-2022 走看看