QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#537700 | #9222. Price Combo | superguymj | WA | 345ms | 52748kb | C++20 | 7.6kb | 2024-08-30 17:32:23 | 2024-08-30 17:32:23 |
Judging History
answer
#include <bits/stdc++.h>
#define rep(i,x,y) for(int i=x; i<=y; ++i)
#define repd(i,x,y) for(int i=x; i>=y; --i)
#define mid ((l + r) >> 1)
#define lch (rt << 1)
#define rch (rt << 1 | 1)
using namespace std;
using i64 = long long;
/*
PAY ATTENTION TO CONSTRUCT FUNCTION OF CLASS INFO!!!
*/
template<class Info>
struct SegmentTree {
int n;
std::vector<Info> info;
SegmentTree() : n(0) {}
SegmentTree(int n_, Info v_ = Info()) {
init(n_, v_);
}
template<class T>
SegmentTree(std::vector<T> init_) {
init(init_);
}
void init(int n_, Info v_ = Info()) {
init(std::vector(n_, v_));
}
template<class T>
void init(std::vector<T> init_) {
n = init_.size();
info.assign(4 << std::__lg(n), Info());
std::function<void(int, int, int)> build = [&](int p, int l, int r) {
if (r - l == 1) {
info[p] = init_[l];
return;
}
int m = (l + r) / 2;
build(2 * p, l, m);
build(2 * p + 1, m, r);
pull(p);
};
build(1, 0, n);
}
void pull(int p) {
info[p] = info[2 * p] + info[2 * p + 1];
}
void modify(int p, int l, int r, int x, const Info &v) {
if (r - l == 1) {
info[p] = v;
return;
}
int m = (l + r) / 2;
if (x < m) {
modify(2 * p, l, m, x, v);
} else {
modify(2 * p + 1, m, r, x, v);
}
pull(p);
}
void modify(int p, const Info &v) {
modify(1, 0, n, p, v);
}
Info rangeQuery(int p, int l, int r, int x, int y) {
if (l >= y || r <= x) {
return Info();
}
if (l >= x && r <= y) {
return info[p];
}
int m = (l + r) / 2;
return rangeQuery(2 * p, l, m, x, y) + rangeQuery(2 * p + 1, m, r, x, y);
}
Info rangeQuery(int l, int r) {
return rangeQuery(1, 0, n, l, r);
}
template<class F>
int findFirst(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
int res = findFirst(2 * p, l, m, x, y, pred);
if (res == -1) {
res = findFirst(2 * p + 1, m, r, x, y, pred);
}
return res;
}
template<class F>
int findFirst(int l, int r, F &&pred) {
return findFirst(1, 0, n, l, r, pred);
}
template<class F>
int findLast(int p, int l, int r, int x, int y, F &&pred) {
if (l >= y || r <= x) {
return -1;
}
if (l >= x && r <= y && !pred(info[p])) {
return -1;
}
if (r - l == 1) {
return l;
}
int m = (l + r) / 2;
int res = findLast(2 * p + 1, m, r, x, y, pred);
if (res == -1) {
res = findLast(2 * p, l, m, x, y, pred);
}
return res;
}
template<class F>
int findLast(int l, int r, F &&pred) {
return findLast(1, 0, n, l, r, pred);
}
};
constexpr int inf = 1E9;
struct Info
{
int x = -inf;
pair<int, int> id {-1, -1};
} ;
Info operator+(Info a, Info b)
{
if (a.x > b.x) return a;
return b;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n), b(n);
rep(i,0,n-1) cin >> a[i];
rep(i,0,n-1) cin >> b[i];
if (n&1)
{
n++;
a.push_back(0);
b.push_back(0);
}
vector<int> id(n);
iota(id.begin(), id.end(), 0);
sort(id.begin(), id.end(),
[&](int i, int j) {
return a[i] == a[j] ? i < j : a[i] < a[j];
});
vector<int> pa(n);
rep(i,0,n-1) pa[id[i]] = i;
sort(id.begin(), id.end(),
[&](int i, int j) {
return b[i] == b[j] ? i < j : b[i] < b[j];
});
vector<int> pb(n);
rep(i,0,n-1) pb[id[i]] = i;
set<pair<int, int>> sA, sB;
rep(i,0,n-1) sA.insert({a[i], i}), sB.insert({b[i], i});
SegmentTree<Info> segA(n), segB(n);
SegmentTree<Info> segA2(n), segB2(n);
i64 ans = 0;
for (int i = 0; i < n; i += 2)
{
int A2 = next(sA.begin()) -> first;
int B2 = next(sB.begin()) -> first;
int Ab = inf;
int aB = inf;
Info qA, qB, qA2, qB2;
if (sA.begin() -> second != sB.begin() -> second)
{
int A = sA.begin() -> second;
int B = sB.begin() -> second;
qA = segA.rangeQuery(0, pb[B]);
qB = segB.rangeQuery(0, pa[A]);
qA2 = segA2.rangeQuery(pb[B], n);
qB2 = segB2.rangeQuery(pa[A], n);
Ab = min(sA.begin() -> first + sB.begin() -> first - qA.x, sA.begin() -> first - qA2.x);
aB = min(sA.begin() -> first + sB.begin() -> first - qB.x, sB.begin() -> first - qB2.x);
}
int v = min({A2, B2, Ab, aB});
ans += v;
if (v == A2)
{
int x = sA.begin() -> second; sA.erase(sA.begin());
int y = sA.begin() -> second; sA.erase(sA.begin());
sB.erase({b[x], x}), sB.erase({b[y], y});
segA.modify(min(pb[x], pb[y]), {v, make_pair(x, y)});
segA2.modify(min(pb[x], pb[y]), {v - min(b[x], b[y]), make_pair(x, y)});
}
else if (v == B2)
{
int x = sB.begin() -> second; sB.erase(sB.begin());
int y = sB.begin() -> second; sB.erase(sB.begin());
sA.erase({a[x], x}), sA.erase({a[y], y});
segB.modify(min(pa[x], pa[y]), {v, make_pair(x, y)});
segB2.modify(min(pa[x], pa[y]), {v - min(a[x], a[y]), make_pair(x, y)});
}
else if (v == Ab)
{
int x = sA.begin() -> second; sA.erase(sA.begin()), sB.erase({b[x], x});
int y = sB.begin() -> second; sB.erase(sB.begin()), sA.erase({a[y], y});
auto [nx, ny] = b[y] - qA.x < -qA2.x ? qA.id : qA2.id;
if (pb[nx] > pb[ny]) swap(nx, ny);
segA.modify(min(pb[nx], pb[ny]), Info());
segA2.modify(min(pb[nx], pb[ny]), Info());
segA.modify(min(pb[x], pb[ny]), {a[x], make_pair(x, ny)});
segB.modify(min(pa[y], pa[nx]), {max(b[y], b[nx]), make_pair(y, nx)});
segA2.modify(min(pb[x], pb[ny]), {a[x] - min(b[x], b[ny]), make_pair(x, ny)});
segB2.modify(min(pa[y], pa[nx]), {max(b[y], b[nx]) - min(a[y], a[nx]), make_pair(y, nx)});
}
else
{
int x = sB.begin() -> second; sB.erase(sB.begin()), sA.erase({a[x], x});
int y = sA.begin() -> second; sA.erase(sA.begin()), sB.erase({b[y], y});
auto [nx, ny] = a[y] - qB.x < -qB2.x ? qB.id : qB2.id;
if (pa[nx] > pa[ny]) swap(nx, ny);
segB.modify(min(pa[nx], pa[ny]), Info());
segB2.modify(min(pa[nx], pa[ny]), Info());
segB.modify(min(pa[x], pa[ny]), {b[x], make_pair(x, ny)});
segA.modify(min(pb[y], pb[nx]), {max(a[y], a[nx]), make_pair(y, nx)});
segB2.modify(min(pa[x], pa[ny]), {b[x] - min(a[x], a[ny]), make_pair(x, ny)});
segA2.modify(min(pb[y], pb[nx]), {max(a[y], a[nx]) - min(b[y], b[nx]), make_pair(y, nx)});
}
}
cout << ans << '\n';
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3800kb
input:
7 10 12 19 99 10 8 49 9 14 15 199 11 7 19
output:
131
result:
ok single line: '131'
Test #2:
score: 0
Accepted
time: 0ms
memory: 3544kb
input:
7 10 12 19 99 10 8 49 9 14 15 199 11 7 19
output:
131
result:
ok single line: '131'
Test #3:
score: -100
Wrong Answer
time: 345ms
memory: 52748kb
input:
199913 1212731 2525164 3210261 2457211 1013738 1931420 923123 867112 762069 2108660 108920 2491869 867107 387025 2278045 574027 1661570 820133 1274150 2001346 779766 3305537 3000211 2418643 2108660 2001343 1074820 2754411 826712 3117447 1661569 338161 1849064 3007920 3057426 468078 3252798 1274146 4...
output:
154816494972
result:
wrong answer 1st lines differ - expected: '154816494865', found: '154816494972'