HDU 1754 I Hate It(区间最值)

来源:互联网 发布:java jdbc连接池配置 编辑:程序博客网 时间:2024/05/29 14:32

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1754

思路:updata()单点替换,query()区间最值

AC代码:

#include <iostream>#include <cstdio>#include <cstdlib>#include <algorithm>#include <queue>#include <stack>#include <map>#include <cstring>#include <climits>#include <cmath>#include <cctype>const int inf = 0x3f3f3f3f;//1061109567typedef long long ll;const int maxn = 200010;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;int value[maxn<<2];void pushup(int rt){    value[rt] = max(value[rt<<1],value[rt<<1|1]);}void build(int l,int r,int rt){    if(l == r)    {        scanf("%d",&value[rt]);        return;    }    int m = (r + l )>> 1;    build(lson);    build(rson);    pushup(rt);}void updata(int p,int sc,int l,int r,int rt){    if(l == r)    {        value[rt] = sc;        return;    }    int m = (l + r) >> 1;    if(p <= m) updata(p,sc,lson);    else updata(p,sc,rson);    pushup(rt);}int query(int L,int R,int l,int r,int rt){    if(l >= L && r <= R) return value[rt];    int m = (l + r) >> 1;    int ans = 0;    if(L <= m) ans = max(ans,query(L,R,lson));    if(R > m) ans = max(ans,query(L,R,rson));    return ans;}int main(){    int n,m;    while(scanf("%d%d",&n,&m) != EOF)    {        build(1,n,1);        char op[2];        for(int i=0; i<m; i++)        {            int a,b;            scanf("%s%d%d",op,&a,&b);            if(op[0] == 'Q') printf("%d\n",query(a,b,1,n,1));            else updata(a,b,1,n,1);        }    }    return 0;}


0 0
原创粉丝点击