zoukankan      html  css  js  c++  java
  • 洛谷P1531 I Hate It 线段树

    洛谷P1531 I Hate It

    线段树 单点修改 区间求最大

     1 #include <cstdio>
     2 #include <cmath>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <string>
     6 #include <algorithm>
     7 #include <iomanip>
     8 #include <iostream> 
     9 using namespace std ; 
    10 
    11 const int maxn = 200011 ; 
    12 struct node{
    13     int l,r,mx ; 
    14 }tree[4*maxn] ;
    15 int a[maxn] ; 
    16 int n,Q,ans,x,y ; 
    17 char s[2] ; 
    18 
    19 inline int read() 
    20 {
    21     char ch = getchar() ; 
    22     int x = 0 ,f = 1 ; 
    23     while(ch<'0'||ch>'9') { if(f=='-') f = -1 ; ch = getchar() ; } 
    24     while(ch>='0'&&ch<='9') { x = x*10 + ch - 48 ; ch = getchar() ; } 
    25     return x * f ; 
    26 }
    27 
    28 inline void pushup(int root) 
    29 {
    30     tree[root].mx = max( tree[root*2].mx,tree[root*2+1].mx ) ; 
    31 }
    32 
    33 inline void build(int l,int r,int root) 
    34 {
    35     tree[root].l = l ;   tree[root].r = r ; 
    36     if(l==r) 
    37     {
    38         tree[root].mx = a[ l ] ; 
    39         return ; 
    40     }
    41     int mid = (l+r) / 2  ; 
    42     build(l,mid,root*2) ; 
    43     build(mid+1,r,root*2+1) ; 
    44     pushup(root) ; 
    45 }
    46 
    47 inline void updata(int pos,int val,int root) 
    48 {
    49     if(tree[root].l==tree[root].r) 
    50     {
    51         if(val > tree[root].mx) tree[root].mx = val ; 
    52         return ; 
    53     }
    54     int mid = (tree[root].l+tree[root].r) / 2 ; 
    55     if( pos<=mid ) 
    56         updata(pos,val,root*2) ; 
    57     else
    58         updata(pos,val,root*2+1) ; 
    59     pushup(root) ; 
    60 }
    61 
    62 inline int query(int l,int r,int root) 
    63 {
    64     if(tree[root].l==l&&tree[root].r==r) 
    65         return tree[root].mx ; 
    66     int mid = ( tree[root].l + tree[root].r ) / 2 ;  
    67     if ( l > mid ) return query(l,r,root*2+1) ; 
    68     if ( r<=mid )  return query(l,r,root*2) ;
    69     int ans = max( query(l,mid,root*2),query(mid+1,r,root*2+1) ) ; 
    70     return ans ; 
    71 }
    72 
    73 int main() 
    74 {
    75     n = read() ;   Q = read() ; 
    76     for(int i=1;i<=n;i++) 
    77         a[ i ] = read() ; 
    78     build(1,n,1) ; 
    79     
    80     for(int i=1;i<=Q;i++) 
    81     {
    82         scanf("%s",s) ; 
    83         x = read() ;  y = read() ; 
    84         if(s[0]=='U') 
    85             updata(x,y,1) ;
    86         else 
    87         {
    88             ans = query(x,y,1) ;   
    89             printf("%d
    ",ans) ; 
    90         }
    91     }
    92     return 0 ; 
    93 }
  • 相关阅读:
    php类型运算符
    今天我开始写自己的东西
    挑选简历
    SQL Server和Oracle数据库索引介绍
    排序算法分析与设计实验
    软件框架 转
    【转】Ajax的原理和应用
    Web Service
    [转]异地分布式敏捷软件开发(Distributed Agile Software Development)
    [转]如何有效的使用C#读取文件
  • 原文地址:https://www.cnblogs.com/third2333/p/7084583.html
Copyright © 2011-2022 走看看