#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) {
int i;
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 decompose(int u,vector<query> vv) {
int i;
u = centroid(u);
doBFS(p[u].first,dist[0]);
doBFS(p[u].second,dist[1]);
vector<query> vv2;
for (i = 0; i < vv.size(); i++) {
if ((dist[0][vv[i].A] == -1) && (dist[0][vv[i].B] == -1) \
&& (dist[1][vv[i].A] == -1) && (dist[1][vv[i].B] == -1)) continue;
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]));
vv2.pb(vv[i]);
}
//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;
vvv.clear();
visited[u] = 1;
for (i = 0; i < adjList2[u].size(); i++) {
int v = adjList2[u][i];
if (!visited[v]) decompose(v,vv2);
}
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);
//cout<<i<<","<<i-1<<endl;
}
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);
//cout<<i<<","<<p[S[S.size()-2]].first<<endl;
}
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 = 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:143:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
143 | scanf("%d %d %d",&N,&K,&Q);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~
railway_trip.cpp:144:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
144 | for (i = 0; i < N; i++) scanf("%d",&L[i]);
| ~~~~~^~~~~~~~~~~~
railway_trip.cpp:145:34: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
145 | for (i = 0; i < Q; i++) scanf("%d %d",&A,&B),vv.pb((query){A-1,B-1,i}),ans[i] = 1e9;
| ~~~~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
10444 KB |
Output is correct |
2 |
Correct |
10 ms |
10444 KB |
Output is correct |
3 |
Correct |
8 ms |
10444 KB |
Output is correct |
4 |
Correct |
8 ms |
10476 KB |
Output is correct |
5 |
Correct |
8 ms |
10444 KB |
Output is correct |
6 |
Correct |
8 ms |
10444 KB |
Output is correct |
7 |
Correct |
8 ms |
10476 KB |
Output is correct |
8 |
Correct |
8 ms |
10448 KB |
Output is correct |
9 |
Correct |
6 ms |
10444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
10700 KB |
Output is correct |
2 |
Execution timed out |
2082 ms |
37036 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2070 ms |
59144 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
2087 ms |
70516 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |