QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#220217 | #5669. Traveling in Jade City | owen0806# | WA | 273ms | 36240kb | C++14 | 4.7kb | 2023-10-19 23:35:55 | 2023-10-19 23:35:55 |
Judging History
answer
#include<bits/stdc++.h>
#define int long long
#define endl '\n'
using namespace std;
const int N = 1e6 + 5;
const int INF = 1e18;
bool visc[N], visx[N];
int c[N], x[N];
struct B{
int n;
vector<int> bit;
void init(int _n){
n = _n;
bit = vector<int>(n+1, 0);
}
int lowbit(int x){ return (x&-x); }
void update(int x, int v){
for(; x<=n; x+=lowbit(x)){
bit[x] += v;
}
}
int query(int x){
int ret = 0;
for(; x; x-=lowbit(x)){
ret += bit[x];
}
return ret;
}
}BIT, BIT2;
void solve(){
int n, k, m, q;
cin >> n >> k >> m >> q;
int s = 0;
for(int i=1; i<=n; i++){
cin >> c[i];
c[i] += s;
s = c[i];
visc[i] = 1;
}
s = 0;
for(int i=1; i<=m+1; i++){
cin >> x[i];
x[i] += s;
s = x[i];
visx[i] = 1;
}
BIT.init(n);
BIT2.init(m+1);
for(int i=0; i<q; i++){
char ch; int a, b;
cin >> ch;
if(ch == 'q'){
cin >> a >> b;
if(a > b) swap(a, b);
int k1=0, k2=0, k3=0, k4=0, k5=-1, k6=-1;
int l1, l2, l3, l4;
int L, M, R;
int ll, mm, rr;
L = BIT.query(n) - BIT.query(k-1);
ll = c[n] - c[k-1];
M = BIT2.query(m+1);
mm = x[m+1];
R = BIT.query(k-1);
rr = c[k-1];
if(1 <= a && a <= k){
k1 = BIT.query(a-1);
l1 = c[a-1];
k2 = BIT.query(k-1) - BIT.query(a-1);
l2 = c[k-1] - c[a-1];
}
else if(k <= a && a <= n){
k1 = BIT.query(n) - BIT.query(a-1);
l1 = c[n] - c[a-1];
k2 = BIT.query(a-1) - BIT.query(k-1);
l2 = c[a-1] - c[k-1];
}
else{
k1 = BIT2.query(a-n);
l1 = x[a-n];
k2 = BIT2.query(m+1) - BIT2.query(a-n);
l2 = x[m+1] - x[a-n];
}
if(1 <= b && b <= k){
k3 = BIT.query(b-1);
l3 = c[b-1];
k4 = BIT.query(k-1) - BIT.query(b-1);
l4 = c[k-1] - c[b-1];
}
else if(k <= b && b <= n){
k3 = BIT.query(n) - BIT.query(b-1);
l3 = c[n] - c[b-1];
k4 = BIT.query(b-1) - BIT.query(k-1);
l4 = c[b-1] - c[k-1];
}
else{
k3 = BIT2.query(b-n);
l3 = x[b-n];
k4 = BIT2.query(m+1) - BIT2.query(b-n);
l4 = x[m+1] - x[b-n];
}
int ans = INF;
if(k1 + k3 == 0)
ans = min(ans, l1+l3);
if(k2 + k4 == 0)
ans = min(ans, l2+l4);
if(k1 + k4 == 0){
int sum = l1+l4;
if(L == 0)
ans = min(ans, sum+ll);
if(M == 0)
ans = min(ans, sum+mm);
if(R == 0)
ans = min(ans, sum+rr);
}
if(k2 + k3 == 0){
int sum = l2 + l3;
if(L == 0)
ans = min(ans, sum+ll);
if(M == 0)
ans = min(ans, sum+mm);
if(R == 0)
ans = min(ans, sum+rr);
}
if(1 <= a && a <= n && 1 <= b && b <= n){
if(BIT.query(b-1) - BIT.query(a-1) == 0)
ans = min(ans, c[b-1]-c[a-1]);
if(BIT.query(n) - BIT.query(b-1) + BIT.query(a-1) == 0)
ans = min(ans, c[n]-c[b-1]+c[a-1]);
}
if(n+1 <= a && a <= n+m+1 && n+1 <= b && b <= n+m+1){
if(BIT2.query(b-n-1) - BIT2.query(a-n-1) == 0)
ans = min(ans, x[b-n-1]-x[a-n-1]);
}
// cout << l1 << " " << l2 << " " << l3 << " " << l4 << endl;
if(ans == INF) cout << "impossible\n";
else cout << ans << endl;
}
else if(ch == 'c'){
cin >> a;
if(visc[a])
BIT.update(a, 1);
else
BIT.update(a, -1);
visc[a] ^= 1;
}
else{
cin >> a;
a++;
if(visx[a])
BIT2.update(a, 1);
else
BIT2.update(a, -1);
visx[a] ^= 1;
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(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: 7696kb
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: 7692kb
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: 273ms
memory: 36240kb
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
Wrong Answer
time: 171ms
memory: 17896kb
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 37968200 35427000 52078200 5074200 70821200 13901400 13614600 8774800 68958800 49822200 31077400 impossible 45392400 48946000 76885400 37532600 34416200 impossible 20744200 71652000 21288000 7501600 70315400 14721800 impossible 12981800 81039600 9506800 impossible 33487200 53520200 impossibl...
result:
wrong answer 414735th lines differ - expected: '200', found: 'impossible'