Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //雪花飄飄北風嘯嘯
- //天地一片蒼茫
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- #include <ext/rope>
- using namespace std;
- using namespace __gnu_pbds;
- using namespace __gnu_cxx;
- #define ll long long
- #define ii pair<ll,ll>
- #define iii pair<ii,ll>
- #define fi first
- #define se second
- #define endl '\n'
- #define debug(x) cout << #x << ": " << x << endl
- #define pub push_back
- #define pob pop_back
- #define puf push_front
- #define pof pop_front
- #define lb lower_bound
- #define ub upper_bound
- #define rep(x,start,end) for(auto x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--))
- #define all(x) (x).begin(),(x).end()
- #define sz(x) (int)(x).size()
- #define indexed_set tree<ll,null_type,less<ll>,rb_tree_tag,tree_order_statistics_node_update>
- //change less to less_equal for non distinct pbds, but erase will bug
- mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
- struct UFDS{
- int p[200005];
- UFDS(){
- rep(x,0,200005) p[x]=x;
- }
- int par(int i){
- if (p[i]==i) return i;
- else return p[i]=par(p[i]);
- }
- void unions(int i,int j){
- //cout<<"d: "<<i<<" "<<j<<endl;
- if (i<0) i=-i,j=-j;
- i=par(i),j=par(j);
- p[i]=j;
- }
- } ufds;
- struct node{
- int s,e,m;
- priority_queue<int,vector<int>,greater<int> > pq;
- node *l,*r;
- node (int _s,int _e){
- s=_s,e=_e,m=s+e>>1;
- if (s!=e){
- l=new node(s,m);
- r=new node(m+1,e);
- }
- }
- void update(int i,int j){
- pq.push(j);
- if (s==e) return;
- else{
- if (i<=m) l->update(i,j);
- else r->update(i,j);
- }
- }
- void query(int i,int j,int k){
- if (s==i && e==j){
- if (pq.empty() || k<pq.top()) return;
- int temp=pq.top();
- while (!pq.empty() && pq.top()<=k){
- ufds.unions(k,pq.top());
- pq.pop();
- }
- pq.push(temp);
- }
- else if (j<=m) l->query(i,j,k);
- else if (m<i) r->query(i,j,k);
- else l->query(i,m,k),r->query(m+1,j,k);
- }
- } *root1=new node(0,200005),*root2=new node(0,200005);
- int n,q;
- int main(){
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- cin.exceptions(ios::badbit | ios::failbit);
- cin>>n>>q;
- int a,b;
- rep(x,0,q){
- cin>>a;
- if (a==1){
- cin>>a>>b;
- if (a>b) swap(a,b);
- ufds.unions(a,b);
- root1->query(a,b,a);
- root1->update(b,a);
- root2->query(a,b,-b);
- root2->update(a,-b);
- }
- else{
- cin>>a>>b;
- if (ufds.par(a)==ufds.par(b)) cout<<1;
- else cout<<0;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement