zoukankan      html  css  js  c++  java
  • HDU 1754 I Hate It 线段树 单点更新 区间最大值

     1 #include<iostream>
     2 #include<string>
     3 #include<algorithm>
     4 #include<cstdlib>
     5 #include<cstdio>
     6 #include<set>
     7 #include<map>
     8 #include<vector>
     9 #include<cstring>
    10 #include<stack>
    11 #include<cmath>
    12 #include<queue>
    13 #include <bits/stdc++.h>
    14 using namespace std;
    15 
    16 int MAX[4000001];
    17 //int max(int a,int b)
    18 //{
    19 //    return a>b?a:b;
    20 //}
    21 void pushup(int rt)
    22 {
    23     MAX[rt]=max(MAX[rt<<1],MAX[(rt<<1)+1]);
    24 }
    25 void build(int l,int r,int rt)
    26 {
    27     if(l==r)
    28     {
    29         scanf("%d",&MAX[rt]);
    30         return;
    31     }
    32     int m=(l+r)>>1;
    33     build(l,m,rt<<1);
    34     build(m+1,r,(rt<<1)+1);
    35     pushup(rt);
    36 }
    37 void update(int p,int q,int l,int r,int rt)
    38 {
    39     if(l==r)
    40     {
    41         MAX[rt]=q;
    42         return;
    43     }
    44     int m=(l+r)>>1;
    45     if(p<=m)
    46         update(p,q,l,m,rt<<1);
    47     else
    48         update(p,q,m+1,r,(rt<<1)+1);
    49     pushup(rt);
    50 }
    51 int getmax(int L,int R,int l,int r,int rt)
    52 {
    53     if(L<=l&&r<=R)
    54         return MAX[rt];
    55     int m=(r+l)>>1;
    56     int ret=0;
    57     if(L<=m)
    58         ret=max(ret,getmax(L,R,l,m,rt<<1));
    59     if(R>m)
    60         ret=max(ret,getmax(L,R,m+1,r,(rt<<1)+1));
    61     return ret;
    62 }
    63 int main()
    64 {
    65     int n,m,a,b,i;
    66     char c;
    67     while(~scanf("%d %d",&n,&m))
    68     {
    69         build(1,n,1);
    70         for(i=0; i<m; i++)
    71         {
    72             scanf("%*c%c%d %d",&c,&a,&b);//*c与getchar一样
    73             if(c=='Q')
    74                 printf("%d
    ",getmax(a,b,1,n,1));
    75             else
    76                 update(a,b,1,n,1);
    77         }
    78     }
    79     return 0;
    80 }
    View Code
  • 相关阅读:
    Mysql安装
    mysql 密码过期
    svn 合并分支
    idea 分支主干管理
    linux删除数据恢复,extundelete
    linux 转换文件编码
    sina 接口 根据ip获取各个国家和地区
    SQL中char、varchar、nvarchar的区别
    C#中virtual和abstract区别,举例子
    父类和子类的关系、代码例子
  • 原文地址:https://www.cnblogs.com/ITUPC/p/5202424.html
Copyright © 2011-2022 走看看