caiOJ1099题
题目描述
给出N个数,两种操作:
1、C x y:修改第x个数的值为y;
2、P x y:求第x到第y个的最大值,注:x未必比y小
输入格式
第一行输入N和M(0<N<=200000,0<M<5000),N表示有N个数,M表示有M个操作
下来N个数
然后是M个操作。
输出格式
遇到P操作的时候,输出结果。
样例输入
5 6
1 2 3 4 5
P 1 5
C 3 6
P 3 4
P 4 5
C 2 9
P 1 5
样例输出
5
6
5
9
代码:(请不要直接拷贝哦)
#include <cstdio> #include <algorithm> int a[200005],n,m,x,y; char ch[1]; struct TREE{ int l,r,maxx; }tree[800005]; using namespace std; inline int read() { int f=1,x=0; char ch=getchar(); if (ch=='-') { f=-1; ch=getchar(); } while ((ch<'0')||(ch>'9')) ch=getchar(); while ((ch>='0')&&(ch<='9')) { x=x*10+ch-48; ch=getchar(); } return f*x; } inline void build(int root,int l,int r) { tree[root].l=l; tree[root].r=r; if (l==r) { tree[root].maxx=a[l]; return; } build(root*2,l,(l+r)/2); build(root*2+1,(l+r)/2+1,r); tree[root].maxx=max(tree[root*2].maxx,tree[root*2+1].maxx); } inline void change(int root) { int ll=tree[root].l,rr=tree[root].r; if ((ll==x)&&(rr==x)) { tree[root].maxx=y; return; } if (x<=(ll+rr)/2) change(root*2); else change(root*2+1); tree[root].maxx=max(tree[root*2].maxx,tree[root*2+1].maxx); } inline int find(int root,int l,int r) { int ll=tree[root].l,rr=tree[root].r; int mid=(ll+rr)/2; if ((ll==l)&&(rr==r)) return tree[root].maxx; if (r<=mid) return find(root*2,l,r); else if (l>mid) return find(root*2+1,l,r); else return max(find(root*2,l,(ll+rr)/2),find(root*2+1,(ll+rr)/2+1,r)); } int main() { n=read(),m=read(); for (int i=1;i<=n;i++) a[i]=read(); build(1,1,n); for (int i=1;i<=m;i++) { scanf("%s",&ch); x=read(),y=read(); if (ch[0]=='C') change(1); else printf("%d\n",find(1,min(x,y),max(x,y))); } return 0; }