QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#179314 | #5669. Traveling in Jade City | arseny_y# | RE | 937ms | 50000kb | C++23 | 6.4kb | 2023-09-14 20:29:44 | 2023-09-14 20:29:44 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define int long long
struct Fenwick {
vector<int> t;
Fenwick(int n) {
t.resize(n + 1, 0);
}
void upd(int i, int add) {
for (; i < t.size(); i += i & -i) t[i] += add;
}
int get(int len) {
int ans = 0;
for (; len > 0; len -= len & -len) ans += t[len];
return ans;
}
int get(int l, int r) {
return max(0LL, get(r) - get(l - 1));
}
};
int inv(int x) {
return x == 1 ? -1 : 1;
}
const int INF = INT_MAX * 100LL;
int32_t main() {
int n, k, m, q;
cin >> n >> k >> m >> q;
Fenwick fenw_round(n);
vector<int> pref_round(n + 1, 0), t_round(n + 1, 0);
for (int i = 1, x; i <= n; ++i) {
cin >> x;
pref_round[i] = pref_round[i - 1] + x;
fenw_round.upd(i, 1);
t_round[i] = 1;
}
++m;
vector<int> pref_dick(m + 1), t_dick(n + 1);
Fenwick fenw_dick(m);
for (int i = 1, x; i <= m; ++i) {
cin >> x;
pref_dick[i] = pref_dick[i - 1] + x;
fenw_dick.upd(i, 1);
t_dick[i] = 1;
}
auto get_dist_1_k = [&]() -> int {
int mn = INF;
if (fenw_dick.get(1, m) == m) mn = min(mn, pref_dick[m]);
if (fenw_round.get(1, k - 1) == k - 1) mn = min(mn, pref_round[k - 1]);
if (fenw_round.get(k, n) == n - k + 1) mn = min(mn, pref_round[n] - pref_round[k - 1]);
return mn;
};
auto check_in_round = [&](int v) -> bool {
return v <= n;
};
auto dist_in_dick = [&](int u, int v) -> int {
if (u > v) swap(u, v);
return (fenw_dick.get(u, v - 1) == v - u ? pref_dick[v - 1] - pref_dick[u - 1] : INF);
};
int sm_in_round = pref_round[n];
while (q--) {
char s;
cin >> s;
if (s == 'q') {
int u, v;
cin >> u >> v;
if (u > v) swap(u, v);
if (u == v) {
cout << "0\n";
continue;
}
int dst1k = get_dist_1_k();
int ans = INF;
int distu1 = INF, distuk = INF, distv1 = INF, distvk = INF;
if (check_in_round(u)) {
if (fenw_round.get(1, u - 1) == u - 1) distu1 = min(distu1, pref_round[u - 1]);
if (fenw_round.get(u, n) == n - u + 1) distu1 = min(distu1, sm_in_round - pref_round[u - 1]);
if (u < k) {
if (fenw_round.get(u, k - 1) == k - u) distuk = min(distuk, pref_round[k - 1] - pref_round[u - 1]);
if (fenw_round.get(1, u - 1) + fenw_round.get(k, n) == n - (k - u)) distuk = min(distuk, sm_in_round - (pref_round[k - 1] - pref_round[u - 1]));
} else {
swap(u, k);
if (fenw_round.get(u, k - 1) == k - u) distuk = min(distuk, pref_round[k - 1] - pref_round[u - 1]);
if (fenw_round.get(1, u - 1) + fenw_round.get(k, n) == n - (k - u)) distuk = min(distuk, sm_in_round - (pref_round[k - 1] - pref_round[u - 1]));
swap(u, k);
}
} else {
u -= n, ++u;
distu1 = min(distu1, dist_in_dick(1, u));
int sm = fenw_dick.get(1, m);
if (sm - fenw_dick.get(1, u - 1) == m + 1 - u) {
distuk = min(distuk, pref_dick[m] - pref_dick[u - 1]);
}
distuk = min(distuk, distu1 + dst1k);
distu1 = min(distu1, distuk + dst1k);
u += n, --u;
}
if (check_in_round(v)) {
if (fenw_round.get(1, v - 1) == v - 1) distv1 = min(distv1, pref_round[v - 1]);
if (fenw_round.get(v, n) == n - v + 1) distv1 = min(distv1, sm_in_round - pref_round[v - 1]);
if (v < k) {
if (fenw_round.get(v, k - 1) == k - v) distvk = min(distvk, pref_round[k - 1] - pref_round[v - 1]);
if (fenw_round.get(1, v - 1) + fenw_round.get(k, n) == n - (k - v)) distvk = min(distvk, sm_in_round - (pref_round[k - 1] - pref_round[v - 1]));
} else {
swap(v, k);
if (fenw_round.get(v, k - 1) == k - v) distvk = min(distvk, pref_round[k - 1] - pref_round[v - 1]);
if (fenw_round.get(1, v - 1) + fenw_round.get(k, n) == n - (k - v)) distvk = min(distvk, sm_in_round - (pref_round[k - 1] - pref_round[v - 1]));
swap(v, k);
}
} else {
v -= n, ++v;
distv1 = min(distv1, dist_in_dick(1, v));
int sm = fenw_dick.get(1, m);
if (sm - fenw_dick.get(1, v - 1) == m + 1 - v) {
distvk = min(distvk, pref_dick[m] - pref_dick[v - 1]);
}
distvk = min(distvk, distv1 + dst1k);
distv1 = min(distv1, distvk + dst1k);
v += n, --v;
}
for (int qq = 0; qq < 4; ++qq) {
distu1 = min(distu1, distuk + dst1k);
distv1 = min(distv1, distvk + dst1k);
distuk = min(distuk, distu1 + dst1k);
distvk = min(distvk, distv1 + dst1k);
}
ans = min(ans, min(distu1 + distv1, distvk+ distuk));
ans = min(ans, min(distuk + distv1, distvk+ distu1) + dst1k);
if (check_in_round(v)) {
int len = v - u;
if (fenw_round.get(u, v - 1) == len) ans = min(ans, pref_round[v - 1] - pref_round[u - 1]);
if (fenw_round.get(1, u - 1) + fenw_round.get(v, n) == n - len) ans = min(ans, sm_in_round - (pref_round[v - 1] - pref_round[u - 1]));
} else if (min(u, v) > n) {
ans = min(ans, dist_in_dick(u - n, v - n));
}
cout << (ans == INF ? "impossible" : to_string(ans)) << "\n";
} else if (s == 'c') {
int x;
cin >> x;
if (t_round[x] == 1) {
fenw_round.upd(x, -1);
} else {
fenw_round.upd(x, 1);
}
t_round[x] = inv(t_round[x]);
} else {
int x;
cin >> x;
++x;
if (t_dick[x] == 1) {
fenw_dick.upd(x, -1);
} else {
fenw_dick.upd(x, 1);
}
t_dick[x] = inv(t_dick[x]);
}
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3512kb
input:
4 3 1 9 2 3 8 4 1 1 q 3 4 x 0 q 3 4 c 3 q 3 4 c 1 q 3 4 x 0 q 3 4
output:
6 8 9 impossible 6
result:
ok 5 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 3764kb
input:
4 3 1 9 2 3 8 4 1 1 q 3 4 x 0 q 3 4 c 3 q 3 4 c 1 q 3 4 x 0 q 3 4
output:
6 8 9 impossible 6
result:
ok 5 lines
Test #3:
score: 0
Accepted
time: 937ms
memory: 50000kb
input:
1000000 999999 1000000 1000000 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 2...
output:
177406400 122264600 70328400 impossible impossible impossible impossible impossible impossible impossible impossible impossible 29295200 impossible 22163200 impossible impossible impossible 11422200 impossible 62579800 impossible 35339400 impossible 20157200 impossible impossible impossible impossib...
result:
ok 500003 lines
Test #4:
score: -100
Runtime Error
input:
100000 25123 500000 1000000 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 200 ...
output:
4243800 157600 0 113507599 0 81036599 0 90964799 373800 82019799 90088199 0 impossible 84008999 0 81054399 0 0 impossible 0 0 0 0 0 80077999 impossible 0 86282599 0 impossible 84115599 0 impossible 99250799 0 0 impossible impossible impossible impossible 0 impossible impossible 99993399 impossible 8...