QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#732438 | #5669. Traveling in Jade City | Ashbourne | WA | 371ms | 86524kb | C++23 | 3.2kb | 2024-11-10 14:32:01 | 2024-11-10 14:32:05 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define rep(i, j, k) for(int i = j; i <= k; ++ i)
using namespace std;
const int N = 2e6 + 10;
int sum[N], sum2[N];
struct BST{
int tr[N << 1], tot;
void init(int siz){
// memset(tr, 0, sizeof tr);
tot = siz;
}
void add(int x, int num){
while(x <= tot){
tr[x] += num;
x += x & (-x);
}
}
int query(int x){
int ans = 0;
while(x){
ans += tr[x];
x -= x & (-x);
}
return ans;
}
}Tr1, Tr2;
int n, m, k, q;
int dist(int u, int v){
int ans = 1e14;
if(u == v) return 0;
if(u > v) swap(u, v);
int x = u, y = v, z = u + n;
// cerr << u << " " << v << endl;
if(!(Tr1.query(y - 1) - Tr1.query(x - 1))) ans = min(ans, sum[v - 1] - sum[u - 1]);
if(!(Tr1.query(z - 1) - Tr1.query(y - 1))) ans = min(ans, sum[z - 1] - sum[y - 1]);
// cout << ans << endl;
return ans;
}
void solve(){
cin >> n >> k >> m >> q;
vector<int> a(n + 1), b(m + 100);
vector<int> c(2 * n + 10);
vector<int> d(m + 10);
rep(i, 1, n) cin >> a[i];
rep(i, 0, m) cin >> b[i];
rep(i, 1, n) sum[i] = sum[i - 1] + a[i], c[i] = 1;
rep(i, n + 1, 2 * n) sum[i] = sum[i - 1] + a[i - n], c[i] = 1;
// cout << sum[2 * n] << endl;
rep(i, 1, m + 1) sum2[i] = sum2[i - 1] + b[i - 1], d[i - 1] = 1;
Tr1.init(2 * n); Tr2.init(m + 1);
while(q--){
char ch;
cin >> ch;
if(ch == 'c'){
int cur; cin >> cur;
// cerr << "c " << c[cur] << endl;
Tr1.add(cur, c[cur]);
Tr1.add(cur + n, c[cur + n]);
c[cur] *= -1;
c[cur + n] *= -1;
continue;
}
if(ch == 'x'){
int cur; cin >> cur;
Tr2.add(cur + 1, d[cur]);
// cerr << "x " << cur << " "<< d[cur] << endl;
d[cur] *= -1;
}
if(ch == 'q'){
int u, v;
cin >> u >> v;
if(u > v) swap(u, v);
// int dit = dist(u, v);
if(v <= n){
int ans = dist(u, v);
// cerr << q << " "<<ans << endl;
// cerr << Tr1.query(7) << endl;
// cerr << Tr1.query(3) << endl;
if(!Tr2.query(m + 1)){
int tt = sum2[m + 1];
int d1 = dist(u, 1), d2 = dist(v, k);
// cerr << d1 << " " << d2 << endl;
ans = min(ans, d1 + d2 + tt);
d1 = dist(u, k), d2 = dist(v, 1);
ans = min(ans, d1 + d2 + tt);
}
if(ans == 1e14) cout << "impossible" << endl;
else cout << ans << endl;
}else if(u > n){
int ans = 1e14;
if(!(Tr2.query(v) - Tr2.query(u))) ans = sum2[v] - sum2[u];
if(!Tr2.query(u) && !(Tr2.query(m + 1) - Tr2.query(v))){
ans = min(ans, dist(1, k) + sum2[u] + sum2[m + 1] - sum2[v]);
}
if(ans == 1e14) cout << "impossible" << endl;
else cout << ans << endl;
}else{
int ans = 1e14;
if(!Tr2.query(v)){
ans = min(ans, sum2[v] + dist(1, u));
}
if(!(Tr2.query(m + 1) - Tr2.query(v))){
ans = min(ans, sum2[m + 1] - sum2[v] + dist(k, u));
}
if(ans == 1e14) cout << "impossible" << endl;
else cout << ans << endl;
}
}
}
}
signed main(){
ios::sync_with_stdio(0);
int T = 1;
// cin >> T;
while(T--) solve();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 5828kb
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: 5832kb
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: 371ms
memory: 86524kb
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 33565800 26009200 impossible impossible impossible impossible impossible impossible impossible impossible impossible 10227000 impossible 10484800 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: '33565800'