题解 宝藏

传送门

很遗憾的题,开始写的时候还记得有单调性写着写着就忘了

  • 所以看出来有什么特殊性质先写下来,要不然容易自闭

先考虑 \(Q=1\) 怎么做
那就直接枚举答案
按权值排序,枚举一个答案后将答案左右两边的区间分别按时间排序,取前 \(\frac{x}{2}\) 小的
发现这个东西可以权值线段树上二分优化到单次 \(nlogn\)
然后又发现随 \(x\) 递增答案递减
于是 \(nlogn\) 预处理出所有可能的 \(x\) 对应的答案,\(O(1)\) 回答

Code:
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define N 300010
#define ll long long
#define fir first
#define sec second
#define make make_pair
//#define int long long

char buf[1<<21], *p1=buf, *p2=buf;
#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf, 1, 1<<21, stdin)), p1==p2?EOF:*p1++)
inline ll read() {
	ll ans=0, f=1; char c=getchar();
	while (!isdigit(c)) {if (c=='-') f=-f; c=getchar();}
	while (isdigit(c)) {ans=(ans<<3)+(ans<<1)+(c^48); c=getchar();}
	return ans*f;
}

int n, q;
ll w[N], t[N], T;
int uni[N], usize;

namespace force{
	int ans[N];
	priority_queue<pair<int, int>> q2;
	struct seg{
		int tl[N<<2], tr[N<<2];
		ll cnt[N<<2], sum[N<<2];
		// multiset<int> s[N<<2];
		#define tl(p) tl[p]
		#define tr(p) tr[p]
		#define cnt(p) cnt[p]
		#define sum(p) sum[p]
		// #define s(p) s[p]
		#define pushup(p) cnt(p)=cnt(p<<1)+cnt(p<<1|1), sum(p)=sum(p<<1)+sum(p<<1|1)
		void build(int p, int l, int r) {
			tl(p)=l; tr(p)=r; cnt(p)=sum(p)=0; //s(p).clear();
			if (l==r) return ;
			int mid=(l+r)>>1;
			build(p<<1, l, mid);
			build(p<<1|1, mid+1, r);
		}
		void add(int p, int pos, int val) {
			if (tl(p)==tr(p)) {
				++cnt(p);
				sum(p)=cnt(p)*uni[tl(p)];
				// s(p).insert(val);
				return ;
			}
			int mid=(tl(p)+tr(p))>>1;
			if (pos<=mid) add(p<<1, pos, val);
			else add(p<<1|1, pos, val);
			pushup(p);
		}
		void del(int p, int pos, int val) {
			if (tl(p)==tr(p)) {
				--cnt(p);
				sum(p)=cnt(p)*uni[tl(p)];
				// s(p).erase(s(p).find(val));
				return ;
			}
			int mid=(tl(p)+tr(p))>>1;
			if (pos<=mid) del(p<<1, pos, val);
			else del(p<<1|1, pos, val);
			pushup(p);
		}
		ll find(int p, int val) {
			if (!val) return 0;
			if (val>cnt(p)) return T+1;
			if (tl(p)==tr(p)) return val*uni[tl(p)];
			if (val<=cnt(p<<1)) return find(p<<1, val);
			else return sum(p<<1)+find(p<<1|1, val-cnt(p<<1));
		}
	}left, right;
	void solve() {
		left.build(1, 1, usize);
		right.build(1, 1, usize);
		memset(ans, -1, sizeof(ans));
		for (int j=1; j<=n; ++j) q2.push(make(w[j], t[j])), left.add(1, t[j], w[j]);
		int pos=n;
		for (int i=1; i<=n; ++i) if (i&1) {
			for (; pos; --pos) {
				// cout<<"j: "<<j<<endl;
				pair<int, int> u=q2.top(); q2.pop();
				// cout<<"u: "<<u.fir<<' '<<u.sec<<endl;
				left.del(1, u.sec, u.fir);
				if (left.find(1, i/2)+uni[u.sec]+right.find(1, i/2)<=T) {
					ans[i]=u.fir;
					left.add(1, u.sec, u.fir);
					q2.push(u);
					break;
				}
				right.add(1, u.sec, u.fir);
			}
		}
		for (int i=1; i<=q; ++i) printf("%d\n", ans[read()]);
		exit(0);
	}
}

signed main()
{
	freopen("treasure.in", "r", stdin);
	freopen("treasure.out", "w", stdout);

	n=read(); T=read(); q=read();
	for (int i=1; i<=n; ++i) w[i]=read(), t[i]=read(), uni[i]=t[i];
	sort(uni+1, uni+n+1);
	usize=unique(uni+1, uni+n+1)-uni-1;
	for (int i=1; i<=n; ++i) t[i]=lower_bound(uni+1, uni+usize+1, t[i])-uni;
	force::solve();

	return 0;
}
posted @ 2021-10-27 21:17  Administrator-09  阅读(0)  评论(0编辑  收藏  举报