标签:
#include<bits/stdc++.h> using namespace std; #define N 100100 int a[N]; struct { int left,right,c; int ln,rn; int ls,rs,ms; }b[N*4]; void pushup(int i){ b[i].ls=b[i*2].ls; b[i].rs=b[2*i+1].rs; b[i].ln=b[i*2].ln; b[i].rn=b[2*i+1].rn; b[i].ms=max(b[2*i].ms,b[2*i+1].ms); if(b[2*i].rn<b[2*i+1].ln){ if(b[2*i].ls == b[2*i].c) b[i].ls+=b[2*i+1].ls; if(b[2*i+1].ls == b[2*i+1].c) b[i].rs+=b[2*i].rs; b[i].ms=max(b[i].ms,b[2*i+1].ls+b[i*2].rs); } } void build(int left,int right,int i){ int mid; b[i].left=left;b[i].right=right;b[i].c=right-left+1; if(left==right) { b[i].ln=b[i].rn=a[left]; b[i].ls=b[i].rs=b[i].ms=1; return ; } mid = (left+right)/2; build(left,mid,2*i); build(mid+1,right,2*i+1); pushup(i); } void Insert(int i,int t,int m){ if(b[i].left==b[i].right){ b[i].ln=b[i].rn=m; return ; } int mid=(b[i].left+b[i].right)>>1; if(t<=mid) Insert(2*i,t,m); if(t>mid) Insert(2*i+1,t,m); pushup(i); } int Query(int l,int r,int i){ if(b[i].left>=l && b[i].right<=r) return b[i].ms; int mid=(b[i].right+b[i].left)>>1,ans=0; if(l<=mid) ans=max(ans,Query(l,r,2*i)); if(r>mid) ans=max(ans,Query(l,r,2*i+1)); if(b[2*i].rn<b[2*i+1].ln) ans = max(ans , min(mid-l+1,b[2*i].rs)+min(r-mid,b[2*i+1].ls)); return ans; } int main(){ int t; scanf("%d",&t); while(t--){ memset(b,0,sizeof(b)); int n,m; scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",&a[i]); build(1,n,1); while(m--){ char q[10]; int x,y; getchar(); scanf("%s%d%d",q,&x,&y); if(q[0]=='U') Insert(1,x+1,y); else printf("%d\n",Query(x+1,y+1,1)); } } }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/a197p/article/details/47659459