[bzoj4491][线段树]我也不知道题目名字是什么

39 篇文章 0 订阅

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值