fork download
  1. #include <bits/stdc++.h>
  2. #define ll int
  3. #define el "\n"
  4. #define fast ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  5. #define __ROOT__ int main()
  6. #define fi first
  7. #define se second
  8. #define M 1000000007
  9. #define MAXN 1000001
  10. using namespace std;
  11.  
  12. ll n, q;
  13. ll arr[MAXN], sz[MAXN], par[MAXN], next_pos[MAXN];
  14.  
  15. ll find_set(ll a) {
  16. if (a == par[a]) return a;
  17. return par[a] = find_set(par[a]);
  18. }
  19.  
  20. void union_set(ll a, ll b) {
  21. a = find_set(a);
  22. b = find_set(b);
  23. if (a == b) return;
  24. if (sz[a] < sz[b]) swap(a, b);
  25. par[b] = a;
  26. sz[a] += sz[b];
  27. }
  28.  
  29. void init() {
  30. cin >> n >> q;
  31. for (ll i = 1; i <= n; i++) {
  32. par[i] = i;
  33. sz[i] = 1;
  34. next_pos[i] = i + 1;
  35. }
  36. }
  37.  
  38. void merge_range(ll x, ll y ){
  39. while(x<= y){
  40. union_set(x,x+1) ;
  41. ll t = x;
  42. x = next_pos[x] ; // bỏ qua các vị trí đã hợp
  43. next_pos[t] = y +1 ;
  44. }
  45. }
  46.  
  47. void solve() {
  48. while (q--) {
  49. ll t, x, y;
  50. cin >> t >> x >> y;
  51. if (t == 1) {
  52. union_set(x, y);
  53. } else if (t == 2) {
  54. merge_range(x, y);
  55. } else {
  56. x = find_set(x);
  57. y = find_set(y);
  58. if (x == y) {
  59. cout << "YES" << el;
  60. } else {
  61. cout << "NO" << el;
  62. }
  63. }
  64. }
  65. }
  66.  
  67. __ROOT__ {
  68. fast;
  69. init();
  70. solve();
  71. }
  72.  
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty