#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
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);
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
10444 KB |
Output is correct |
2 |
Correct |
7 ms |
10444 KB |
Output is correct |
3 |
Correct |
7 ms |
10444 KB |
Output is correct |
4 |
Correct |
8 ms |
10444 KB |
Output is correct |
5 |
Correct |
7 ms |
10444 KB |
Output is correct |
6 |
Correct |
7 ms |
10444 KB |
Output is correct |
7 |
Correct |
8 ms |
10448 KB |
Output is correct |
8 |
Correct |
7 ms |
10444 KB |
Output is correct |
9 |
Correct |
6 ms |
10444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
10700 KB |
Output is correct |
2 |
Correct |
249 ms |
38080 KB |
Output is correct |
3 |
Correct |
266 ms |
39032 KB |
Output is correct |
4 |
Correct |
277 ms |
39932 KB |
Output is correct |
5 |
Correct |
266 ms |
40256 KB |
Output is correct |
6 |
Correct |
262 ms |
40096 KB |
Output is correct |
7 |
Correct |
298 ms |
39876 KB |
Output is correct |
8 |
Correct |
134 ms |
37176 KB |
Output is correct |
9 |
Correct |
191 ms |
38096 KB |
Output is correct |
10 |
Correct |
168 ms |
37708 KB |
Output is correct |
11 |
Correct |
237 ms |
37240 KB |
Output is correct |
12 |
Correct |
242 ms |
37304 KB |
Output is correct |
13 |
Correct |
237 ms |
37428 KB |
Output is correct |
14 |
Correct |
240 ms |
37304 KB |
Output is correct |
15 |
Correct |
232 ms |
37240 KB |
Output is correct |
16 |
Correct |
235 ms |
37304 KB |
Output is correct |
17 |
Correct |
297 ms |
39920 KB |
Output is correct |
18 |
Correct |
301 ms |
39860 KB |
Output is correct |
19 |
Correct |
225 ms |
39940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
356 ms |
42680 KB |
Output is correct |
2 |
Correct |
341 ms |
43592 KB |
Output is correct |
3 |
Correct |
355 ms |
44220 KB |
Output is correct |
4 |
Correct |
351 ms |
44220 KB |
Output is correct |
5 |
Correct |
352 ms |
44268 KB |
Output is correct |
6 |
Correct |
356 ms |
44392 KB |
Output is correct |
7 |
Correct |
359 ms |
44472 KB |
Output is correct |
8 |
Correct |
251 ms |
39740 KB |
Output is correct |
9 |
Correct |
194 ms |
42812 KB |
Output is correct |
10 |
Correct |
195 ms |
42824 KB |
Output is correct |
11 |
Correct |
193 ms |
42888 KB |
Output is correct |
12 |
Correct |
191 ms |
42776 KB |
Output is correct |
13 |
Correct |
193 ms |
42912 KB |
Output is correct |
14 |
Correct |
203 ms |
37360 KB |
Output is correct |
15 |
Correct |
242 ms |
36640 KB |
Output is correct |
16 |
Correct |
287 ms |
38692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
384 ms |
49564 KB |
Output is correct |
2 |
Correct |
394 ms |
46392 KB |
Output is correct |
3 |
Correct |
382 ms |
44408 KB |
Output is correct |
4 |
Correct |
375 ms |
44880 KB |
Output is correct |
5 |
Correct |
197 ms |
41520 KB |
Output is correct |
6 |
Correct |
266 ms |
42440 KB |
Output is correct |
7 |
Correct |
274 ms |
42400 KB |
Output is correct |
8 |
Correct |
234 ms |
42132 KB |
Output is correct |
9 |
Correct |
340 ms |
41640 KB |
Output is correct |
10 |
Correct |
335 ms |
43052 KB |
Output is correct |
11 |
Correct |
330 ms |
42552 KB |
Output is correct |
12 |
Correct |
344 ms |
41564 KB |
Output is correct |
13 |
Correct |
337 ms |
43196 KB |
Output is correct |
14 |
Correct |
361 ms |
45504 KB |
Output is correct |
15 |
Correct |
366 ms |
46668 KB |
Output is correct |
16 |
Correct |
367 ms |
49204 KB |
Output is correct |
17 |
Correct |
345 ms |
43640 KB |
Output is correct |
18 |
Correct |
346 ms |
43272 KB |
Output is correct |
19 |
Correct |
348 ms |
43664 KB |
Output is correct |
20 |
Correct |
342 ms |
40660 KB |
Output is correct |
21 |
Correct |
381 ms |
46380 KB |
Output is correct |
22 |
Correct |
416 ms |
48056 KB |
Output is correct |
23 |
Correct |
333 ms |
46568 KB |
Output is correct |