标签:
线段树,区间合并
最长上升子序列(严格上升)
#include<iostream> #define maxn 100010 using namespace std; int n,m; int a,b; struct stu { int l,r,mid; int ll,rl,ml; int d() { return r-l+1; } }; stu mapp[maxn*4]; int num[maxn]; void push(int count) { if(mapp[count*2].ll==mapp[count*2].d()&&num[mapp[count*2].r]<num[mapp[count*2+1].l]) mapp[count].ll=mapp[count*2].ll+mapp[count*2+1].ll; else mapp[count].ll=mapp[count*2].ll; if(mapp[count*2+1].rl==mapp[count*2+1].d()&&num[mapp[count*2].r]<num[mapp[count*2+1].l]) mapp[count].rl=mapp[count*2+1].rl+mapp[count*2].rl; else mapp[count].rl=mapp[count*2+1].rl; mapp[count].ml=max(mapp[count*2].ml,mapp[count*2+1].ml); if(num[mapp[count*2].r]<num[mapp[count*2+1].l]) { int x=mapp[count*2].rl+mapp[count*2+1].ll; mapp[count].ml=max(mapp[count].ml,x); } } void build(int l,int r,int count) { mapp[count].l=l; mapp[count].r=r; mapp[count].mid=(l+r)/2; if(l==r) { mapp[count].ll=mapp[count].rl=mapp[count].ml=1; return ; } build(l,mapp[count].mid,count*2); build(mapp[count].mid+1,r,count*2+1); push(count); //cout<<count<<"~"<<mapp[count].l<<"~"<<mapp[count].r<<"~"<<mapp[count].ll<<"~"<<mapp[count].rl<<"~"<<mapp[count].ml<<endl; } void updata(int l,int r,int count) { if(l==r) return; int mid=mapp[count].mid; if(a+1<=mapp[count].mid) updata(l,mid,count*2); else updata(mid+1,r,count*2+1); push(count); } int que(int l,int r,int count) { //cout<<l<<"~"<<r<<endl; int mid=mapp[count].mid; if(mapp[count].l==l&&mapp[count].r==r) return mapp[count].ml; if(r<=mid) return que(l,r,count*2); else if(l>=mid+1) return que(l,r,count*2+1); else { int x=0; int y=max(que(l,mid,count*2),que(mid+1,r,count*2+1)); if(num[mid]<num[mid+1]) { x=min(mapp[count*2].rl,mid-l+1)+min(r-mid,mapp[count*2+1].ll); } return max(x,y); } } int main() { cin.sync_with_stdio(false); int t; cin>>t; while(t--) { cin>>n>>m; for(int i=1;i<=n;i++) cin>>num[i]; build(1,n,1); //cout<<mapp[1].ll<<"~"<<mapp[1].rl<<"~"<<mapp[1].ml<<endl; //cout<<mapp[2].ll<<"~"<<mapp[2].rl<<"~"<<mapp[2].ml<<endl; while(m--) { string cmd; cin>>cmd>>a>>b; if(cmd=="Q") { cout<<que(a+1,b+1,1)<<endl; } else { num[a+1]=b; updata(1,n,1); } } } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
标签:
原文地址:http://blog.csdn.net/zafkiel_nightmare/article/details/47657803