zoukankan      html  css  js  c++  java
  • hdu1698 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.

     

    题意:描述了一个屠夫,它的肉钩有N节,每节价值是1~3,它可以成段修改它的肉钩的价值,问修改若干次后肉钩的总价值是多少

    线段树裸题,区间改值与查询总权值和。

     1 #include<stdio.h>
     2 #include<string.h>
     3 const int maxm=100005;
     4 int st[maxm<<2],cha[maxm<<2];
     5 
     6 int read(){
     7     int x=0;
     8     char c=getchar();
     9     while(c>'9'||c<'0')c=getchar();
    10     while(c>='0'&&c<='9'){
    11         x=x*10+c-'0';
    12         c=getchar();
    13     }
    14     return x;
    15 }
    16 
    17 void build(int o,int l,int r){
    18     if(l==r){st[o]=1;return;}
    19     int m=l+((r-l)>>1);
    20     build(o<<1,l,m);
    21     build(o<<1|1,m+1,r);
    22     st[o]=st[o<<1]+st[o<<1|1];
    23 }
    24 
    25 void pushup(int o){
    26     st[o]=st[o<<1]+st[o<<1|1];
    27 }
    28 
    29 void pushdown(int o,int l,int r){
    30     if(cha[o]){
    31         cha[o<<1]=cha[o];
    32         cha[o<<1|1]=cha[o];
    33         int m=l+((r-l)>>1);
    34         st[o<<1]=cha[o]*(m-l+1);
    35         st[o<<1|1]=cha[o]*(r-m);
    36         cha[o]=0;
    37     }
    38 }
    39 
    40 void update(int o,int l,int r,int ql,int qr,int c){
    41     if(ql<=l&&qr>=r){
    42         cha[o]=c;
    43         st[o]=c*(r-l+1);
    44         return;
    45     }
    46     pushdown(o,l,r);
    47     int m=l+((r-l)>>1);
    48     if(ql<=m)update(o<<1,l,m,ql,qr,c);
    49     if(qr>=m+1)update(o<<1|1,m+1,r,ql,qr,c);
    50     pushup(o);
    51 }
    52 
    53 int main(){
    54     int T=read();
    55     for(int q=1;q<=T;q++){
    56         memset(cha,0,sizeof(cha));
    57         int n=read();
    58         build(1,1,n);
    59         int m=read();
    60         for(int i=1;i<=m;i++){
    61             int ql=read();
    62             int qr=read();
    63             int c=read();
    64             update(1,1,n,ql,qr,c);
    65         }
    66         printf("Case %d: The total value of the hook is %d.
    ",q,st[1]);
    67     }
    68     return 0;
    69 }
    View Code
  • 相关阅读:
    1242 斐波那契数列的第N项
    1256 乘法逆元
    1264 线段相交
    1265 四点共面
    Uva10881 Piotr's Ants
    hdu 5438 Ponds 长春网赛1002
    CodeForces 540D Bad Luck Island 概率dp
    hdu 1281 二分图残量增广
    hdu 2444判定二分图+最大匹配
    hdu 3416 Marriage Match IV
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/6592275.html
Copyright © 2011-2022 走看看