QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#94880 | #5669. Traveling in Jade City | SEM_PRESSAO_pedroteosousa# | WA | 457ms | 34308kb | C++23 | 3.0kb | 2023-04-08 03:35:54 | 2023-04-08 03:35:58 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
#define all(v) v.begin(),v.end()
#define pb push_back
#define endl '\n'
void dbg_out() {cerr << endl; }
template< typename H, typename... T>
void dbg_out(H h, T... t) { cerr << ' ' << h; dbg_out(t...); }
#define dbg(...) //{ cerr << #__VA_ARGS__ << ':'; dbg_out(__VA_ARGS__); }
struct Bit {
int total_sum;
vector<int> bit;
Bit(int n): total_sum(0), bit(n) {}
int query(int id) {
int res = 0;
for(id++;id>0;id-=id&-id) res += bit[id-1];
return res;
}
void update(int id, int val) {
total_sum += val;
for(id++;id<=int(bit.size());id+=id&-id) bit[id-1] += val;
}
int query_range(int l, int r) {
return query(r) - ((l == 0) ? 0 : query(l-1));
}
};
void solve() {
int n, k, m, q; cin >> n >> k >> m >> q;
--k;
Bit cyc(n), path(m+1);
vector<int> c(n), p(m+1);
vector<bool> okc(n, true), okp(m+1, true);
for(int i=0;i<n;i++) {
cin >> c[i];
cyc.update(i, c[i]);
}
for(int i=0;i<=m;i++) {
cin >> p[i];
path.update(i, p[i]);
}
dbg(cyc.total_sum, path.total_sum);
dbg(cyc.query(0));
dbg(cyc.query(1));
dbg(cyc.query_range(0, 1));
const int INF = 1e10;
auto cyc_dist = [&](int u, int v) -> int {
assert(u < n && v < n);
if(u > v) swap(u, v);
if(u == v) return 0;
int ans = cyc.query_range(u, v-1);
return min<int>(ans, cyc.total_sum - ans);
};
// transform in path coordinates
auto transform = [&](int x) -> int {
if(x == 0) return 0;
else if(x == k) return m + 1;
else {
assert(x >= n);
return x - n + 1;
}
};
auto path_dist = [&](int u, int v) {
u = transform(u);
v = transform(v);
if(u > v) swap(u, v);
return path.query_range(u, v);
};
auto out_path_dist = [&](int u, int v) {
return path.total_sum - path.query_range(u, v);
};
function<int(int,int)> query = [&](int u, int v) {
assert(u <= v);
int ans = INF;
if(u < n && v < n) {
// both outside
ans = min(ans, cyc_dist(u, v));
ans = min(ans, cyc_dist(u, 0) + path.total_sum + cyc_dist(k, v));
ans = min(ans, cyc_dist(u, k) + path.total_sum + cyc_dist(0, v));
} else if(u < n && v >= n) {
// v is inside
ans = min(ans, cyc_dist(u, 0) + path_dist(v, 0));
ans = min(ans, cyc_dist(u, k) + path_dist(v, k));
} else {
assert(u >= n && v >= n);
ans = min(ans, path_dist(u, v));
ans = min(ans, cyc_dist(0, k) + out_path_dist(u, v));
}
return ans;
};
while(q--) {
char cc; cin >> cc;
if(cc == 'q') {
int u, v; cin >> u >> v;
--u; --v;
if(u > v) swap(u,v);
int ans = query(u, v);
if(ans >= INF) {
cout << "impossible" << endl;
} else {
cout << ans << endl;
}
} else if(cc == 'c') { // cycle
int i; cin >> i;
--i;
if(okc[i]) {
cyc.update(i, INF - c[i]);
} else {
cyc.update(i, c[i] - INF);
}
okc[i] = !okc[i];
} else if(cc == 'x') {
int i; cin >> i;
if(okp[i]) {
path.update(i, INF - p[i]);
} else {
path.update(i, p[i] - INF);
}
okp[i] = !okp[i];
} else {
assert(false);
}
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3384kb
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: 3312kb
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: 457ms
memory: 34308kb
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 122264400 70328200 impossible impossible impossible impossible impossible impossible impossible impossible impossible 29295400 impossible 22163400 impossible impossible impossible 11422400 impossible 62580000 impossible 164661400 impossible -9802339000 impossible impossible impossible impo...
result:
wrong answer 2nd lines differ - expected: '122264600', found: '122264400'