Description
给定一个序列A[i],每次询问l,r,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串
Input
第一行n,表示A数组有多少元素
接下来一行为n个整数A[i]
接下来一个整数Q,表示询问数量
接下来Q行,每行2个整数l,r
N,Q<=50000
Output
对于每个询问,求[l,r]内最长子串,使得该子串为不上升子串或不下降子串
Sample Input
9
1 2 3 4 5 6 5 4 3
5
1 6
1 7
2 7
1 9
5 9
1 2 3 4 5 6 5 4 3
5
1 6
1 7
2 7
1 9
5 9
Sample Output
6
6
5
6
4
//样例解释
五个询问分别对应
[1,6][1,6][2,6][1,6][6,9]
6
5
6
4
//样例解释
五个询问分别对应
[1,6][1,6][2,6][1,6][6,9]
HINT
Source
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct node
{
int l,r,lc,rc;
int zhigh,zlow,yhigh,ylow;//左升,左降,右升,右降
int maxup,maxdown;
}tr[110000];int len;
int col[51000];
void bt(int l,int r)
{
len++;int now=len;
tr[now].l=l;tr[now].r=r;tr[now].lc=tr[now].rc=-1;
tr[now].zhigh=tr[now].zlow=tr[now].yhigh=tr[now].ylow=0;
if(l!=r)
{
int mid=(l+r)/2;
tr[now].lc=len+1;bt(l,mid);
tr[now].rc=len+1;bt(mid+1,r);
}
}
void tongji(int now,int lc,int rc)//now下的lc向rc延伸,rc向lc延伸
{
//不下降
tr[now].maxup=max(tr[lc].maxup,tr[rc].maxup);
if(col[tr[rc].l]>=0)//右子树最左边比左子树最右边大
tr[now].maxup=max(tr[now].maxup,tr[lc].yhigh+tr[rc].zhigh);
tr[now].zhigh=tr[lc].zhigh;
if(tr[now].zhigh==tr[lc].r-tr[lc].l+1 && col[tr[rc].l]>=0)
tr[now].zhigh=tr[lc].zhigh+tr[rc].zhigh;
tr[now].yhigh=tr[rc].yhigh;
if(tr[now].yhigh==tr[rc].r-tr[rc].l+1 && col[tr[rc].l]>=0)
tr[now].yhigh=tr[rc].yhigh+tr[lc].yhigh;
//不上升
tr[now].maxdown=max(tr[lc].maxdown,tr[rc].maxdown);
if(col[tr[rc].l]<=0)//右子树最左边比左子树最右边小
tr[now].maxup=max(tr[now].maxup,tr[lc].ylow+tr[rc].zlow);
tr[now].zlow=tr[lc].zlow;
if(tr[now].zlow==tr[lc].r-tr[lc].l+1 && col[tr[rc].l]<=0)
tr[now].zlow=tr[lc].zlow+tr[rc].zlow;
tr[now].ylow=tr[rc].ylow;
if(tr[now].ylow==tr[rc].r-tr[rc].l+1 && col[tr[rc].l]<=0)
tr[now].ylow=tr[lc].ylow+tr[rc].ylow;
}
void change(int now,int p,int x)
{
if(tr[now].l==tr[now].r)
{
tr[now].maxup=tr[now].maxdown=1;
tr[now].zhigh=tr[now].zlow=tr[now].yhigh=tr[now].ylow=1;
return ;
}
int lc=tr[now].lc,rc=tr[now].rc;
int mid=(tr[now].l+tr[now].r)/2;
if(p<=mid) change(lc,p,x);
else change(rc,p,x);
tongji(now,lc,rc);
}
int findmaxup(int now,int l,int r)//找不下降
{
if(tr[now].l==l && tr[now].r==r) return tr[now].maxup;
int lc=tr[now].lc,rc=tr[now].rc;
int mid=(tr[now].l+tr[now].r)/2;
if(r<=mid) return findmaxup(lc,l,r);
else if(l>mid) return findmaxup(rc,l,r);
else
{
int cnt=max(findmaxup(lc,l,mid),findmaxup(rc,mid+1,r));
if(col[tr[rc].l]>=0)
{
int ss=min(tr[rc].zhigh,r-mid);
ss+=min(tr[lc].yhigh,mid-l+1);
return max(cnt,ss);
}
return cnt;
}
}
int findmaxdown(int now,int l,int r)//找不上升
{
if(tr[now].l==l && tr[now].r==r) return tr[now].maxdown;
int lc=tr[now].lc,rc=tr[now].rc;
int mid=(tr[now].l+tr[now].r)/2;
if(r<=mid) return findmaxdown(lc,l,r);
else if(l>mid) return findmaxdown(rc,l,r);
else
{
int cnt=max(findmaxdown(lc,l,mid),findmaxdown(rc,mid+1,r));
if(col[tr[rc].l]<=0)
{
int ss=min(tr[rc].zlow,r-mid);
ss+=min(tr[lc].ylow,mid-l+1);
return max(cnt,ss);
}
return cnt;
}
}
int main()
{
int n,x,y,m;
scanf("%d",&n);
bt(1,n);
int last=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
col[i]=x-last;
last=x;
change(1,i,col[i]);
}
scanf("%d",&m);
while(m--)
{
scanf("%d%d",&x,&y);
int mup=findmaxup(1,x,y);
int mdw=findmaxdown(1,x,y);
printf("%d\n",max(mup,mdw));
}
return 0;
}
/*
7
6 8 5 42 5 1 8
6
1 7
2 6
4 5
3 5
4 6
2 2
*/