#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#define int long long
using namespace std;
struct Dsu{
int n;
vector<int> pr;
Dsu(int N){
n = N + 4;
pr.resize(n);
iota(pr.begin(), pr.end(), 0);
}
int fd(int x){
return (x == pr[x] ? x : pr[x] = fd(pr[x]));
}
bool unite(int x, int y){
x = fd(x), y = fd(y);
if(x != y){
pr[x] = y;
return true;
}
return false;
}
};
struct Seg{
int n;
vector<int> tr, chk;
Seg(int N){
n = N + 4;
tr.resize(n * 4 + 4);
chk.resize(n * 4 + 4);
}
void push(int x, int s, int e, int ps, int pe){
if(pe < s || ps > e) return;
if(ps <= s && pe >= e){
chk[x] = 1;
tr[x] = e - s + 1;
return;
}
int m = s + e >> 1;
push(x * 2, s, m, ps, pe);
push(x * 2 + 1, m + 1, e, ps, pe);
if(chk[x]) tr[x] = e - s + 1;
else tr[x] = tr[x * 2] + tr[x * 2 + 1];
}
int get(int x, int s, int e, int fs, int fe){
if(fe < s || fs > e || fs > fe) return 0;
if(chk[x]) return min(fe, e) - max(fs, s) + 1;
if(fs <= s && fe >= e) return tr[x];
int m = s + e >> 1;
return get(x * 2, s, m, fs, fe) + get(x * 2 + 1, m + 1, e, fs, fe);
}
};
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n, q;
cin >> n >> q;
vector<vector<int>> que(q, vector<int>(3));
vector<vector<int>> way(n);
Dsu gr(n + 4);
for(int i = 0; i < q; ++i){
cin >> que[i][0] >> que[i][1] >> que[i][2];
--que[i][1], --que[i][2];
if(que[i][0] == 1){
if(gr.unite(que[i][1], que[i][2])){
// cout << que[i][1] << ' ' << que[i][2] << endl;
way[que[i][1]].push_back(que[i][2]);
way[que[i][2]].push_back(que[i][1]);
}
}
}
vector<int> dep(n), sz(n), up(n), in(n);
vector<vector<int>> spa(n, vector<int>(20));
int inN = 0;
auto dfs = [&](auto&&self, int x)->void{
for(auto&nxt:way[x]){
if(nxt == spa[x][0]) continue;
spa[nxt][0] = x;
dep[nxt] = dep[x] + 1;
self(self, nxt);
sz[x] += sz[nxt];
}
sz[x] += 1;
};
for(int i = 0; i < n; ++i){
if(!dep[i]){
dep[i] = 1;
spa[i][0] = i;
dfs(dfs, i);
}
}
for(int j = 1; j < 20; ++j){
for(int i = 0; i < n; ++i){
spa[i][j] = spa[spa[i][j - 1]][j - 1];
}
}
auto dfshld = [&](auto&&self, int x)->void{
in[x] = ++inN;
for(int i = 0; i < (int)way[x].size(); ++i){
if(way[x][i] == spa[x][0]){
swap(way[x][i], way[x].back());
way[x].pop_back();
break;
}
}
for(int i = 1; i < (int)way[x].size(); ++i){
if(sz[way[x][i]] > sz[way[x][0]]){
swap(way[x][i], way[x][0]);
}
}
for(int i = 0; i < (int)way[x].size(); ++i){
if(!i) up[way[x][i]] = up[x];
else up[way[x][i]] = way[x][i];
self(self, way[x][i]);
}
};
for(int i = 0; i < n; ++i){
if(i == spa[i][0]){
up[i] = i;
dfshld(dfshld, i);
}
}
vector<int> cnt(n);
gr = Dsu(n);
auto getlca = [&](int x, int y){
if(dep[x] > dep[y]) swap(x, y);
for(int i = 19; i >= 0; --i){
if(dep[y] - (1 << i) >= dep[x]){
y = spa[y][i];
}
}
if(x == y) return x;
for(int i = 19; i >= 0; --i){
if(spa[x][i] != spa[y][i]){
x = spa[x][i];
y = spa[y][i];
}
}
return spa[x][0];
};
Seg tr(n + 4);
for(int i = 0; i < q; ++i){
int x = que[i][1], y = que[i][2];
if(que[i][0] == 1){
if(gr.unite(x, y)){
continue;
}
int lca = getlca(x, y);
while(x != lca){
int u = up[x];
if(dep[u] <= dep[lca]) u = way[lca][0];
tr.push(1, 1, n, in[u], in[x]);
x = spa[u][0];
}
while(y != lca){
int u = up[y];
if(dep[u] <= dep[lca]) u = way[lca][0];
tr.push(1, 1, n, in[u], in[y]);
y = spa[u][0];
}
}
else{
if(gr.fd(x) != gr.fd(y)){
cout << "-1\n";
continue;
}
int lca = getlca(x, y);
int ans = dep[x] + dep[y] - dep[lca] * 2;
while(x != lca){
int u = up[x];
if(dep[u] <= dep[lca]) u = way[lca][0];
ans -= tr.get(1, 1, n, in[u], in[x]);
x = spa[u][0];
}
while(y != lca){
int u = up[y];
if(dep[u] <= dep[lca]) u = way[lca][0];
ans -= tr.get(1, 1, n, in[u], in[y]);
y = spa[u][0];
}
cout << ans << '\n';
}
}
return 0;
}
Compilation message
road_development.cpp: In member function 'void Seg::push(long long int, long long int, long long int, long long int, long long int)':
road_development.cpp:44:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
44 | int m = s + e >> 1;
| ~~^~~
road_development.cpp: In member function 'long long int Seg::get(long long int, long long int, long long int, long long int, long long int)':
road_development.cpp:54:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | int m = s + e >> 1;
| ~~^~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
2 ms |
852 KB |
Output is correct |
7 |
Correct |
3 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
277 ms |
53656 KB |
Output is correct |
2 |
Correct |
556 ms |
53524 KB |
Output is correct |
3 |
Correct |
505 ms |
56568 KB |
Output is correct |
4 |
Correct |
519 ms |
60220 KB |
Output is correct |
5 |
Correct |
476 ms |
59428 KB |
Output is correct |
6 |
Correct |
510 ms |
58472 KB |
Output is correct |
7 |
Correct |
595 ms |
57656 KB |
Output is correct |
8 |
Correct |
502 ms |
58840 KB |
Output is correct |
9 |
Correct |
522 ms |
56380 KB |
Output is correct |
10 |
Correct |
466 ms |
57248 KB |
Output is correct |
11 |
Correct |
515 ms |
59204 KB |
Output is correct |
12 |
Correct |
387 ms |
61148 KB |
Output is correct |
13 |
Correct |
482 ms |
60644 KB |
Output is correct |
14 |
Correct |
508 ms |
58924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
283 ms |
53664 KB |
Output is correct |
2 |
Correct |
502 ms |
53592 KB |
Output is correct |
3 |
Correct |
482 ms |
55804 KB |
Output is correct |
4 |
Correct |
447 ms |
60624 KB |
Output is correct |
5 |
Correct |
288 ms |
57708 KB |
Output is correct |
6 |
Correct |
547 ms |
59984 KB |
Output is correct |
7 |
Correct |
215 ms |
57652 KB |
Output is correct |
8 |
Correct |
459 ms |
59200 KB |
Output is correct |
9 |
Correct |
381 ms |
59012 KB |
Output is correct |
10 |
Correct |
556 ms |
60028 KB |
Output is correct |
11 |
Correct |
305 ms |
56548 KB |
Output is correct |
12 |
Correct |
483 ms |
60484 KB |
Output is correct |
13 |
Correct |
534 ms |
60492 KB |
Output is correct |
14 |
Correct |
429 ms |
57660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
186 ms |
48032 KB |
Output is correct |
2 |
Correct |
353 ms |
47900 KB |
Output is correct |
3 |
Correct |
351 ms |
49700 KB |
Output is correct |
4 |
Correct |
350 ms |
53084 KB |
Output is correct |
5 |
Correct |
670 ms |
56772 KB |
Output is correct |
6 |
Correct |
489 ms |
59708 KB |
Output is correct |
7 |
Correct |
90 ms |
43864 KB |
Output is correct |
8 |
Correct |
99 ms |
46168 KB |
Output is correct |
9 |
Correct |
158 ms |
47604 KB |
Output is correct |
10 |
Correct |
144 ms |
50832 KB |
Output is correct |
11 |
Correct |
454 ms |
55828 KB |
Output is correct |
12 |
Correct |
486 ms |
58668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
852 KB |
Output is correct |
2 |
Correct |
2 ms |
852 KB |
Output is correct |
3 |
Correct |
2 ms |
852 KB |
Output is correct |
4 |
Correct |
2 ms |
852 KB |
Output is correct |
5 |
Correct |
3 ms |
852 KB |
Output is correct |
6 |
Correct |
2 ms |
852 KB |
Output is correct |
7 |
Correct |
3 ms |
852 KB |
Output is correct |
8 |
Correct |
2 ms |
852 KB |
Output is correct |
9 |
Correct |
277 ms |
53656 KB |
Output is correct |
10 |
Correct |
556 ms |
53524 KB |
Output is correct |
11 |
Correct |
505 ms |
56568 KB |
Output is correct |
12 |
Correct |
519 ms |
60220 KB |
Output is correct |
13 |
Correct |
476 ms |
59428 KB |
Output is correct |
14 |
Correct |
510 ms |
58472 KB |
Output is correct |
15 |
Correct |
595 ms |
57656 KB |
Output is correct |
16 |
Correct |
502 ms |
58840 KB |
Output is correct |
17 |
Correct |
522 ms |
56380 KB |
Output is correct |
18 |
Correct |
466 ms |
57248 KB |
Output is correct |
19 |
Correct |
515 ms |
59204 KB |
Output is correct |
20 |
Correct |
387 ms |
61148 KB |
Output is correct |
21 |
Correct |
482 ms |
60644 KB |
Output is correct |
22 |
Correct |
508 ms |
58924 KB |
Output is correct |
23 |
Correct |
283 ms |
53664 KB |
Output is correct |
24 |
Correct |
502 ms |
53592 KB |
Output is correct |
25 |
Correct |
482 ms |
55804 KB |
Output is correct |
26 |
Correct |
447 ms |
60624 KB |
Output is correct |
27 |
Correct |
288 ms |
57708 KB |
Output is correct |
28 |
Correct |
547 ms |
59984 KB |
Output is correct |
29 |
Correct |
215 ms |
57652 KB |
Output is correct |
30 |
Correct |
459 ms |
59200 KB |
Output is correct |
31 |
Correct |
381 ms |
59012 KB |
Output is correct |
32 |
Correct |
556 ms |
60028 KB |
Output is correct |
33 |
Correct |
305 ms |
56548 KB |
Output is correct |
34 |
Correct |
483 ms |
60484 KB |
Output is correct |
35 |
Correct |
534 ms |
60492 KB |
Output is correct |
36 |
Correct |
429 ms |
57660 KB |
Output is correct |
37 |
Correct |
186 ms |
48032 KB |
Output is correct |
38 |
Correct |
353 ms |
47900 KB |
Output is correct |
39 |
Correct |
351 ms |
49700 KB |
Output is correct |
40 |
Correct |
350 ms |
53084 KB |
Output is correct |
41 |
Correct |
670 ms |
56772 KB |
Output is correct |
42 |
Correct |
489 ms |
59708 KB |
Output is correct |
43 |
Correct |
90 ms |
43864 KB |
Output is correct |
44 |
Correct |
99 ms |
46168 KB |
Output is correct |
45 |
Correct |
158 ms |
47604 KB |
Output is correct |
46 |
Correct |
144 ms |
50832 KB |
Output is correct |
47 |
Correct |
454 ms |
55828 KB |
Output is correct |
48 |
Correct |
486 ms |
58668 KB |
Output is correct |
49 |
Correct |
358 ms |
57808 KB |
Output is correct |
50 |
Correct |
551 ms |
57680 KB |
Output is correct |
51 |
Correct |
516 ms |
60272 KB |
Output is correct |
52 |
Correct |
448 ms |
61244 KB |
Output is correct |
53 |
Correct |
246 ms |
54816 KB |
Output is correct |
54 |
Correct |
259 ms |
55108 KB |
Output is correct |
55 |
Correct |
435 ms |
57276 KB |
Output is correct |
56 |
Correct |
378 ms |
57932 KB |
Output is correct |
57 |
Correct |
538 ms |
58464 KB |
Output is correct |
58 |
Correct |
408 ms |
57812 KB |
Output is correct |
59 |
Correct |
374 ms |
57144 KB |
Output is correct |
60 |
Correct |
340 ms |
56796 KB |
Output is correct |
61 |
Correct |
410 ms |
58208 KB |
Output is correct |