• kuangbin专题七 HDU1754 I Hate It (单点修改维护最大值)


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

    不管你喜不喜欢,现在需要你做的是,就是按照老师的要求,写一个程序,模拟老师的询问。当然,老师有时候需要更新某位同学的成绩。

    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
    
    经过两道题的磨练,终于可以手搓了!细节需要注意一下

     1 #include <iostream>
     2 #include <stdio.h>
     3 #include <math.h>
     4 #include <string.h>
     5 #include <stdlib.h>
     6 #include <string>
     7 #include <vector>
     8 #include <set>
     9 #include <map>
    10 #include <queue>
    11 #include <algorithm>
    12 #include <sstream>
    13 #include <stack>
    14 using namespace std;
    15 #define FO freopen("in.txt","r",stdin);
    16 #define rep(i,a,n) for (int i=a;i<n;i++)
    17 #define per(i,a,n) for (int i=n-1;i>=a;i--)
    18 #define pb push_back
    19 #define mp make_pair
    20 #define all(x) (x).begin(),(x).end()
    21 #define fi first
    22 #define se second
    23 #define SZ(x) ((int)(x).size())
    24 #define debug(x) cout << "&&" << x << "&&" << endl;
    25 #define lowbit(x) (x&-x)
    26 #define mem(a,b) memset(a, b, sizeof(a));
    27 typedef vector<int> VI;
    28 typedef long long ll;
    29 typedef pair<int,int> PII;
    30 const ll mod=1000000007;
    31 const int inf = 0x3f3f3f3f;
    32 ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
    33 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
    34 //head
    35 
    36 const int N=200010;
    37 int n,m,a[N<<2],maxx[N<<2],pos,val,ans;
    38 
    39 void Pushup(int rt) {
    40     maxx[rt]=max(maxx[rt<<1],maxx[rt<<1|1]);
    41 }
    42 
    43 void build(int rt,int L,int R) {
    44     if(L==R) {
    45         scanf("%d",&a[rt]);
    46         maxx[rt]=a[rt];//这里是rt的维护信息
    47         return;
    48     }
    49     int mid=(L+R)>>1;
    50     build(rt<<1,L,mid);
    51     build(rt<<1|1,mid+1,R);
    52     Pushup(rt);
    53 }
    54 
    55 void Updata(int rt,int L,int R,int pos,int val) {
    56     if(L==R) {//修改
    57         a[rt]=val;
    58         maxx[rt]=a[rt];
    59         return;
    60     }
    61     int mid=(L+R)>>1;
    62     if(pos<=mid) Updata(rt<<1,L,mid,pos,val);
    63     else Updata(rt<<1|1,mid+1,R,pos,val);
    64     Pushup(rt);
    65 }
    66 
    67 void Query(int rt,int L,int R,int l,int r) {//维护最大值
    68     if(L>=l&&R<=r) {
    69         ans=max(ans,maxx[rt]);
    70         return;
    71     }
    72     int mid=(L+R)>>1;
    73     if(l<=mid) Query(rt<<1,L,mid,l,r);
    74     if(r>mid) Query(rt<<1|1,mid+1,R,l,r);
    75 }
    76 
    77 int main() {
    78     while(~scanf("%d%d",&n,&m)) {
    79         build(1,1,n);
    80         char s[5];
    81         while(m--) {
    82             ans=-1;
    83             scanf("%s%d%d",s,&pos,&val);
    84             if(s[0]=='Q') {
    85                 Query(1,1,n,pos,val);
    86                 printf("%d
    ",ans);
    87             } else Updata(1,1,n,pos,val);
    88         }
    89     }
    90 }




  • 相关阅读:
    aop
    javascript学习笔记
    Hibernate一对多关联映射的配置及其级联删除问题
    Hibernate一对多单向关联和双向关联映射方法及其优缺点 (待续)
    剑指offer---08---动态规划:跳台阶
    剑指offer---07---动态规划:斐波那契数列
    剑指offer---06---数组,二分法---旋转数组的最小数字
    剑指offer---05---用栈实现队列
    剑指offer---03---从尾到头打印链表---链表
    剑指offer---04-树--重建二叉树(前序和中序)
  • 原文地址:https://www.cnblogs.com/ACMerszl/p/9822294.html
Copyright © 2020-2023  润新知