QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#183918 | #5669. Traveling in Jade City | ZiadElGafy# | WA | 831ms | 73592kb | C++20 | 6.2kb | 2023-09-20 00:38:11 | 2023-09-20 00:38:11 |
Judging History
answer
#pragma GCC optimize("Ofast")
#pragma GCC optimize("O3")
#include <bits/stdc++.h>
#define el '\n'
#define F first
#define S second
typedef long long ll;
typedef long double ld;
typedef __int128 bigInt;
using namespace std;
const int N = 1e6 + 5, INF = 1e9 + 5, mod = 1e9 + 7, LOG = 21, SQ = 500;
int n, k, m, q, c[N], x[N];
struct SegmentTree {
vector<ll> tree;
ll neutral;
SegmentTree(){}
SegmentTree(int sz) {
tree.assign(4 * sz, 0);
neutral = 0;
}
ll merge(ll &u, ll &v) {
return u + v;
}
void build(int node, int start, int end, int a[]) {
if (start == end)
return tree[node] = a[start], void();
int m = (start + end) >> 1;
build(2 * node, start, m, a);
build(2 * node + 1, m + 1, end, a);
tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
}
void set(int node, int start, int end, int idx, ll val) {
if (start == end)
return tree[node] = val, void();
int m = (start + end) >> 1;
if (idx <= m)
set(2 * node, start, m, idx, val);
else
set(2 * node + 1, m + 1, end, idx, val);
tree[node] = merge(tree[2 * node], tree[2 * node + 1]);
}
ll query(int node, int start, int end, int left, int right) {
if (end < left or start > right or left > right)
return neutral;
if (left <= start and end <= right)
return tree[node];
int m = (start + end) >> 1;
ll l = query(2 * node, start, m, left, right);
ll r = query(2 * node + 1, m + 1, end, left, right);
return merge(l, r);
}
}stc, stx;
ll pathcw(int u, int v) {
if (u == v) {
return 0;
}
ll ret = 0;
if (v > u) {
// direct u -> v
ret += stc.query(1, 1, n, u, v - 1);
}
else {
// u to one, one to v
ret += stc.query(1, 1, n, u, n);
ret += stc.query(1, 1, n, 1, v - 1);
}
return ret;
}
ll pathccw(int u, int v) {
if (u == v) {
return 0;
}
ll ret = 0;
if (v < u) { // haroh lel soghayar
// direct u -> v
ret += stc.query(1, 1, n, v, u - 1);
}
else {
// one to u, v to one
ret += stc.query(1, 1, n, 1, u - 1);
ret += stc.query(1, 1, n, v, n);
}
return ret;
}
ll pathx(int u, int v) {
if (u == v) {
return 0;
}
if (u == 1) {
u = 0;
}
if (v == 1) {
v = 0;
}
if (u == k) {
u = m + 1;
}
if (v == k) {
v = m + 1;
}
if (u > v) {
swap(u, v);
}
return stx.query(1, 0, m, u, v - 1);
}
void doWork() {
cin >> n >> k >> m >> q;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 0; i <= m; i++) {
cin >> x[i];
}
stc = SegmentTree(n);
stc.build(1, 1, n, c);
stx = SegmentTree(m + 1);
stx.build(1, 0, m, x);
while (q--) {
char type;
cin >> type;
if (type == 'q') {
int u, v;
cin >> u >> v;
bool uc = 0, ux = 0, vc = 0, vx = 0;
(u <= n ? uc = 1 : ux = 1);
(v <= n ? vc = 1 : vx = 1);
ll ans;
if (uc and vc) { // cc
ans = min(pathcw(u, v), pathccw(u, v));
// cout << "init ans " << ans << el;
ll uone = min(pathcw(u, 1), pathccw(u, 1));
// cout << "uone " << uone << " " << pathcw(u, 1) << ' ' << pathccw(u, 1) << el;
ll onek = pathx(1, k);
// cout << "onek " << onek << el;
ll kv = min(pathcw(k, v), pathccw(k, v));
// cout << "kv " << kv << el;
ll uk = min(pathcw(u, k), pathccw(u, k));
// cout << "uk " << uk << el;
ll kone = pathx(k, 1);
// cout << "kone " << kone << el;
ll onev = min(pathcw(1, v), pathccw(1, v));
// cout << "onev " << onev << endl;
ans = min({
ans,
uone + onek + kv,
uk + kone + onev
});
}
else if (ux and vx) { // xx
ans = pathx(u, v);
ll uone = pathx(u, 1);
ll onek = min(pathcw(1, k), pathccw(1, k));
ll kv = pathx(k, v);
ll uk = pathx(u, k);
ll kone = min(pathcw(1, k), pathccw(1, k));
ll onev = pathx(1, v);
ans = min({
ans,
uone + onek + kv,
uk + kone + onev
});
}
else { // cx
if (ux) {
swap(u, v);
}
ll uone = min(pathcw(u, 1), pathccw(u, 1));
ll onev = pathx(1, v);
ll uk = min(pathcw(u, k), pathccw(u, k));
ll kv = pathx(k, v);
ans = min(uone + onev, uk + kv);
}
if (ans < 1e9) {
cout << ans << el;
}
else {
cout << "impossible" << el;
}
}
else if (type == 'c') {
int i;
cin >> i;
// update edge i in c with INF
int val = stc.query(1, 1, n, i, i);
if (val < INF) {
stc.set(1, 1, n, i, INF);
}
else {
stc.set(1, 1, n, i, c[i]);
}
}
else if (type == 'x') {
int i;
cin >> i;
// update edge i in x with INF
int val = stx.query(1, 0, m, i, i);
if (val < INF) {
stx.set(1, 0, m, i, INF);
}
else {
stx.set(1, 0, m, i, x[i]);
}
}
}
}
int main() {
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int tests = 1;
// cin >> tests;
for (int i = 1; i <= tests; i++) {
doWork();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5672kb
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: 1ms
memory: 5780kb
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: -100
Wrong Answer
time: 831ms
memory: 73592kb
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 33565400 26009600 impossible impossible impossible impossible impossible impossible impossible impossible impossible 10227400 impossible 10485200 impossible impossible impossible 0 impossible 0 impossible 0 impossible 0 impossible impossible impossible impossible impossible 0 impossible im...
result:
wrong answer 2nd lines differ - expected: '122264600', found: '33565400'