码迷,mamicode.com
首页 > 其他好文 > 详细

P1531 I Hate It 题解

时间:2020-03-30 23:07:32      阅读:67      评论:0      收藏:0      [点我收藏+]

标签:输入格式   type   lowbit   pre   str   cin   swap   老师   成绩   

P1531 I Hate It

题目背景

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

题目描述

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

输入格式

第一行,有两个正整数 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‘的时候,表示这是一条更新操作,如果当前A学生的成绩低于B,则把ID为A的学生的成绩更改为B,否则不改动。

输出格式

对于每一次询问操作,在一行里面输出最高成绩

输入输出样例

输入 #1

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

输出 #1

5
6
5
9

题解

分析

这题有2种操作,1.区间求最值,2.单点修改

所以这题正常情况下可以用线段树做,但是我懒,就用树状数组水过去了。

单点修改没有问题。

区间求最值,可以打暴力。

代码

#include <bits/stdc++.h>
#define ll long long
#define re register int
#define For(i,a,b) for(ll i=(a); i<=(b); i++)
const int INF=1<<30;
const int mod=1e9+3;
using namespace std;

template <typename T>
inline void read(T &x) { 
    x = 0;
    ll f = 1;
    char ch = getchar();
    while (!isdigit(ch)) {
        if (ch == ‘-‘) f = -1;
        ch = getchar();
    }
    while (isdigit(ch)) {
        x = x * 10 + (ch ^ 48);
        ch = getchar();
    }
    x *= f;
    return;
}
template <typename T>
inline void write(T x){
    if(x < 0) {
        putchar(‘-‘);
        x = -x; 
    } 
    if(x > 9) 
        write(x/10); 
    putchar(x % 10 + ‘0‘); 
    return; 
}
inline ll qpow(ll a,ll b){
    ll x=1,y=a;
    while(b>=1){
        if(b%2==1) x=x*y%mod;
        y=y*y%mod;
        b/=2;
    } 
    return x;
}
int q[200005],n,m,k,x,y;
char fuck;
inline int lowbit(int x){return x&(-x);}
inline void add(int x,int y){
    for(int i=x;i<=n;i+=lowbit(i))
        q[i]+=y;
}
inline int ask(int x){
    int ans=0;
    for(int i=x;i>=1;i-=lowbit(i))
        ans+=q[i];
    return ans;
}
int main() {
    read(n),read(m);
    for(re i=1;i<=n;++i)
        read(k),add(i,k);
    for(re i=1;i<=m;++i){
        cin>>fuck;
        read(x),read(y);
        if(fuck==‘Q‘){
            int ans=0;
            if(x>y) swap(x,y);
            for(int i=x;i<=y;i++)
                ans=max(ask(i)-ask(i-1),ans);
            write(ans),puts("");
        }
        else{
            int o=ask(x)-ask(x-1),p=y;
            if(o<p)
                add(x,p-o);
        }
    }

    
}

P1531 I Hate It 题解

标签:输入格式   type   lowbit   pre   str   cin   swap   老师   成绩   

原文地址:https://www.cnblogs.com/iloveori/p/12601849.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!