QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#689757#9252. Penguins in Refrigeratorreal_sigma_teamWA 185ms19500kbC++234.1kb2024-10-30 18:27:552024-10-30 18:27:56

Judging History

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

  • [2024-10-30 18:27:56]
  • 评测
  • 测评结果:WA
  • 用时:185ms
  • 内存:19500kb
  • [2024-10-30 18:27:55]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

constexpr int mod = 1'000'000'007;

mt19937 mt(852095);

int add(int a, int b) {
	return a + b >= mod ? a + b - mod : a + b;
}

int sub(int a, int b) {
	return a >= b ? a - b : a + mod - b;
}

int mul(int a, int b) {
	return 1ll * a * b % mod;
}

struct node {
	int v, mx, sz, l, r, y, w;
	pair<int, int> mnw, mxw;
	node() {
		mnw = {INT32_MAX / 2, -1};
		mxw = {-1, -1};
		w = 0;
		v = -1;
		mx = -1;
		sz = 1;
		l = 0;
		r = 0;
		y = mt();
	}
};

struct treap {
	vector<node> tr;
	treap() {
		tr.emplace_back();
		tr[0].y = 0;
		tr[0].sz = 0;
	}
	void update(int i) {
		if (!i) {
			return;
		}
		tr[i].sz = 1 + tr[tr[i].l].sz + tr[tr[i].r].sz;
		tr[i].mx = max({tr[tr[i].l].mx, tr[tr[i].r].mx, tr[i].v});
		tr[i].mxw = max({tr[tr[i].l].mxw, make_pair(tr[tr[i].r].mxw.first, tr[tr[i].r].mxw.second + tr[tr[i].l].sz + 1), make_pair(tr[i].w, tr[tr[i].l].sz)});
		tr[i].mnw = min({tr[tr[i].l].mnw, make_pair(tr[tr[i].r].mnw.first, tr[tr[i].r].mnw.second + tr[tr[i].l].sz + 1), make_pair(tr[i].w, tr[tr[i].l].sz)});
	}
	pair<int, int> split(int i, int sz) {
		if (!i) {
			return {0, 0};
		}
		if (!sz) {
			return {0, i};
		}
		if (sz == tr[i].sz) {
			return {i, 0};
		}
		if (tr[tr[i].l].sz >= sz) {
			auto [nl, nr] = split(tr[i].l, sz);
			tr[i].l = nr;
			update(i);
			return {nl, i};
		}
		auto [nl, nr] = split(tr[i].r, sz - tr[tr[i].l].sz - 1);
		tr[i].r = nl;
		update(i);
		return {i, nr};
	}
	pair<int, int> splitv(int i, int v) {
		if (!i) {
			return {0, 0};
		}
		if (tr[i].mx < v) {
			return {i, 0};
		}
		if (tr[tr[i].l].mx >= v || tr[i].v >= v) {
			auto [nl, nr] = splitv(tr[i].l, v);
			tr[i].l = nr;
			update(i);
			return {nl, i};
		}
		auto [nl, nr] = splitv(tr[i].r, v);
		tr[i].r = nl;
		update(i);
		return {i, nr};
	}
	int merge(int a, int b) {
		if (a == 0) {
			return b;
		}
		if (b == 0) {
			return a;
		}
		if (tr[a].y > tr[b].y) {
			tr[a].r = merge(tr[a].r, b);
			update(a);
			return a;
		}
		tr[b].l = merge(a, tr[b].l);
		update(b);
		return b;
	}
	int insert(int root, int i) {
		if (root == 0) {
			return i;
		}
		if (tr[i].y > tr[root].y) {
			auto [nl, nr] = splitv(root, tr[i].v);
			tr[i].l = nl;
			tr[i].r = nr;
			update(i);
			return i;
		}
		if (tr[tr[root].l].mx >= tr[i].v || tr[root].v >= tr[i].v) {
			tr[root].l = insert(tr[root].l, i);
			update(root);
			return root;
		}
		tr[root].r = insert(tr[root].r, i);
		update(root);
		return root;
	}
	int add_val(int v, int w) {
		int res = tr.size();
		tr.emplace_back();
		tr[res].v = tr[res].mx = v;
		tr[res].mxw = tr[res].mnw = {w, 0};
		tr[res].w = w;
		return res;
	}
	void dfs(int root, vector<int>& ans) {
		if (!root) {
			return;
		}
		dfs(tr[root].l, ans);
		ans.push_back(tr[root].v);
		dfs(tr[root].r, ans);
	}
	node operator[](int i) {
		return tr[i];
	}
};

