5 6 1 2 3 4 5 Q 1 5 U 3 6 Q 3 4 Q 4 5 U 2 9 Q 1 5
5 6 5 9HintHuge input,the C function scanf() will work better than cin
//回顾数据结构专题,悲剧错了n次的发现个问题:宏定义一些函数可能造成想不到的错误或超时等!
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define maxn 200050 struct node { int l,r,maxp; }tree[3*maxn]; int n,m,a[maxn]; int max(int p,int q) //最初将max函数宏定义#define max(p,q) (p>q?p:q) 一直超时,改了一下午在找到问题 { return p>q?p:q; } void BuildTree(int p,int l,int r) { tree[p].l=l,tree[p].r=r; if(l==r) { tree[p].maxp=a[l]; return; } int mid=(l+r)>>1; BuildTree(p<<1,l,mid); BuildTree(p<<1|1,mid+1,r); tree[p].maxp=max(tree[p<<1].maxp,tree[p<<1|1].maxp); } void change(int p,int i,int x) { if(tree[p].l==tree[p].r) { tree[p].maxp=x; return ; } if(i<=tree[p<<1].r) change(p<<1,i,x); else change(p<<1|1,i,x); tree[p].maxp=max(x,tree[p].maxp); } int query(int p,int l,int r) { if(tree[p].l==l&&tree[p].r==r) return tree[p].maxp; if(r<=tree[p<<1].r) return query(p<<1,l,r); if(l>=tree[p<<1|1].l) return query(p<<1|1,l,r); int mid=(tree[p].l+tree[p].r)>>1; return max(query(p<<1,l,mid),query(p<<1|1,mid+1,r)); } int main() { int i,x,y; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=n;i++) scanf("%d",&a[i]); BuildTree(1,1,n); char s[3]; while(m--) { scanf("%s%d%d",s,&x,&y); if(s[0]=='U') change(1,x,y); else if(s[0]=='Q') { if(x>y) swap(x,y); printf("%d\n",query(1,x,y)); } } } return 0; }