可持久化 01Trie
点击查看代码
struct Persistent_01Trie{
int R[ N ] , T[ N ][ 2 ] , C;
void Insert( int p , int q , ll x ){
for( int i = lgN ; i >= 0 ; i -- ){
int v = ( x >> i ) & 1;
T[ p ][ v ] = ++ C , T[ p ][ v ^ 1 ] = T[ q ][ v ^ 1 ];
p = T[ p ][ v ] , q = T[ q ][ v ];
}
}
ll Get_Xor( int p , ll x ){
ll res = 0;
p = R[ p ];
for( int i = lgN ; i >= 0 ; i -- ){
int v = ( x >> i ) & 1;
if( T[ p ][ v ^ 1 ] ) res += ( 1 << i ) , p = T[ p ][ v ^ 1 ];
else p = T[ p ][ v ];
}
return res;
}
void Build(){
R[ 0 ] = ++ C , Insert( R[ 0 ] , 0 , 0 );
for( int i = 1 ; i <= n ; i ++ ) R[ i ] = ++ C , Insert( R[ i ] , R[ i - 1 ] , a[ i ] );
}
}G;