http://codeforces.com/contest/484/problem/E
题意:
给出n个数,查询最大的在区间[l,r]内,长为w的子区间的最小值
第i棵线段树表示>=i的数
维护最长连续子区间
把数从大到小插入主席树
对于每个询问,二分x
在第x棵线段树中查,若最长连续子区间>=w,到代表更大的线段树中查
没有建第n+1棵线段树,导致前面节点的siz不对,WA了一次
#include<cstdio> #include<iostream> #include<algorithm> using namespace std; #define N 100001 pair<int,int>a[N]; int tot; int root[N+1],lc[N*20],rc[N*20]; struct node { int siz; int mx,lmax,rmax; node operator + (node p) { node c; c.siz=siz+p.siz; c.lmax=lmax; if(lmax==siz) c.lmax+=p.lmax; c.rmax=p.rmax; if(p.rmax==p.siz) c.rmax+=rmax; c.mx=max(mx,p.mx); c.mx=max(c.mx,rmax+p.lmax); return c; } }e[N*20]; void read(int &x) { x=0; char c=getchar(); while(!isdigit(c)) c=getchar(); while(isdigit(c)) { x=x*10+c-'0'; c=getchar(); } } void build(int &k,int l,int r) { e[k=++tot].siz=r-l+1;; if(l==r) return; int mid=l+r>>1; build(lc[k],l,mid); build(rc[k],mid+1,r); } void insert(int pre,int &k,int l,int r,int pos) { k=++tot; if(l==r) { e[k].siz=1; e[k].mx=e[k].lmax=e[k].rmax=1; return; } int mid=l+r>>1; if(pos<=mid) { rc[k]=rc[pre]; insert(lc[pre],lc[k],l,mid,pos); } else { lc[k]=lc[pre]; insert(rc[pre],rc[k],mid+1,r,pos); } e[k]=e[lc[k]]+e[rc[k]]; } node query(int k,int l,int r,int opl,int opr) { if(l>=opl && r<=opr) return e[k]; int mid=l+r>>1; if(opr<=mid) return query(lc[k],l,mid,opl,opr); if(opl>mid) return query(rc[k],mid+1,r,opl,opr); return query(lc[k],l,mid,opl,opr)+query(rc[k],mid+1,r,opl,opr); } int main() { //freopen("data.in","r",stdin); //freopen("my.out","w",stdout); int n; read(n); for(int i=1;i<=n;++i) { read(a[i].first); a[i].second=i; } sort(a+1,a+n+1); build(root[n+1],1,n); for(int i=n;i;--i) insert(root[i+1],root[i],1,n,a[i].second); int m; read(m); int s,t,w; int l,r,mid; int ans; while(m--) { read(s); read(t); read(w); l=1,r=n; ans=0; while(l<=r) { mid=l+r>>1; if(query(root[mid],1,n,s,t).mx>=w) ans=mid,l=mid+1; else r=mid-1; } cout<<a[ans].first<<' '; } }
Bizon the Champion has recently finished painting his wood fence. The fence consists of a sequence of n panels of 1 meter width and of arbitrary height. The i-th panel's height is hi meters. The adjacent planks follow without a gap between them.
After Bizon painted the fence he decided to put a "for sale" sign on it. The sign will be drawn on a rectangular piece of paper and placed on the fence so that the sides of the sign are parallel to the fence panels and are also aligned with the edges of some panels. Bizon the Champion introduced the following constraints for the sign position:
- The width of the sign should be exactly w meters.
- The sign must fit into the segment of the fence from the l-th to the r-th panels, inclusive (also, it can't exceed the fence's bound in vertical direction).
The sign will be really pretty, So Bizon the Champion wants the sign's height to be as large as possible.
You are given the description of the fence and several queries for placing sign. For each query print the maximum possible height of the sign that can be placed on the corresponding segment of the fence with the given fixed width of the sign.
The first line of the input contains integer n — the number of panels in the fence (1 ≤ n ≤ 105).
The second line contains n space-separated integers hi, — the heights of the panels (1 ≤ hi ≤ 109).
The third line contains an integer m — the number of the queries (1 ≤ m ≤ 105).
The next m lines contain the descriptions of the queries, each query is represented by three integers l, r and w (1 ≤ l ≤ r ≤ n,1 ≤ w ≤ r - l + 1) — the segment of the fence and the width of the sign respectively.
For each query print the answer on a separate line — the maximum height of the sign that can be put in the corresponding segment of the fence with all the conditions being satisfied.
5
1 2 2 3 3
3
2 5 3
2 5 2
1 5 5
2
3
1
The fence described in the sample looks as follows:
The possible positions for the signs for all queries are given below.