• HDU 1754.I Hate It-完全版线段树(单点替换、区间最值)


    HDU1754.I Hate It

    直接模板就可以了

    代码:

     1 //B
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cstring>
     5 #include<cmath>
     6 #include<algorithm>
     7 #include<cstdlib>
     8 #include<queue>
     9 #include<stack>
    10 using namespace std;
    11 typedef long long ll;
    12 const int inf=0x3f3f3f3f;
    13 const double eps=1e-5;
    14 const int maxn=5*1e5+10;
    15 #define lson l,m,rt<<1
    16 #define rson m+1,r,rt<<1|1
    17 int sum[maxn<<2],MAX[maxn<<2];
    18 
    19 void PushUp(int rt){
    20     //sum[rt]=sum[rt<<1]+sum[rt<<1|1];
    21     MAX[rt]=max(MAX[rt<<1],MAX[rt<<1|1]);
    22 }
    23 
    24 void build(int l,int r,int rt){
    25     if(l==r){
    26         //scanf("%d",&sum[rt]);
    27         scanf("%d",&MAX[rt]);
    28         return ;
    29     }
    30     int m=(l+r)>>1;
    31     build(lson);
    32     build(rson);
    33     PushUp(rt);
    34 }
    35 /*
    36 //单点增减
    37 void update(int p,int add,int l,int r,int rt){
    38     if(l==r){
    39         sum[rt]+=add;
    40         return ;
    41     }
    42     int m=(l+r)>>1;
    43     if(p<=m)update(p,add,lson);
    44     else update(p,add,rson);
    45     PushUp(rt);
    46 }
    47 */
    48 
    49 //单点更新
    50 void update(int p,int sc,int l,int r,int rt){
    51     if(l==r){
    52         MAX[rt]=sc;
    53         return ;
    54     }
    55     int m=(l+r)>>1;
    56     if(p<=m)update(p,sc,lson);
    57     else update(p,sc,rson);
    58     PushUp(rt);
    59 }
    60 /*
    61 //区间求和
    62 int query(int L,int R,int l,int r,int rt){
    63     if(L<=l&&r<=R){
    64         return sum[rt];
    65     }
    66     int m=(l+r)>>1;
    67     int ret=0;
    68     if(L<=m)ret+=query(L,R,lson);
    69     if(R>m) ret+=query(L,R,rson);
    70     return ret;
    71 }
    72 */
    73 
    74 //区间最值
    75 int query(int L,int R,int l,int r,int rt){
    76     if(L<=l&&r<=R){
    77         return MAX[rt];
    78     }
    79     int m=(l+r)>>1;
    80     int ret=0;
    81     if(L<=m)ret=max(ret,query(L,R,lson));
    82     if(R>m) ret=max(ret,query(L,R,rson));
    83     return ret;
    84 }
    85 
    86 
    87 int main(){
    88     int n,m;
    89     while(~scanf("%d%d",&n,&m)){
    90         build(1,n,1);
    91         char s[10];int a,b;
    92         while(m--){
    93             scanf("%s%d%d",s,&a,&b);
    94             if(s[0]=='Q')printf("%d
    ",query(a,b,1,n,1));
    95             else update(a,b,1,n,1);
    96         }
    97     }
    98     return 0;
    99 }
  • 相关阅读:
    期末考试结束啦!!!
    【sdut2878】Circle
    【HDU4035】Maze
    【ZOJ3329】One Person Game
    【POJ2151】Check the difficulty of problems
    【CodeForces148D】Bag of mice
    【POJ2096】Collecting Bugs
    【HDU3853】LOOPS
    【HDU4405】Aeroplane_chess
    「Luogu P5826 【模板】子序列自动机」
  • 原文地址:https://www.cnblogs.com/ZERO-/p/9729086.html
Copyright © 2020-2023  润新知