zoukankan      html  css  js  c++  java
  • HDU 1754 单点更新,求区间最大值

    I Hate It

    Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 46646    Accepted Submission(s): 18266


    Problem Description
    很多学校流行一种比较的习惯。老师们很喜欢询问,从某某到某某当中,分数最高的是多少。
    这让很多学生很反感。

    不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。
     
    Input
    本题目包含多组测试,请处理到文件结束。
    在每个测试的第一行,有两个正整数 N 和 M ( 0<N<=200000,0<M<5000 ),分别代表学生的数目和操作的数目。
    学生ID编号分别从1编到N。
    第二行包含N个整数,代表这N个学生的初始成绩,其中第i个数代表ID为i的学生的成绩。
    接下来有M行。每一行有一个字符 C (只取'Q'或'U') ,和两个正整数A,B。
    当C为'Q'的时候,表示这是一条询问操作,它询问ID从A到B(包括A,B)的学生当中,成绩最高的是多少。
    当C为'U'的时候,表示这是一条更新操作,要求把ID为A的学生的成绩更改为B。
     
    Output
    对于每一次询问操作,在一行里面输出最高成绩。
     
    Sample Input
    5 6
    1 2 3 4 5
    Q 1 5
    U 3 6
    Q 3 4
    Q 4 5
    U 2 9
    Q 1 5
     
    Sample Output
    5
    6
    5
    9
     
    思路:每个节点一个maxh,更新后,向上更新父区间即可。
     
    代码:
     1 #include <cstdio>
     2 #include <cstring>
     3 #include <algorithm>
     4 #include <iostream>
     5 #include <vector>
     6 #include <queue>
     7 #include <cmath>
     8 #include <set>
     9 using namespace std;
    10 
    11 #define N 200005
    12 #define ll root<<1
    13 #define rr root<<1|1
    14 #define mid (a[root].l+a[root].r)/2
    15 
    16 
    17 int max(int x,int y){return x>y?x:y;}
    18 int min(int x,int y){return x<y?x:y;}
    19 int abs(int x,int y){return x<0?-x:x;}
    20 
    21 int n;
    22 int b[N];
    23 
    24 struct node{
    25     int l, r, maxh;
    26 }a[N*4];
    27 
    28 void build(int l,int r,int root){
    29     a[root].l=l;
    30     a[root].r=r;
    31     if(l==r){
    32         a[root].maxh=b[l];return;
    33     }
    34     build(l,mid,ll);
    35     build(mid+1,r,rr);
    36     a[root].maxh=max(a[ll].maxh,a[rr].maxh);
    37 }
    38 
    39 void update(int p,int val,int root){
    40     if(a[root].l==a[root].r&&a[root].l==p){
    41         a[root].maxh=val;
    42         return;
    43     }
    44     if(a[ll].r>=p) update(p,val,ll);
    45     else update(p,val,rr);
    46     a[root].maxh=max(a[ll].maxh,a[rr].maxh);
    47 }
    48 
    49 
    50 
    51 int query(int l,int r,int root){
    52     if(a[root].l==l&&a[root].r==r) return a[root].maxh;
    53     if(a[ll].r<l) return query(l,r,rr);
    54     else if(a[rr].l>r) return query(l,r,ll);
    55     else return max(query(l,mid,ll),query(mid+1,r,rr));
    56 }
    57 
    58 main()
    59 {
    60     int t, i, j, k;
    61     int l, r;
    62     int q;
    63     while(scanf("%d %d",&n,&q)==2){
    64         char s[5];
    65         for(i=1;i<=n;i++) scanf("%d",&b[i]);
    66         build(1,n,1);
    67         while(q--){
    68             scanf("%s%d%d",s,&l,&r);
    69             if(s[0]=='Q') printf("%d
    ",query(l,r,1));
    70             else update(l,r,1);
    71         }
    72     }
    73 }
  • 相关阅读:
    python:xlrd模块
    psql:转:会引起全表扫描的10种sql语句
    Linux相关
    面试题
    Siege Web服务器性能压力测试工具
    Nginx+uWSGI+Supervisor配置
    SQLAlchemy
    Virtualenv创建虚拟环境
    算法
    Mac常用快捷键
  • 原文地址:https://www.cnblogs.com/qq1012662902/p/4527334.html
Copyright © 2011-2022 走看看