constexpr int N = 200200;
int a[N], w[N], W;
treap tr;

pair<int, int> rec(int root) {
	if (root == 0) {
		return {1, 0};
	}
	auto [mn, mni] = tr[root].mnw;
	auto [mx, mxi] = tr[root].mxw;
	if (mx + mn > W) {
		auto [r1, rhui] = tr.split(root, mxi);
		auto [r, r2] = tr.split(rhui, 1);
		auto [cnt1, r3] = rec(r1);
		auto [cnt2, r4] = rec(r2);
		int cnt = mul(cnt1, cnt2);
		root = tr.merge(r3, r);
		root = tr.merge(root, r4);
		return {cnt, root};
	}
	auto [r1, rhui] = tr.split(root, mni);
	auto [r, r2] = tr.split(rhui, 1);
	root = tr.merge(r1, r2);
	auto [cnt, r4] = rec(root);
	root = tr.insert(r4, r);
	cnt = mul(cnt, tr[root].sz);
	return {cnt, root};
}

int32_t main() {
	std::cin.tie(nullptr)->sync_with_stdio(false);
	
	int n;
	cin >> n >> W;
	int root = 0;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int i = 0; i < n; i++) {
		cin >> w[i];
	}
	reverse(a, a + n);
	reverse(w, w + n);
	for (int i = 0; i < n; i++) {
		root = tr.merge(root, tr.add_val(a[i], w[i]));
	}
	auto [cnt, r] = rec(root);
	vector<int> ans;
	tr.dfs(r, ans);
	cout << cnt << '\n';
	for (int i = 0; i < n; i++) {
		cout << ans[i] << ' ';
	}
	cout << '\n';
}

详细

Test #1:

score: 100
Accepted
time: 0ms
memory: 3636kb

input:

5 10
1 2 3 4 5
6 5 3 9 2

output:

3
5 4 2 1 3 

result:

ok 2 lines

Test #2:

score: 0
Accepted
time: 0ms
memory: 3860kb

input:

5 10
1 2 3 4 5
2 4 3 3 8

output:

30
1 5 2 3 4 

result:

ok 2 lines

Test #3:

score: 0
Accepted
time: 0ms
memory: 3616kb

input:

5 10
1 2 3 4 5
2 3 4 5 1

output:

120
1 2 3 4 5 

result:

ok 2 lines

Test #4:

score: 0
Accepted
time: 0ms
memory: 3576kb

input:

5 10
1 2 3 4 5
2 3 4 5 6

output:

60
1 2 3 5 4 

result:

ok 2 lines

Test #5:

score: 0
Accepted
time: 185ms
memory: 19500kb

input:

100000 96
1996 78922 45321 68844 32404 82013 66552 81163 17216 48170 35495 56660 13480 43118 23173 47257 50168 87069 26167 67231 31758 25694 61063 56642 8923 7727 54528 96554 38964 7604 6822 16256 45300 58869 31359 48638 87645 14779 81505 59585 89293 9291 7002 31810 84701 77648 78295 42595 11394 479...

output:

457992974
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 10...

result:

ok 2 lines

Test #6:

score: -100
Wrong Answer
time: 56ms
memory: 9908kb

input:

100000 84
93330 3894 94859 22134 49668 30606 26739 82976 76701 56323 75537 7626 87226 20857 98177 21811 70827 75898 8111 48223 26186 64222 63002 79024 19126 41638 1048 43857 25379 19764 60207 27675 77665 66327 6274 34861 30287 13449 64505 51490 5804 65843 49014 85795 12365 31565 34411 71697 66568 28...

output:

778158997
1146 2481 2800 8588 14089 15421 17476 20123 27661 31659 32947 33622 35785 36013 43919 59690 56945 4491 18825 43842 39523 62369 74728 78078 82792 88414 89122 28520 41413 24857 87721 94788 98143 80586 69064 29028 30999 34715 36175 54502 75343 88502 26125 1723 22878 70160 71878 76546 77142 91...

result:

wrong answer 1st lines differ - expected: '524727018', found: '778158997'