线段树之HDU 1754 I hate it

题目:HDU 1754 I hate it!

//Must so
#include<bits/stdc++.h>
#define mem(a,x) memset(a,x,sizeof(a))
#define sqrt(n) sqrt((double)n)
#define pow(a,b) pow((double)a,(int)b)
#define inf 1<<29
#define NN 200006
using namespace std;
const double PI = acos(-1.0);
typedef long long LL;
struct Node
{
    int l,r,v;
}q[NN*4];
void buildtree(int i,int l,int r)
{
    q[i].l = l,q[i].r = r;
    if (l == r)
    {
        scanf("%d",&q[i].v);
        return ;
    }
    int mid = (l+r)/2;
    buildtree(i<<1,l,mid);
    buildtree((i<<1)+1,mid+1,r);
    q[i].v = max(q[i<<1].v,q[(i<<1)+1].v);
}
void update (int i ,int a,int b)
{
    if (q[i].l == q[i].r&&q[i].l == a)
    {
        q[i].v = b;
        return ;
    }
    if (a<=q[i<<1].r) update(i<<1,a,b);
    else update((i<<1)+1,a,b);
    q[i].v = max(q[i<<1].v,q[(i<<1)+1].v);
}
int query(int i,int l,int r)
{
    if (l <= q[i].l&&q[i].r <= r)
    {
        return q[i].v;
    }
    int mid = (q[i].l+q[i].r)/2;
    if (r <= mid) return query(i<<1,l,r);
    else if (l > mid) return query((i<<1)+1,l,r);
    else return max(query(i<<1,l,mid),query((i<<1)+1,mid+1,r));
}
int main()
{
    int n,m;
    while (cin>>n>>m)
    {
        buildtree(1,1,n);
        while (m--)
        {
            char tmp;
            int a,b;
            cin>>tmp>>a>>b;
            if (tmp == 'Q') printf("%d\n",query(1,a,b));
            else if (tmp == 'U') update(1,a,b);
        }
    }
    return 0;
}


你可能感兴趣的:(线段树)