線段樹解決,類似的,數狀數組也行。
點修改,區間查詢 都是 logn 的複雜度。
沒有使用自底向上的ZKW線段樹寫法,就普通的遞歸線段樹。
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<algorithm>
#include<map>
#include<stack>
#include<iostream>
#include<list>
#include<set>
#include<bitset>
#include<vector>
#include<cmath>
#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define PI 3.141592654
#define CLR(a,b) memset(a,b,sizeof(a))
#define FOR(i,a,b) for(int i=a;i<b;i++)
#define FOR_(i,a,b) for(int i=a;i>=b;i--)
#define pub push_back
#define puf push_front
#define pob pop_back
#define pof pop_front
#define mp make_pair
#define ft first
#define sd second
#define sf scanf
#define pf printf
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(),(v).end()
#define acfun std::ios::sync_with_stdio(false)
#define SIZE 400000 +1
#define MOD 1000000007
using namespace std;
int a[SIZE];
int _max[SIZE];
int n,m;
int build(int o,int l,int r)
{
if(l==r)return _max[o]=a[l];
int m=(l+r)>>1;
return _max[o]=max(build(o*2,l,m),build(o*2+1,m+1,r));
}
int ql,qr;
int query(int o,int l,int r)
{
if(ql<=l&&qr>=r)return _max[o];
int m=(l+r)>>1;
int ans=-INF;
if(ql<=m)ans=max(ans,query(o*2,l,m));
if(qr>m)ans=max(ans,query(o*2+1,m+1,r));
return ans;
}
int site,up;
void update(int o,int l,int r)
{
if(l==r)_max[o]=up;
else
{
int m=(l+r)>>1;
if(site<=m)
update(o*2,l,m);
else
update(o*2+1,m+1,r);
_max[o]=max(_max[o*2],_max[o*2+1]);
}
}
int main()
{
while(~sf("%d%d",&n,&m))
{
CLR(_max,0);
FOR(i,1,n+1)
sf("%d",&a[i]);
build(1,1,n);
char c;
while(m--)
{
getchar();
sf("%c",&c);
if(c=='Q')
{
sf("%d%d",&ql,&qr);
pf("%d\n",query(1,1,n));
}
else if(c=='U')
{
sf("%d%d",&site,&up);
update(1,1,n);
}
}
}
}