Submission #400301

#TimeUsernameProblemLanguageResultExecution timeMemory
400301dualityRailway Trip (JOI17_railway_trip)C++11
100 / 100
416 ms49564 KiB
#define DEBUG 0 #include <bits/stdc++.h> using namespace std; #if DEBUG // basic debugging macros int __i__,__j__; #define printLine(l) for(__i__=0;__i__<l;__i__++){cout<<"-";}cout<<endl #define printLine2(l,c) for(__i__=0;__i__<l;__i__++){cout<<c;}cout<<endl #define printVar(n) cout<<#n<<": "<<n<<endl #define printArr(a,l) cout<<#a<<": ";for(__i__=0;__i__<l;__i__++){cout<<a[__i__]<<" ";}cout<<endl #define print2dArr(a,r,c) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<a[__i__][__j__]<<" ";}cout<<endl;} #define print2dArr2(a,r,c,l) cout<<#a<<":\n";for(__i__=0;__i__<r;__i__++){for(__j__=0;__j__<c;__j__++){cout<<setw(l)<<setfill(' ')<<a[__i__][__j__]<<" ";}cout<<endl;} // advanced debugging class // debug 1,2,'A',"test"; class _Debug { public: template<typename T> _Debug& operator,(T val) { cout << val << endl; return *this; } }; #define debug _Debug(), #else #define printLine(l) #define printLine2(l,c) #define printVar(n) #define printArr(a,l) #define print2dArr(a,r,c) #define print2dArr2(a,r,c,l) #define debug #endif // define #define MAX_VAL 999999999 #define MAX_VAL_2 999999999999999999LL #define EPS 1e-6 #define mp make_pair #define pb push_back // typedef typedef unsigned int UI; typedef long long int LLI; typedef unsigned long long int ULLI; typedef unsigned short int US; typedef pair<int,int> pii; typedef pair<LLI,LLI> plli; typedef vector<int> vi; typedef vector<LLI> vlli; typedef vector<pii> vpii; typedef vector<plli> vplli; // ---------- END OF TEMPLATE ---------- int L[100000]; struct query { int A,B,i; }; vector<query> vv; int ans[100000]; pii p[200000]; set<int> adjList[100000]; vi adjList2[200000]; vi S; int size[200000],visited[200000]; int doDFS(int u,int p) { int i; size[u] = 1; for (i = 0; i < adjList2[u].size(); i++) { int v = adjList2[u][i]; if (!visited[v] && (v != p)) size[u] += doDFS(v,u); } return size[u]; } int centroid(int u) { int i,p = -1,r = u; doDFS(r,-1); while (1) { int h = -1; for (i = 0; i < adjList2[u].size(); i++) { int v = adjList2[u][i]; if (!visited[v] && (v != p)) { if ((h == -1) || (size[v] > size[h])) h = v; } } if (((h == -1) || (size[h] <= size[r]/2)) && (size[r]-size[u] <= size[r]/2)) break; else p = u,u = h; } return u; } int dist[2][100000]; vi vvv; queue<int> Q; int doBFS(int u,int *dist) { Q.push(u),dist[u] = 0,vvv.pb(u); while (!Q.empty()) { int u = Q.front(); Q.pop(); for (auto it = adjList[u].begin(); it != adjList[u].end(); it++) { int v = *it; if (dist[v] == -1) { dist[v] = dist[u]+1,vvv.pb(v); Q.push(v); } } } return 0; } int f(int d) { return (d == -1) ? 1e9:d; } int top[200000]; int decompose(int u,vector<query> vv) { int i; u = centroid(u); if (!vv.empty()) { doBFS(p[u].first,dist[0]); doBFS(p[u].second,dist[1]); } vector<query> in,out; vector<query> L,R; for (i = 0; i < vv.size(); i++) { ans[vv[i].i] = min(ans[vv[i].i],f(dist[0][vv[i].A])+f(dist[0][vv[i].B])); ans[vv[i].i] = min(ans[vv[i].i],f(dist[1][vv[i].A])+f(dist[1][vv[i].B])); } for (auto it = adjList[p[u].first].begin(); it != adjList[p[u].first].end(); it++) adjList[*it].erase(p[u].first); for (auto it = adjList[p[u].second].begin(); it != adjList[p[u].second].end(); it++) adjList[*it].erase(p[u].second); adjList[p[u].first].clear(),adjList[p[u].second].clear(); for (i = 0; i < vvv.size(); i++) dist[0][vvv[i]] = dist[1][vvv[i]] = -1; for (i = 0; i < vv.size(); i++) { if ((vv[i].A == p[u].first) || (vv[i].B == p[u].first)) continue; if ((vv[i].A == p[u].second) || (vv[i].B == p[u].second)) continue; int a = (vv[i].A > p[u].first) && (vv[i].A < p[u].second); int b = (vv[i].B > p[u].first) && (vv[i].B < p[u].second); if (a == b) { if (a) in.pb(vv[i]); else { if (!top[u]) out.pb(vv[i]); else { if ((min(vv[i].A,vv[i].B) < p[u].first) && (max(vv[i].A,vv[i].B) > p[u].second)) continue; else { if (max(vv[i].A,vv[i].B) < p[u].first) L.pb(vv[i]); else R.pb(vv[i]); } } } } } vvv.clear(); visited[u] = 1; for (i = 0; i < adjList2[u].size(); i++) { int v = adjList2[u][i]; if (!visited[v]) { if ((p[v].first >= p[u].first) && (p[v].second <= p[u].second)) decompose(v,in); else { if (!top[u]) decompose(v,out); else { if (p[v].first < p[u].first) decompose(v,L); else decompose(v,R); } } } } return 0; } int main() { int i; int N,K,Q,A,B; scanf("%d %d %d",&N,&K,&Q); for (i = 0; i < N; i++) scanf("%d",&L[i]); for (i = 0; i < Q; i++) { scanf("%d %d",&A,&B); vv.pb((query){A-1,B-1,i}),ans[i] = 1e9; } int c = 0; for (i = 0; i < N; i++) { if (i > 0) { p[c++] = mp(i-1,i); S.pb(c-1); adjList[i-1].insert(i); adjList[i].insert(i-1); } int e = 1; while ((S.size() >= 2) && (L[p[S[S.size()-2]].second] < L[i])) { if (e && (L[p[S[S.size()-2]].first] > L[p[S[S.size()-2]].second])) { adjList[p[S[S.size()-2]].first].insert(i); adjList[i].insert(p[S[S.size()-2]].first); } e &= L[p[S[S.size()-2]].first] < L[i]; p[c++] = mp(p[S[S.size()-2]].first,i); adjList2[c-1].pb(S[S.size()-2]); adjList2[S[S.size()-2]].pb(c-1); adjList2[c-1].pb(S.back()); adjList2[S.back()].pb(c-1); S.pop_back(),S.pop_back(),S.pb(c-1); } } for (i = 0; i < S.size(); i++) top[S[i]] = 1; for (i = 1; i < S.size(); i++) { adjList2[S[i-1]].pb(S[i]); adjList2[S[i]].pb(S[i-1]); } memset(dist,-1,sizeof(dist)); decompose(S[0],vv); for (i = 0; i < Q; i++) printf("%d\n",ans[i]-1); return 0; }

Compilation message (stderr)

railway_trip.cpp: In function 'int main()':
railway_trip.cpp:171:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  171 |     scanf("%d %d %d",&N,&K,&Q);
      |     ~~~~~^~~~~~~~~~~~~~~~~~~~~
railway_trip.cpp:172:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  172 |     for (i = 0; i < N; i++) scanf("%d",&L[i]);
      |                             ~~~~~^~~~~~~~~~~~
railway_trip.cpp:174:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
  174 |         scanf("%d %d",&A,&B);
      |         ~~~~~^~~~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...