QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#288499 | #5669. Traveling in Jade City | VinhLuu | RE | 3ms | 27064kb | C++23 | 7.4kb | 2023-12-22 19:22:50 | 2023-12-22 19:22:51 |
Judging History
answer
#include <bits/stdc++.h>
//#define int long long
//#define pb push_back
using namespace std;
typedef pair<int,int> pii;
typedef tuple<int,int,int> tp;
const int N = 1e6 + 5;
const int oo = 3e8;
char Q[N];
int U[N], V[N];
int n, mx, k, m, q, gc[N], gx[N], f[N], c[N], x[N], sum, s[N], R[N], g[N], TX, t[N], NG;
vector<tp> p[N];
int st[5][N << 2], lz[5][N << 2];
void dolz(int j,int i){
if(lz[j][i] == 0) return;
int x = lz[j][i];
lz[j][i] = 0;
lz[j][i << 1] += x;
lz[j][i << 1|1] += x;
st[j][i << 1] += x;
st[j][i << 1|1] += x;
}
void update(int j,int i,int l,int r,int u,int v,int x){
if(l > r || l > v || r < u) return;
if(u <= l && r <= v) {
st[j][i] += x;
lz[j][i] += x;
return;
}
dolz(j, i);
int mid = (l + r) >> 1;
update(j, i << 1, l, mid, u, v, x);
update(j, i << 1|1, mid + 1, r, u, v, x);
}
int get(int j,int i,int l,int r,int u){
if(l > r || l > u || r < u || u == 0 || u == n) return 0;
if(l == r) return st[j][i];
dolz(j, i);
int mid = (l + r) >> 1;
return get(j, i << 1, l, mid, u) + get(j, i << 1|1, mid + 1, r, u);
}
int C(int u,int v){
if(u == v) return 0;
if(u > v) swap(u, v);
int L1 = (get(0, 1, 1, mx, v - 1) - get(0, 1, 1, mx, u - 1) != 0 ? oo : s[v] - s[u]);
int L2 = (g[n] || NG - (get(0, 1, 1, mx, v - 1) - get(0, 1, 1, mx, u - 1)) != 0 ? oo : sum - (s[v] - s[u]));
// cout << u << " " << v << " " << L1 << " " << L2 << " " << get(0, 1, 1, mx, u - 1) << " r\n";
return min(L1, L2);
}
int X(int u,int v){
if(u == v) return 0;
if(R[u] > R[v]) swap(u, v);
if(u == 1 && v == k){
if(gx[0] || gx[m]) return oo;
if(get(1, 1, 1, mx, n + m - 1) - get(1, 1, 1, mx, n) != 0) return oo;
return TX;
}
if(u == 1){
if(get(1, 1, 1, mx, v - 1) - get(1, 1, 1, mx, n) != 0 || gx[0] == true) return oo;
return t[v] - t[u];
}
if(v == k){
if(get(1, 1, 1, mx, n + m - 1) - get(1, 1, 1, mx, u - 1) != 0 || gx[m] == true) return oo;
return t[v] - t[u];
}
if(get(1, 1, 1, mx, v - 1) - get(1, 1, 1, mx, u - 1) != 0) return oo;
return t[v] - t[u];
}
void sub3(){
mx = n + m;
for(int i = 2; i <= n; i ++) s[i] = s[i - 1] + c[i - 1];
R[1] = 0;
int cnt = 0;
for(int i = n + 1; i <= n + m; i ++){
g[i] = 1;
R[i] = ++cnt;
t[i] = t[i - 1] + x[i - n - 1];
}
R[k] = ++cnt;
t[k] = t[n + m] + x[m];
for(int j = 1; j <= q; j ++){
char type = Q[j];
if(type == 'q'){
int u = U[j], v = V[j];
int kq = oo;
if(g[u] == g[v]){
if(g[u] == 1){
kq = min({X(u, v), X(u, 1) + X(v, k) + C(1, k), C(1, k) + X(u, k) + X(v, 1)});
}else{
kq = min({C(u, v), C(1, u) + C(k, v) + X(1, k), C(k, u) + C(1, v) + X(1, k)});
}
}else{
if(g[u] == 1){
kq = min({X(1, u) + C(1, v), X(k, u) + C(k, v)}) ;
}else{
kq = min({X(k, v) + C(k, u), X(1, v) + C(1, u)});
}
}
if(kq >= oo) cout << "impossible\n";
else cout << kq << "\n";
}else if(type == 'c'){
int i = U[j];
if(gc[i] == 0){
NG++;
update(0, 1, 1, mx, i, n, 1);
gc[i] = 1;
}else{
NG--;
gc[i] = 0;
update(0, 1, 1, mx, i, n, -1);
}
}else{
int i = U[j];
if(gx[i] == 0){
if(i != 0 && i != m) update(1, 1, 1, mx, n + i, n + m, 1);
gx[i] = 1;
}else{
gx[i] = 0;
if(i != 0 && i != m) update(1, 1, 1, mx, n + i, n + m, -1);
}
}
}
}
int dij(int u,int v){
for(int i = 1; i <= n + m; i ++) f[i] = oo;
priority_queue<pii,vector<pii>,greater<pii>> pq;
pq.push({f[u] = 0, u});
while(!pq.empty()){
int w, u; tie(w, u) = pq.top();
pq.pop();
if(w > f[u]) continue;
for(auto tt : p[u]){
int j, v, e; tie(j, v, e) = tt;
if(e == 0){
if(gc[v] == 1) continue;
if(f[j] > w + c[v]) pq.push({f[j] = w + c[v], j});
}else{
if(gx[v] == 1) continue;
if(f[j] > w + x[v]) pq.push({f[j] = w + x[v], j});
}
}
}
if(f[v] == oo) return -1;
else return f[v];
}
void sub1(){
while(q--){
char type; cin >> type;
if(type == 'q'){
int u, v; cin >> u >> v;
int what = dij(u, v);
if(what == -1) cout << "impossible\n";
else cout << what << "\n";
}else if(type == 'c'){
int i; cin >> i;
gc[i] = 1 - gc[i];
}else{
int i; cin >> i;
gx[i] = 1 - gx[i];
}
}
}
int calc(int u,int v){
if(u == v) return 0;
if(u > v) swap(u, v);
return min(s[v] - s[u], sum - (s[v] - s[u]));
}
int calx(int u,int v){
if(u == v) return 0;
if(R[u] > R[v]) swap(u, v);
return t[v] - t[u];
}
void sub2(){
for(int i = 2; i <= n; i ++) s[i] = s[i - 1] + c[i - 1];
R[1] = 0;
int cnt = 0;
for(int i = n + 1; i <= n + m; i ++){
g[i] = 1;
R[i] = ++cnt;
t[i] = t[i - 1] + x[i - n - 1];
}
R[k] = ++cnt;
t[k] = t[n + m] + x[m];
for(int i = 1; i <= q; i ++){
char type = Q[i];
int u = U[i], v = V[i];
if(g[u] == g[v]){
if(g[u] == 1){
cout << min({calx(u, v), calx(u, 1) + calx(v, k) + calc(1, k), calc(1, k) + calx(u, k) + calx(v, 1)}) << "\n";
}else{
cout << min({calc(u, v), calc(1, u) + calc(k, v) + TX, calc(k, u) + calc(1, v) + TX}) << "\n";
}
}else{
if(g[u] == 1){
cout << min({calx(1, u) + calc(1, v), calx(k, u) + calc(k, v)}) << "\n";
}else{
cout << min({calx(k, v) + calc(k, u), calx(1, v) + calc(1, u)}) << "\n";
}
}
}
}
signed main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen("train.inp","r")){
freopen("train.inp","r",stdin);
freopen("train.out","w",stdout);
}
cin >> n >> k >> m >> q;
for(int i = 1; i <= n; i ++){
cin >> c[i];
sum += c[i];
#define pb push_back
p[i].push_back({i % n + 1, i, 0});
p[i % n + 1].pb({i, i, 0});
}
for(int i = 0; i <= m; i ++){
cin >> x[i];
TX += x[i];
if(i == 0){
p[1].pb({n + 1, i, 1});
p[n + 1].pb({1, i, 1});
}
else if(i == m){
p[n + m].pb({k, i, 1});
p[k].pb({n + m, i, 1});
}
else{
p[n + i].pb({n + i + 1, i, 1});
p[n + i + 1].pb({n + i, i, 1});
}
}
// if(n <= 2000 && q <= 2000 && k <= 2000 && m <= 2000){
// sub1();
// return 0;
// }
bool ff = true;
for(int i = 1; i <= q; i ++){
cin >> Q[i];
if(Q[i] == 'q') cin >> U[i] >> V[i];
else{
ff = false;
cin >> U[i];
}
}
sub3();
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 26948kb
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: 3ms
memory: 27064kb
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
Runtime Error
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...