QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#689578 | #9252. Penguins in Refrigerator | real_sigma_team | WA | 0ms | 3576kb | C++23 | 4.0kb | 2024-10-30 17:49:28 | 2024-10-30 17:49:28 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
constexpr int mod = 1'000'000'007;
mt19937 mt(456);
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[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);
for (int i = 0; i < n; i++) {
cout << ans[i] << ' ';
}
cout << '\n';
}
详细
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3576kb
input:
5 10 1 2 3 4 5 6 5 3 9 2
output:
5 4 2 1 3
result:
wrong answer 1st lines differ - expected: '3', found: '5 4 2 1 3 '