-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17619.cpp
More file actions
68 lines (53 loc) · 1.08 KB
/
17619.cpp
File metadata and controls
68 lines (53 loc) · 1.08 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
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
using namespace std;
int par[101010];
int sz[101010];
struct wood{
int x1, x2, i;
};
vector<wood> v;
int find(int u){
if(u==par[u]) return u;
return par[u] = find(par[u]);
}
bool comp(wood a, wood b){
return a.x1 < b.x1;
}
void unionWood(int u, int v){
int x = find(u);
int y = find(v);
if(x == y) return;
if(sz[x] > sz[y]) swap(x, y);
par[x] = y;
sz[y] += sz[x];
}
int main(){
cin.tie(NULL);
ios_base::sync_with_stdio();
int N, Q, a, b, c;
cin >> N >> Q;
for(int i=1; i<=N; i++){
cin >> a >> b >> c;
v.push_back({a, b, i});
par[i] = i;
sz[i] = 1;
}
sort(v.begin(), v.end(), comp);
wood last = v[0];
for(auto w : v){
if(w.x1 <= last.x2){
unionWood(w.i, last.i);
if(w.x2 > last.x2) last = w;
}else{
last = w;
}
}
for(int i=0; i<Q; i++){
cin >> a >> b;
if(find(a)==find(b)) cout << 1 << '\n';
else cout << 0 << '\n';
}
}