QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#537627#7523. Partially Free MealOIer_kzc#TL 1ms7880kbC++172.8kb2024-08-30 16:58:452024-08-30 16:58:45

Judging History

你现在查看的是最新测评结果

  • [2024-08-30 16:58:45]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:7880kb
  • [2024-08-30 16:58:45]
  • 提交

answer

#include <stdio.h>
#include <string.h>

#include <vector>
#include <algorithm>
#include <numeric>

#define LOG(FMT...) fprintf(stderr, FMT)

#define eb emplace_back

using namespace std;

typedef long long LL;
constexpr int N = 200005, TC = 30 * N;

int n;
struct Pair {
	int a, b, k;
} w[N];
bool cmpA(const Pair &x, const Pair &y) {
	return x.a < y.a;
}
bool cmpB(const Pair &x, const Pair &y) {
	return x.b < y.b;
}

#define L (tr[z].l)
#define R (tr[z].r)

struct Tree {
	int l, r, c; LL v;
} tr[TC];
int rt[N], ids;

void mdf(int &z, int x, int c, int l = 0, int r = n) {
	z = ++ids;
	tr[z].v = c;
	tr[z].c = 1;
	if (l == r) {
		return;
	}
	int mid = l + r >> 1;
	if (x <= mid) {
		mdf(L, x, c, l, mid);
	} else {
		mdf(R, x, c, mid + 1, r);
	}
}

void merge(int &y, int x, int l = 0, int r = n) {
	if (!x) {
		return;
	}
	if (!y) {
		y = x;
		return;
	}
	tr[y].v += tr[x].v;
	tr[y].c += tr[x].c;
	if (l == r) {
		return;
	}
	int mid = l + r >> 1;
	merge(tr[y].l, tr[x].l, l, mid);
	merge(tr[y].r, tr[x].r, mid + 1, r);
}

struct Dat {
	int c; LL d;
	bool operator < (const Dat &t) const {
		return c < t.c || c == t.c && d < t.d;
	}
	Dat operator + (int tc) const {
		return (Dat){c + tc, d};
	}
} q[N];

Dat qry(int z, LL v, int l = 0, int r = n) {
	if (l == r) {
		return (Dat){0, v};
	}
	int mid = l + r >> 1;
	if (tr[L].v <= v) {
		return qry(R, v - tr[L].v, mid + 1, r) + tr[L].c;
	}
	return qry(L, v, l, mid);
}

LL res[N];

void solve(int l, int r, LL vl, LL vr, const vector<int> &v) {
	if (l > r) {
		return;
	}
	if (vl == vr) {
		for (int k = l; k <= r; ++k) {
			res[k] = vr;
		}
		return;
	}
	LL md = (vl + vr) >> 1;
	int maxc = 0;
	for (int i = 0; i < v.size(); ++i) {
		int x = v[i];
		q[i] = qry(rt[x], md - w[x].b);
		maxc = max(maxc, q[i].c);
	}
	
	vector<int> nv;
	Dat t = (Dat){-1, -1};
	for (int i = 0; i < v.size(); ++i) {
		if (!(q[i] < t)) {
			nv.eb(v[i]);
			t = q[i];
		}
	}
	solve(l, maxc, vl, md, v);
	
	nv.clear();
	t = (Dat){-1, -1};
	for (int i = v.size() - 1; ~i; --i) {
		if (t < q[i]) {
			nv.eb(v[i]);
			t = q[i];
		}
	}
	reverse(nv.begin(), nv.end());
	solve(maxc + 1, r, md + 1, vr, nv);
}

int main() {
	scanf("%d", &n);
	LL suma = 0; int maxb = 0;
	for (int i = 1; i <= n; ++i) {
		scanf("%d%d", &w[i].a, &w[i].b);
		suma += w[i].a;
		maxb = max(maxb, w[i].b);
	}
	sort(w + 1, w + n + 1, cmpA);
	for (int i = 1; i <= n; ++i) {
		w[i].k = i - 1;
	}
	sort(w + 1, w + n + 1, cmpB);
	for (int i = 1; i <= n; ++i) {
		mdf(rt[i], w[i].k, w[i].a);
		merge(rt[i], rt[i - 1]);
	}
	vector<int> v(n);
	iota(v.begin(), v.end(), 1);
	solve(1, n, 0, suma + maxb, v);
	for (int i = 1; i <= n; ++i) {
		printf("%lld\n", res[i]);
	}
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 7880kb

input:

3
2 5
4 3
3 7

output:

7
11
16

result:

ok 3 lines

Test #2:

score: -100
Time Limit Exceeded

input:

200000
466436993 804989151
660995237 756645598
432103296 703610564
6889895 53276988
873617076 822481192
532911431 126844295
623111499 456772252
937464699 762157133
708503076 786039753
78556972 5436013
582960979 398984169
786333369 325119902
930705057 615928139
924915828 506145001
164984329 208212435...

output:


result: