NOIP 模拟 $84\; \rm 宝藏$
题解 \(by\;zj\varphi\)
显然,\(x\) 越大,答案单调不增,所以可以对 \(w\) 进行排序,从大往小扫。
\(x\) 从小到大,维护一个单调指针。
假设当前答案为 \(p\) 位置,那么在 \(p\) 位置之前一定选 \(\frac{x}{2}\) 个最小的,在其后同理。直接在权值线段树上二分 \(\frac{x}{2}\) 个最小的即可。
Code
#include<bits/stdc++.h>
#define ri signed
#define pd(i) ++i
#define bq(i) --i
#define func(x) std::function<x>
namespace IO{
char buf[1<<21],*p1=buf,*p2=buf;
#define gc() p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?(-1):*p1++
#define debug1(x) std::cerr << #x"=" << x << ' '
#define debug2(x) std::cerr << #x"=" << x << std::endl
#define Debug(x) assert(x)
struct nanfeng_stream{
template<typename T>inline nanfeng_stream &operator>>(T &x) {
bool f=false;x=0;char ch=gc();
while(!isdigit(ch)) f|=ch=='-',ch=gc();
while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=gc();
return x=f?-x:x,*this;
}
}cin;
}
using IO::cin;
namespace nanfeng{
#define FI FILE *IN
#define FO FILE *OUT
template<typename T>inline T cmax(T x,T y) {return x>y?x:y;}
template<typename T>inline T cmin(T x,T y) {return x>y?y:x;}
using ll=long long;
static const int N=3e5+7,MX=1e6+1;
int ans[N],n,Q,lft;
ll T,tmp;
struct Pair{int w,t;}pr[N];
struct Seg{
#define ls(x) T[x].l
#define rs(x) T[x].r
#define up(x) T[x].sum=T[ls(x)].sum+T[rs(x)].sum,\
T[x].nm=T[ls(x)].nm+T[rs(x)].nm;
struct segmenttree{ll sum;int l,r,nm;}T[N<<5];
int tot,rt;
func(void(int&,int,int,int,int)) update=[&](int &x,int pos,int k,int l,int r) {
if (!x) x=++tot;
if (l==r) return T[x].sum+=pos*k,T[x].nm+=k,void();
int mid=(l+r)>>1;
if (pos<=mid) update(ls(x),pos,k,l,mid);
else update(rs(x),pos,k,mid+1,r);
up(x);
};
func(void(int,int,int)) query=[&](int x,int l,int r) {
if (!x) return;
if (l==r) return tmp+=1ll*l*lft,void();
int mid=(l+r)>>1;
if (T[ls(x)].nm>lft) query(ls(x),l,mid);
else if (T[ls(x)].nm==lft) tmp+=T[ls(x)].sum,lft=0;
else lft-=T[ls(x)].nm,tmp+=T[ls(x)].sum,query(rs(x),mid+1,r);
};
}T1,T2;
inline int main() {
FI=freopen("treasure.in","r",stdin);
FO=freopen("treasure.out","w",stdout);
cin >> n >> T >> Q;
memset(ans+1,-1,sizeof(int)*n);
for (ri i(1);i<=n;pd(i)) cin >> pr[i].w >> pr[i].t,++pr[i].t;
std::sort(pr+1,pr+n+1,[](Pair p1,Pair p2) {return p1.w<p2.w;});
int p=1;
for (ri i(1);i<n;pd(i)) T1.update(T1.rt,pr[i].t,1,1,MX);
for (ri i(n);i;bq(i)) {
int lim=cmin(i-1,n-i);
while((p>>1)<=lim)
if (p==1) {
if (pr[i].t<=T+1) ans[p]=pr[i].w,p+=2;
else break;
} else {
lft=p>>1,tmp=0;
T1.query(1,1,MX);
lft=p>>1;
T2.query(1,1,MX);
if (tmp+pr[i].t<=T+p) ans[p]=pr[i].w,p+=2;
else break;
}
if (p>n) break;
T1.update(T1.rt,pr[i-1].t,-1,1,MX);
T2.update(T2.rt,pr[i].t,1,1,MX);
}
for (ri i(1),x;i<=Q;pd(i)) cin >> x,printf("%d\n",x>n?-1:ans[x]);
return 0;
}
}
int main() {return nanfeng::main();}