Submission #77830

#TimeUsernameProblemLanguageResultExecution timeMemory
77830tmwilliamlin168Wild Boar (JOI18_wild_boar)C++14
12 / 100
4 ms608 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define a array const int mxN=2e3, mxL=1e5; const ll INF=1e18; int n, m, t, l, x[mxL], eu, ev[2*mxN], ew[mxN], nx[4*mxN], le[mxN], pv[4*mxN]; ll d[4*mxN]; a<ll, 3> st[1<<18][4]; priority_queue<a<ll, 2>, vector<a<ll, 2>>, greater<a<ll, 2>>> pq; a<ll, 3> dij(int s, int t, int ia, int ib) { memset(d, 0x3f, 8*4*m); while(pq.size()) pq.pop(); // cout << "iab " << ia << " " << ib << endl; if(ia==-1) { s=2*le[s]; d[s]=0; pq.push({0, s}); pv[s]=-1; } else { ia=2*ia+(ev[2*ia]==s); for(int i=0, j; i<2; ++i) { if((j=nx[2*ia+i])==-1) continue; d[j]=0; pq.push({0, j}); pv[j]=-1; // cout << "ie " << j << " " << 2*ia+i << endl; } } while(pq.size()) { a<ll, 2> u=pq.top(); pq.pop(); if(u[0]>d[u[1]]) continue; int e=u[1]/2; // if(s==12&&t==3) // cout << u[1] << " " << u[0] << " " << ev[e^1] << " " << ev[e] << endl; if(ev[e]!=t||e/2==ib) { for(int i=0; i<2; ++i) { int v=nx[(e^1)*2+i]; if(v==-1||d[v]<=u[0]+ew[e/2]) continue; d[v]=u[0]+ew[e/2]; pq.push({d[v], v}); pv[v]=u[1]; } } else { int v=u[1]; for(; pv[v]!=-1; v=pv[v]); assert(ia==-1||v/4!=ia/2); return {u[0]+ew[e/2], v/4, e/2}; } if(nx[u[1]]!=-1&&d[nx[u[1]]]>u[0]) { d[nx[u[1]]]=u[0]; pq.push({u[0], nx[u[1]]}); pv[nx[u[1]]]=pv[u[1]]; } } return {INF, -1, -1}; } void upd(int l1, int i=1, int l2=0, int r2=l-2) { if(l2==r2) { st[i][0]=dij(x[l1], x[l1+1], -1, -1); st[i][1]=dij(x[l1], x[l1+1], st[i][0][1], st[i][0][2]); st[i][2]=dij(x[l1], x[l1+1], st[i][0][1], st[i][1][2]); st[i][3]=dij(x[l1], x[l1+1], st[i][1][1], st[i][0][2]); // for(int j=0; j<4; ++j) { // cout << "o " << i << " " << j << endl; // for(int k=0; k<3; ++k) // cout << st[i][j][k] << " "; // cout << endl; // } return; } int m2=(l2+r2)/2; if(l1<=m2) upd(l1, 2*i, l2, m2); else upd(l1, 2*i+1, m2+1, r2); st[i][0]={0, -1, -1}; auto b=[&](const int &j) { st[i][j]={INF, -1, -1}; for(int k=0; k<4; ++k) for(int l=0; l<4; ++l) if(st[2*i][k][1]!=st[i][j&1?0:3][1]&&st[2*i+1][l][2]!=st[i][j&2?0:3][2]&&st[2*i][k][2]!=st[2*i+1][l][1]) st[i][j]=min(a<ll, 3>{st[2*i][k][0]+st[2*i+1][l][0], st[2*i][k][1], st[2*i+1][l][2]}, st[i][j]); }; b(3); for(int j=0; j<3; ++j) b(j); // for(int j=0; j<4; ++j) { // cout << "I " << i << " " << j << endl; // for(int k=0; k<3; ++k) // cout << st[i][j][k] << " "; // cout << endl; // } } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> t >> l; memset(le, -1, 4*n); memset(nx, -1, 4*4*m); for(int i=0; i<2*m; ++i) { if(i&1) { ev[i]=eu; eu=ev[i^1]; } else cin >> eu >> ev[i] >> ew[i/2], --eu, --ev[i]; if(le[eu]!=-1) { nx[2*i]=2*le[eu]; nx[2*le[eu]+1]=2*i+1; } else nx[2*i]=-1; le[eu]=i; // cout << "e " << i << " " << eu << " " << ev[i] << endl; } for(int i=0; i<l; ++i) { cin >> x[i], --x[i]; if(i) upd(i-1); } while(t--) { int pk, qk; cin >> pk >> qk, --pk, --qk; x[pk]=qk; if(pk) upd(pk-1); if(pk<l-1) upd(pk); ll ans=min({st[1][0][0], st[1][1][0], st[1][2][0], st[1][3][0]}); cout << (ans>=INF?-1:ans) << "\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...