树状数组求区间最值和单点更新

题目链接:https://cn.vjudge.net/contest/66989#problem/B

AC代码:

#include<iostream>
#include<string>
#include<cstring>
#include<cmath>
#include<stdio.h>
#include<iomanip>
#include<algorithm>
using namespace std;
# define ll long long
# define maxn 200000+100
int n,m;
int a[maxn];
int h[maxn];
int lowbit(int t)
{
    return t&(-t);
}
void update(int t)
{
    while(t<=n)
    {
        h[t]=a[t];
        int len=lowbit(t);
        for(int i=1; i<len; i<<=1)
        {
            h[t]=max(h[t],h[t-i]);
        }
        t+=lowbit(t);
    }
}
int query(int t1,int t2)
{
    int ans=0;
    while(t1<=t2)
    {
        ans=max(ans,a[t2]);
        t2--;
        while(t2-lowbit(t2)>=t1)
        {
            ans=max(ans,h[t2]);
            t2-=lowbit(t2);
        }
    }
    return ans;
}
int main()
{
    while(~scanf("%lld%lld",&n,&m))
    {
        memset(a,0,sizeof(a));
        memset(h,0,sizeof(h));
        for(int i=1; i<=n; i++)
        {
            scanf("%lld",&a[i]);
            update(i);
        }
        while(m--)
        {
            char str[10];
            int t1,t2;
            scanf("%s",str);
            if(str[0]=='Q')
            {
                scanf("%d%d",&t1,&t2);
                int ans=query(t1,t2);
                printf("%d\n",ans);
            }
            else if(str[0]=='U')
            {
                scanf("%d%d",&t1,&t2);
                a[t1]=t2;
                update(t1);
            }
        }
    }
    return 0;
}

 

posted @ 2018-10-12 20:35  Let_Life_Stop  阅读(178)  评论(0编辑  收藏  举报