-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdsuminmax.cpp
More file actions
108 lines (84 loc) · 1.55 KB
/
dsuminmax.cpp
File metadata and controls
108 lines (84 loc) · 1.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <iostream>
using namespace std;
#include <vector>
#include <algorithm>
#include <math.h>
#include <map>
#include <queue>
#define ll long long int
vector<ll> p;
vector<ll> r;
vector<ll> mi;
vector<ll> ma;
vector<ll> sz;
ll get(ll a) {
return p[a] = (p[a] == a ? a : get(p[a]));
}
void _union(ll a, ll b){
a = get(a);
b = get(b);
if (r[a] == r[b])
r[a]++;
if (r[a] > r[b]) {
if (a != b)
sz[a] += sz[b];
p[b] = a;
//sz[b] = 0;
mi[a] = min(mi[a], mi[b]);
ma[a] = max(ma[a], ma[b]);
}
else {
if (a != b)
sz[b] += sz[a];
p[a] = b;
//sz[a] = 0;
mi[b] = min(mi[a], mi[b]);
ma[b] = max(ma[a], ma[b]);
}
}
void solve() {
ll n, m;
cin >> n >> m;
p.resize(n, 0);
r.resize(n, 0);
mi.resize(n, 0);
ma.resize(n, 0);
sz.resize(n, 0);
for (int j = 0; j < n; j++) {
p[j] = j;
mi[j] = j;
ma[j] = j;
sz[j] = 1;
}
for (int i = 0; i < m; i++) {
string s;
int u, v;
cin >> s;
if (s == "union") {
cin >> u >> v; u--; v--;
_union(u, v);
/*for (int j = 0; j < n; j++)
cout << p[j] << " ";
cout << endl;
for (int j = 0; j < n; j++)
cout << sz[j] << " + ";
cout << endl;*/
}
else if (s == "get") {
cin >> u; u--;
ll a = get(u);
cout << (mi[a]+1) << " " << (ma[a]+1) << " " << sz[a] << endl;
}
}
}
int main() {
// your code goes here
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//ll T;
//cin >> T;
//while (T--) {
solve();
//}
return 0;
}