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
Sample Output
6
6
5
6
4
HINT
//样例解释
五个询问分别对应
[1,6][1,6][2,6][1,6][6,9]
题解
差分一下
转化成求一段均大于等于0或者均小于等于0的最长
线段树瞎维护即可
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
int lc,rc,l,r;
int ul,ur,dl,dr;//upl upr downl downr
int mu,md;//maxup maxdown
}tr[110000];int trlen;
void bt(int l,int r)
{
int now=++trlen;
tr[now].l=l;tr[now].r=r;
tr[now].lc=tr[now].rc=-1;
tr[now].ul=tr[now].ur=tr[now].dl=tr[now].dr=tr[now].mu=tr[now].md=0;
if(l<r)
{
int mid=(l+r)/2;
tr[now].lc=trlen+1;bt(l,mid);
tr[now].rc=trlen+1;bt(mid+1,r);
}
}
int col[51000];
void upd(int now,int lc,int rc)
{
//最长不下降
tr[now].mu=max(tr[lc].mu,tr[rc].mu);
if(col[tr[rc].l]>=0)tr[now].mu=max(tr[now].mu,tr[lc].ur+tr[rc].ul);//交界
tr[now].ul=tr[lc].ul;
if(tr[lc].ul==tr[lc].r-tr[lc].l+1 && col[tr[rc].l]>=0)tr[now].ul=tr[lc].ul+tr[rc].ul;
tr[now].ur=tr[rc].ur;
if(tr[rc].ur==tr[rc].r-tr[rc].l+1 && col[tr[rc].l]>=0)tr[now].ur=tr[rc].ur+tr[lc].ur;
//最长不上升
tr[now].md=max(tr[lc].md,tr[rc].md);
if(col[tr[rc].l]<=0)tr[now].md=max(tr[now].md,tr[lc].dr+tr[rc].dl);//交界
tr[now].dl=tr[lc].dl;
if(tr[lc].dl==tr[lc].r-tr[lc].l+1 && col[tr[rc].l]<=0)tr[now].dl=tr[lc].dl+tr[rc].dl;
tr[now].dr=tr[rc].dr;
if(tr[rc].dr==tr[rc].r-tr[rc].l+1 && col[tr[rc].l]<=0)tr[now].dr=tr[rc].dr+tr[lc].dr;
}
void change(int now,int p,int c)
{
if(tr[now].l==tr[now].r)
{
tr[now].ul=tr[now].ur=tr[now].dl=tr[now].dr=tr[now].mu=tr[now].md=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,c);
else change(rc,p,c);
upd(now,lc,rc);
}
int findmaxu(int now,int l,int r)
{
if(tr[now].l==l && tr[now].r==r)return tr[now].mu;
int lc=tr[now].lc,rc=tr[now].rc;
int mid=(tr[now].l+tr[now].r)/2;
if(r<=mid)return findmaxu(lc,l,r);
else if(mid+1<=l)return findmaxu(rc,l,r);
else
{
int tmp=max(findmaxu(lc,l,mid),findmaxu(rc,mid+1,r));
if(col[tr[rc].l]>=0)
{
int cnt=min(tr[rc].ul,r-mid);
cnt+=min(tr[lc].ur,mid-l+1);
return max(tmp,cnt);
}
return tmp;
}
}
int findmaxd(int now,int l,int r)
{
if(tr[now].l==l && tr[now].r==r)return tr[now].md;
int lc=tr[now].lc,rc=tr[now].rc;
int mid=(tr[now].l+tr[now].r)/2;
if(r<=mid)return findmaxd(lc,l,r);
else if(mid+1<=l)return findmaxd(rc,l,r);
else
{
int tmp=max(findmaxd(lc,l,mid),findmaxd(rc,mid+1,r));
if(col[tr[rc].l]<=0)
{
int cnt=min(tr[rc].dl,r-mid);
cnt+=min(tr[lc].dr,mid-l+1);
return max(tmp,cnt);
}
return tmp;
}
}
int n,m;
int main()
{
scanf("%d",&n);bt(1,n);
int last=0;
for(int i=1;i<=n;i++){int x;scanf("%d",&x);col[i]=x-last;last=x;}
for(int i=1;i<=n;i++)change(1,i,col[i]);
scanf("%d",&m);
while(m--)
{
int u,v;
scanf("%d%d",&u,&v);
int tx=findmaxu(1,u,v);
int ty=findmaxd(1,u,v);
printf("%d\n",max(tx,ty));
}
return 0;
}

本文介绍了一种利用线段树进行区间查询的方法,以解决寻找给定序列中特定子串的问题。具体地,针对每个询问,找出指定范围内最长的不上升或不下降子串,并详细阐述了解决方案的实现细节。
408

被折叠的 条评论
为什么被折叠?



