CodeForces 484E Sign on Fence (主席树+二分+排序思维)

#include<bits/stdc++.h>
using namespace std;
#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long
#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first                                  
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e5+100;
const int ub=1e6;
const ll INF=1e18;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){
    if(y==0) return x;
    return gcd(y,x%y);
}
int n,q,b[maxn];///存储排序好的用来二分的
pii p[maxn];
///持久化线段树
int root[maxn],ch[maxn*30][2],tot=0;
struct N{
    int maxv,lc,rc;
}node[maxn*30];
void update(int old,int &now,int l,int r,int pos){
    now=++tot;
    ch[now][0]=ch[old][0],ch[now][1]=ch[old][1];
    node[now]=node[old];
    if(l==r){node[now]=N{1,1,1};return ;}
    int mid=l+r>>1;
    if(pos<=mid) update(ch[old][0],ch[now][0],l,mid,pos);
    if(mid<pos) update(ch[old][1],ch[now][1],mid+1,r,pos);
    int x=ch[now][0],y=ch[now][1];
    node[now].maxv=max(node[x].rc+node[y].lc,max(node[x].maxv,node[y].maxv));
    node[now].lc=node[x].lc,node[now].rc=node[y].rc;
    if(node[x].maxv==mid-l+1) node[now].lc+=node[y].lc;
    if(node[y].maxv==r-mid) node[now].rc+=node[x].rc;
}
int query(int now,int l,int r,int L,int R){
    if(L<=l&&r<=R) return node[now].maxv;
    int mid=l+r>>1,ans=0;
    if(L<=mid) ans=max(ans,query(ch[now][0],l,mid,L,R));
    if(mid<R) ans=max(ans,query(ch[now][1],mid+1,r,L,R));
    if(L<=mid&&mid<R){
        int tmp=min(node[ch[now][0]].rc,mid-L+1)+min(R-mid,node[ch[now][1]].lc);
        ans=max(ans,tmp);
    }
    return ans;
}
map<int,int> mp;
int l,r,ans,cnt;
int main(){/*
    freopen("d://in.txt","r",stdin);
    freopen("d://out.txt","w",stdout);*/
    ios::sync_with_stdio(false);
    cin>>n;
    rep(i,1,n+1) {
        int x;cin>>x;
        b[i]=x;
        p[i]=mk(-x,i);
    }
    sort(b+1,b+1+n);
    cnt=unique(b+1,b+1+n)-b-1;
    sort(p+1,p+1+n);
    rep(i,1,n+1){
        update(root[i-1],root[i],1,n,p[i].se);
        p[i].fi=-p[i].fi;
        mp[p[i].fi]=root[i];
    }
    cin>>q;
    while(q--){
        int x,y,w;
        cin>>x>>y>>w;
        l=1,r=cnt,ans=1;
        while(l<=r){
            int mid=l+r>>1;
            if(query(mp[b[mid]],1,n,x,y)>=w) ans=mid,l=mid+1;
            else r=mid-1;
        }
        cout<<b[ans]<<"\n";
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值