BZOJ 4491: 我也不知道题目名字是什么 线段树+离线

code: 

#include <string> 
#include <cstring>  
#include <cstdio> 
#include <algorithm>      
#define N 50003     
#define lson now<<1 
#define rson now<<1|1   
#define inf 100000000   
using namespace std; 
void setIO(string s) {
    string in=s+".in"; 
    string out=s+".out"; 
    freopen(in.c_str(),"r",stdin); 
    // freopen(out.c_str(),"w",stdout); 
}         
int n; 
int sm[N]; 
int bg[N];   
int pos[N]; 
int val[N];  
int mx[N<<2]; 
int mn[N<<2];   
int answer[N];  
struct node {
    int x; 
    int id;     
    node(int x=0,int id=0):x(x),id(id){}  
}a[N<<1];   
struct data {
    int l,r,id; 
    data(int l=0,int r=0,int id=0):l(l),r(r),id(id){}     
}e[N];   
bool cmp(node a,node b) {
    return a.x==b.x?a.id<b.id:a.x<b.x;  
}
bool cmp_data(data a,data b) {
    return a.r<b.r;  
}  
void build(int l,int r,int now) {
    if(l==r) {
        mn[now]=inf; 
        mx[now]=-inf; 
        return; 
    } 
    int mid=(l+r)>>1; 
    build(l,mid,lson); 
    build(mid+1,r,rson);         
}
void fix_mn(int l,int r,int now,int p,int v) {   
    if(l==r) {
        mn[now]=v;        
        return; 
    } 
    int mid=(l+r)>>1;        
    if(p<=mid) {
        fix_mn(l,mid,lson,p,v); 
    } 
    else {
        fix_mn(mid+1,r,rson,p,v); 
    }        
    mn[now]=min(mn[lson],mn[rson]);   
}   
void fix_mx(int l,int r,int now,int p,int v) { 
    if(l==r) {
        mx[now]=v;    
        return; 
    }    
    int mid=(l+r)>>1;    
    if(p<=mid) {
        fix_mx(l,mid,lson,p,v); 
    } 
    else {
        fix_mx(mid+1,r,rson,p,v);  
    }
    mx[now]=max(mx[lson],mx[rson]);   
} 
int que_mn(int l,int r,int now,int L,int R) {
    if(l>=L&&r<=R) {
        return mn[now]; 
    } 
    int mid=(l+r)>>1,re=inf;  
    if(L<=mid) {
        re=min(re,que_mn(l,mid,lson,L,R)); 
    } 
    if(R>mid) {
        re=min(re,que_mn(mid+1,r,rson,L,R));      
    } 
    return re; 
}  
int que_mx(int l,int r,int now,int L,int R) {
    if(l>=L&&r<=R) {
        return mx[now]; 
    } 
    int mid=(l+r)>>1,re=-inf;   
    if(L<=mid) {
        re=max(re,que_mx(l,mid,lson,L,R)); 
    } 
    if(R>mid) {
        re=max(re,que_mx(mid+1,r,rson,L,R)); 
    } 
    return re;  
}
int main() { 
    // setIO("input");      
    int i,j,cnt=0; 
    scanf("%d",&n); 
    for(i=1;i<=n;++i) {
        scanf("%d",&val[i]); 
    }  
    pos[n]=sm[n]=bg[n]=n;   
    for(i=n-1;i>=1;--i) {
        sm[i]=(val[i+1]<=val[i]?sm[i+1]:i);   
        bg[i]=(val[i+1]>=val[i]?bg[i+1]:i);   
        pos[i]=max(sm[i],bg[i]);          
        a[++cnt]=node(i,0);   
        a[++cnt]=node(pos[i],i);   
    }                              
    sort(a+1,a+1+cnt,cmp);   
    int q; 
    scanf("%d",&q); 
    for(i=1;i<=q;++i) {
        scanf("%d%d",&e[i].l,&e[i].r); 
        e[i].id=i;  
    }     
    sort(e+1,e+1+q,cmp_data);   
    build(1,n,1);    
    for(i=j=1;i<=q;++i) {       
        while(j<=cnt&&a[j].x<=e[i].r) {
            if(a[j].id) {   
                fix_mn(1,n,1,a[j].id,inf);     
                fix_mx(1,n,1,a[j].id,a[j].x-a[j].id+1);   
            } 
            else {     
                fix_mn(1,n,1,a[j].x,a[j].x);   
            }
            ++j; 
        }
        answer[e[i].id]=max(e[i].r-que_mn(1,n,1,e[i].l,e[i].r)+1, que_mx(1,n,1,e[i].l,e[i].r));  
    }
    for(i=1;i<=q;++i) {
        printf("%d\n",answer[i]); 
    }
    return 0; 
}

  

posted @ 2019-12-21 09:33  EM-LGH  阅读(148)  评论(0编辑  收藏  举报