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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| #include<iostream> #include<algorithm> #include<cstring> #include<cstdio> #include<map> #include<ctime> using namespace std; #define MAXN 1000216 #define swap( a , b ) ( (a)^=(b),(b)^=(a),(a)^=(b) ) #define pii pair<int,int> #define fi first #define se second int n , m , q , ty; int fa[MAXN] , ch[MAXN][2] , rev[MAXN] , S[MAXN] , val[MAXN]; #define ls(x) ch[x][0] #define rs(x) ch[x][1] #define il inline #define rg register il bool nr( int x ) { return ch[fa[x]][0] == x || ch[fa[x]][1] == x; } il void pu( int x ) { S[x] = ( val[S[ch[x][0]]] > val[S[ch[x][1]]] ? S[ch[x][1]] : S[ch[x][0]] ); if( val[x] < val[S[x]] ) S[x] = x; } il void rv( int x ) { swap( ch[x][0] , ch[x][1] ) , rev[x] ^= 1; } il void pd( int x ) { if( rev[x] ) { if( ch[x][0] ) rv( ch[x][0] ); if( ch[x][1] ) rv( ch[x][1] ); rev[x] = 0; } } il void ro( int x ) { int f = fa[x] , g = fa[f] , w = ch[fa[x]][1] == x , k = ch[x][w^1]; if( nr( f ) ) ch[g][ch[g][1]==f] = x; ch[f][w] = k , ch[x][w ^ 1] = f; fa[f] = x , fa[k] = f , fa[x] = g; pu( f );// pu( x ); } il void up( int x ) { if( nr( x ) ) up( fa[x] ); pd( x ); } il void sp( int x ) { up( x ); int f , g; while( nr( x ) ) { f = fa[x] , g = fa[f]; if( nr( f ) ) ro( ( ( ch[f][0] == x ) ^ ( ch[g][0] == f ) ) ? x : f ); ro( x ); } pu( x ); } il void ac( int x ) { for( int p = 0 ; x ; ( p = x , x = fa[x] ) ) sp( x ) , ch[x][1] = p , pu( x ); } il void mr( int x ) { ac( x ); sp( x ); rv( x ); } il int fr( int x ) { ac( x ) , sp( x ); while( ls( x ) ) pd( x ) , x = ls( x ); sp( x ); return x; } inline bool lk( int u , int v ) { // if( rand() % 2 ) swap( u , v ); mr( u ); // if( fr( v ) == u ) return false; fa[u] = v; return true; } il void ct( int u , int v ) { mr( u ); // if( fr(v) == u&& fa[v] == u && !ch[v][0] ) { ac( v ); sp( u ); fa[v] = ch[u][1] = 0; pu( u ); // } } inline int qr( int u , int v ) { mr( u ); // if( fr( v ) != u ) return -1; ac( v );sp( v ); return val[S[v]]; }
int T[MAXN << 4] , ls[MAXN << 4] , rs[MAXN << 4] , idx = 0; int roo[MAXN]; void build( int& rt , int l , int r ) { rt = ++ idx , T[rt] = 0; if( l == r ) { return; } int m = l + r >> 1; build( ls[rt] , l , m ) , build( rs[rt] , m + 1 , r ); } void mdfy( int& rt , int old , int l , int r , int p , int c ) { rt = ++ idx; T[rt] = T[old] + c , ls[rt] = ls[old] , rs[rt] = rs[old]; if( l == r ) return; int m = l + r >> 1; if( p <= m ) mdfy( ls[rt] , ls[old] , l , m , p , c ); else mdfy( rs[rt] , rs[old] , m + 1 , r , p , c ); } int que( int rt , int l , int r , int p ) { if( l == r && l == p ) return 0; int m = l + r >> 1; if( p <= m ) return que( ls[rt] , l , m , p ); else return T[ls[rt]] + que( rs[rt] , m + 1 , r , p ); }
int FA[MAXN]; int find( int x ) { return x == FA[x] ? x : FA[x] = find( FA[x] ); }
pii bac[MAXN]; int cid = 0 , res = 0x3f3f3f3f; int id[MAXN]; int main() { // freopen("3.in","r",stdin); cin >> n >> m >> q >> ty; cid = n; build( roo[0] , 1 , m ); memset( val , 0x3f , sizeof val ); for( int i = 1 ; i <= n ; ++ i ) FA[i] = i; for( int i = 1 , u , v ; i <= m ;++ i ) { scanf("%d%d",&u,&v); if( u == v ) { roo[i] = roo[i - 1]; continue; } ++ cid; if( find( u ) != find( v ) ) { bac[cid] = make_pair( u , v ); FA[find( u )] = find( v ); val[cid] = i , pu( cid ); lk( u , cid ) , lk( v , cid ); mdfy( roo[i] , roo[i - 1] , 0 , m , 0 , 1 ); continue; } mr( u ) , ac( v ) , sp( v ); id[i] = val[S[v]]; mdfy( roo[i] , roo[i - 1] , 0 , m , val[S[v]] , 1 ); int U = bac[S[v]].fi , V = bac[S[v]].se , ttt = S[v]; ct( U , ttt ) , ct( V , ttt ); ++ cid , bac[cid] = make_pair( u , v ) , val[cid] = i , pu( cid ); lk( u , cid ) , lk( v , cid ); } // for( int i = 1 ; i <= m ; ++ i ) printf("%d\n",id[i]); int l , r , las = 0; while( q --> 0 ) { scanf("%d%d",&l,&r); if( ty ) l ^= las , r ^= las; // cout << que( roo[r] , 0 , m , l ) << endl; int res = que( roo[r] , 0 , m , l ) - que( roo[l - 1] , 0 , m , l ); printf("%d\n", las = n - res); } }
|