#include "island.h"
#include <bits/stdc++.h>
#define sz(V) ((int)(V).size())
using namespace std;
using pi = pair<int, int>;
const int MAXN = 400005;
struct disj{
int pa[MAXN];
void init(int n){
iota(pa, pa + n + 1, 0);
}
int find(int x){
return pa[x] = (pa[x] == x ? x : find(pa[x]));
}
bool uni(int p, int q){
p = find(p);
q = find(q);
if(p == q) return 0;
if(p < q) swap(p, q);
pa[q] = p; return 1;
}
}disj;
int N, piv;
vector<int> gph[MAXN];
vector<int> V[MAXN];
int din[MAXN], dout[MAXN], cst[MAXN];
int elem[20][MAXN];
void dfs(int x){
for(auto &i : gph[x]){
din[i] = ++piv;
elem[0][din[i]] = cst[x];
dfs(i);
dout[i] = ++piv;
elem[0][dout[i]] = cst[x];
}
}
bool cmp(int x, int y){ return din[x] < din[y]; }
int lg[MAXN];
void Init(int K, std::vector<int> F, std::vector<int> S, std::vector<int> E){
for(int i=1; i<MAXN; i++){
lg[i] = lg[i-1];
while((2 << lg[i]) <= i) lg[i]++;
}
N = sz(F);
disj.init(2 * N);
int cnt = N;
memset(cst, 0x3f, sizeof(cst));
for(int i=(int)S.size()-1; i>=0; i--){
int l = disj.find(S[i]);
int r = disj.find(E[i]);
if(disj.find(l) != disj.find(r)){
disj.uni(l, cnt);
disj.uni(r, cnt);
cst[cnt] = i + 1;
gph[cnt].push_back(l);
gph[cnt].push_back(r);
cnt++;
}
}
memset(elem, 0x3f, sizeof(elem));
dfs(cnt - 1);
for(int i=0; i<N; i++){
V[F[i]].push_back(i);
}
for(int i=0; i<K; i++) sort(V[i].begin(), V[i].end(), cmp);
for(int i=1; i<20; i++){
for(int j=1; j<=piv; j++){
elem[i][j] = elem[i-1][j];
if(j >= (1 << (i-1))) elem[i][j] = min(elem[i][j], elem[i-1][j - (1<<(i-1))]);
}
}
}
int QUERY(int x, int y){
int L = lg[y - x + 1];
return min(elem[L][y], elem[L][x + (1<<L) - 1]);
}
map<pi, int> mp;
int Separate(int A, int B){
if(sz(V[A]) > sz(V[B])) swap(A, B);
if(mp.find(pi(A, B)) != mp.end()) return mp[pi(A, B)];
int ret = 0;
for(auto &i : V[A]){
auto itr = upper_bound(V[B].begin(), V[B].end(), i, cmp);
if(itr != V[B].begin()){
ret = max(ret, QUERY(dout[*prev(itr)], din[i]));
}
if(itr != V[B].end()){
ret = max(ret, QUERY(din[i] + 1, din[*itr]));
}
}
mp[pi(A, B)] = mp[pi(B, A)] = ret;
return ret;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
339 ms |
77268 KB |
Output is correct |
2 |
Correct |
340 ms |
77372 KB |
Output is correct |
3 |
Correct |
339 ms |
77244 KB |
Output is correct |
4 |
Correct |
364 ms |
77300 KB |
Output is correct |
5 |
Correct |
349 ms |
77212 KB |
Output is correct |
6 |
Correct |
349 ms |
77388 KB |
Output is correct |
7 |
Correct |
351 ms |
77244 KB |
Output is correct |
8 |
Correct |
354 ms |
77248 KB |
Output is correct |
9 |
Correct |
342 ms |
77252 KB |
Output is correct |
10 |
Correct |
346 ms |
77276 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
53624 KB |
Output is correct |
2 |
Correct |
48 ms |
53532 KB |
Output is correct |
3 |
Correct |
252 ms |
70016 KB |
Output is correct |
4 |
Correct |
301 ms |
69988 KB |
Output is correct |
5 |
Correct |
484 ms |
70124 KB |
Output is correct |
6 |
Correct |
585 ms |
70012 KB |
Output is correct |
7 |
Correct |
892 ms |
70112 KB |
Output is correct |
8 |
Correct |
1519 ms |
70684 KB |
Output is correct |
9 |
Correct |
1190 ms |
73348 KB |
Output is correct |
10 |
Correct |
871 ms |
73924 KB |
Output is correct |
11 |
Correct |
850 ms |
73952 KB |
Output is correct |
12 |
Correct |
887 ms |
73976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
53624 KB |
Output is correct |
2 |
Correct |
48 ms |
53532 KB |
Output is correct |
3 |
Correct |
339 ms |
77268 KB |
Output is correct |
4 |
Correct |
340 ms |
77372 KB |
Output is correct |
5 |
Correct |
339 ms |
77244 KB |
Output is correct |
6 |
Correct |
364 ms |
77300 KB |
Output is correct |
7 |
Correct |
349 ms |
77212 KB |
Output is correct |
8 |
Correct |
349 ms |
77388 KB |
Output is correct |
9 |
Correct |
351 ms |
77244 KB |
Output is correct |
10 |
Correct |
354 ms |
77248 KB |
Output is correct |
11 |
Correct |
342 ms |
77252 KB |
Output is correct |
12 |
Correct |
346 ms |
77276 KB |
Output is correct |
13 |
Correct |
252 ms |
70016 KB |
Output is correct |
14 |
Correct |
301 ms |
69988 KB |
Output is correct |
15 |
Correct |
484 ms |
70124 KB |
Output is correct |
16 |
Correct |
585 ms |
70012 KB |
Output is correct |
17 |
Correct |
892 ms |
70112 KB |
Output is correct |
18 |
Correct |
1519 ms |
70684 KB |
Output is correct |
19 |
Correct |
1190 ms |
73348 KB |
Output is correct |
20 |
Correct |
871 ms |
73924 KB |
Output is correct |
21 |
Correct |
850 ms |
73952 KB |
Output is correct |
22 |
Correct |
887 ms |
73976 KB |
Output is correct |
23 |
Correct |
770 ms |
74280 KB |
Output is correct |
24 |
Correct |
518 ms |
74472 KB |
Output is correct |
25 |
Correct |
459 ms |
74468 KB |
Output is correct |
26 |
Correct |
406 ms |
74620 KB |
Output is correct |
27 |
Correct |
382 ms |
74684 KB |
Output is correct |
28 |
Correct |
358 ms |
75212 KB |
Output is correct |
29 |
Correct |
343 ms |
75636 KB |
Output is correct |
30 |
Correct |
342 ms |
76500 KB |
Output is correct |
31 |
Correct |
347 ms |
76828 KB |
Output is correct |
32 |
Correct |
333 ms |
77208 KB |
Output is correct |