HDOJ 1754 I Hate It (线段树单点更新求区间最大值)

来源:互联网 发布:数据库软件access 编辑:程序博客网 时间:2024/05/01 03:32


http://acm.hdu.edu.cn/showproblem.php?pid=1754


#include<cstdio>#include<cstring>#include<string>#include<iostream>#include<cmath>#include<algorithm>#define VN 200005#define INT_MIN -0x3f3f3f3fusing namespace std;int N, n, m, x, y, a, T, A[VN*3], dat[VN*3], ans;char s[20];void init(int _n){    N = 1;    while (N < _n) N *= 2;    for (int i = 0; i < 2*N-1; i++) dat[i] = INT_MIN;}int query(int a, int b, int k, int l, int r){    if (a >= r || b <= l) return INT_MIN;    if (a <= l && r <= b) return dat[k];    int vl = query(a, b, k*2+1, l, (l+r)/2);    int vr = query(a, b, k*2+2, (l+r)/2, r);    return max(vl, vr);}void update(int k, int a){    k += N - 1;    dat[k] = a;    while (k > 0)    {        k = (k - 1) / 2;        dat[k] = max(dat[k*2+1], dat[k*2+2]);    }}int main(){    while (~scanf("%d%d", &n, &m))    {        init(n);        for (int i = 0; i < n; i++)        {            scanf("%d", &a);            update(i, a);        }        for (int i = 0; i < m; i++)        {            scanf("%s", s);            if (s[0] == 'Q')            {                scanf("%d%d", &x, &y);                ans = query(x-1, y, 0, 0, N);                printf("%d\n", ans);            }            if (s[0] == 'U')            {                scanf("%d%d", &x, &y);                update(x-1, y);            }        }    }    return 0;}

0 0
原创粉丝点击