Advertisement
Guest User

Untitled

a guest
Oct 1st, 2021
668
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.49 KB | None | 0 0
  1. //雪花飄飄北風嘯嘯
  2. //天地一片蒼茫
  3.  
  4. #include <bits/stdc++.h>
  5. #include <ext/pb_ds/assoc_container.hpp>
  6. #include <ext/pb_ds/tree_policy.hpp>
  7. #include <ext/rope>
  8. using namespace std;
  9. using namespace __gnu_pbds;
  10. using namespace __gnu_cxx;
  11. #define ll long long
  12. #define ii pair<ll,ll>
  13. #define iii pair<ii,ll>
  14. #define fi first
  15. #define se second
  16. #define endl '\n'
  17. #define debug(x) cout << #x << ": " << x << endl
  18.  
  19. #define pub push_back
  20. #define pob pop_back
  21. #define puf push_front
  22. #define pof pop_front
  23. #define lb lower_bound
  24. #define ub upper_bound
  25.  
  26. #define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
  27. #define all(x) (x).begin(),(x).end()
  28. #define sz(x) (int)(x).size()
  29.  
  30. #define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
  31. //change less to less_equal for non distinct pbds, but erase will bug
  32.  
  33. mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
  34.  
  35. struct UFDS{
  36.     int p[200005];
  37.    
  38.     UFDS(){
  39.         rep(x,0,200005) p[x]=x;
  40.     }
  41.    
  42.     int par(int i){
  43.         if (p[i]==i) return i;
  44.         else return p[i]=par(p[i]);
  45.     }
  46.    
  47.     void unions(int i,int j){
  48.         //cout<<"d: "<<i<<" "<<j<<endl;
  49.         if (i<0) i=-i,j=-j;
  50.         i=par(i),j=par(j);
  51.         p[i]=j;
  52.     }
  53. } ufds;
  54.  
  55. struct node{
  56.     int s,e,m;
  57.     priority_queue<int,vector<int>,greater<int> > pq;
  58.     node *l,*r;
  59.    
  60.     node (int _s,int _e){
  61.         s=_s,e=_e,m=s+e>>1;
  62.        
  63.         if (s!=e){
  64.             l=new node(s,m);
  65.             r=new node(m+1,e);
  66.         }
  67.     }
  68.    
  69.     void update(int i,int j){
  70.         pq.push(j);
  71.        
  72.         if (s==e) return;
  73.         else{
  74.             if (i<=m) l->update(i,j);
  75.             else r->update(i,j);
  76.         }
  77.     }
  78.    
  79.     void query(int i,int j,int k){
  80.         if (s==i && e==j){
  81.             if (pq.empty() || k<pq.top()) return;
  82.             int temp=pq.top();
  83.             while (!pq.empty() && pq.top()<=k){
  84.                 ufds.unions(k,pq.top());
  85.                 pq.pop();
  86.             }
  87.             pq.push(temp);
  88.         }
  89.         else if (j<=m) l->query(i,j,k);
  90.         else if (m<i) r->query(i,j,k);
  91.         else l->query(i,m,k),r->query(m+1,j,k);
  92.     }
  93. } *root1=new node(0,200005),*root2=new node(0,200005);
  94.  
  95. int n,q;
  96.  
  97.  
  98. int main(){
  99.     ios::sync_with_stdio(0);
  100.     cin.tie(0);
  101.     cout.tie(0);
  102.     cin.exceptions(ios::badbit | ios::failbit);
  103.    
  104.     cin>>n>>q;
  105.    
  106.     int a,b;
  107.     rep(x,0,q){
  108.         cin>>a;
  109.        
  110.         if (a==1){
  111.             cin>>a>>b;
  112.             if (a>b) swap(a,b);
  113.            
  114.             ufds.unions(a,b);
  115.            
  116.             root1->query(a,b,a);
  117.             root1->update(b,a);
  118.            
  119.             root2->query(a,b,-b);
  120.             root2->update(a,-b);
  121.         }
  122.         else{
  123.             cin>>a>>b;
  124.            
  125.             if (ufds.par(a)==ufds.par(b)) cout<<1;
  126.             else cout<<0;
  127.         }
  128.     }
  129. }
  130.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement