标签:
Time Limit: 9000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 59558 Accepted Submission(s): 23201
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1);
const int maxn=200000;
int cnt;
struct node{
int l,r,maxn;
}tree[4*maxn];
void build(int k,int l,int r)
{
tree[k].l=l;
tree[k].r=r;//在建树的同时给节点设置好左右区间
if(r==l) scanf("%d",&tree[k].maxn);
else
{
int mid=(l+r)>>1;
build(2*k,l,mid);
build(2*k+1,mid+1,r);
tree[k].maxn=max(tree[2*k].maxn,tree[2*k+1].maxn);//记得向上一层更新
}
}
int query(int k,int l,int r)
{
if(l<=tree[k].l&&tree[k].r<=r) return tree[k].maxn;
else if(tree[k].r>=l&&tree[k].l<=r)
{
int mid=(l+r)>>1;
return max(query(2*k,l,r),query(2*k+1,l,r));
}
else return 0;
}
void add(int k,int pos,int val)
{
if(tree[k].r==tree[k].l) tree[k].maxn=val;
else
{
int mid=(tree[k].l+tree[k].r)>>1;
if(pos<=mid) add(2*k,pos,val);
else add(2*k+1,pos,val);
tree[k].maxn=max(tree[2*k].maxn,tree[2*k+1].maxn);
//记得向上一层更新
}
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
build(1,1,n);
char s[5];
for(int k=1;k<=m;k++)
{
scanf("%s",s);
int x,y;
scanf("%d %d",&x,&y);
if(s[0]==‘Q‘)
printf("%d\n",query(1,x,y));
else
add(1,x,y);
}
}
return 0;
}
标签:
原文地址:http://www.cnblogs.com/smilesundream/p/5425616.html