C.可疑的区间
# include <iostream>
# include <cstring>
# include <algorithm>
# include <vector>
using namespace std;
# define int long long
const int N = 10000010 ;
int s1[ N] , s2[ N] ;
signed main ( )
{
int n, len; cin>> n>> len;
for ( int i= 1 , l, r; i<= n; i++ )
{
cin>> l>> r;
s1[ max ( 1ll , l- len+ 1 ) ] ++ ;
s1[ r+ 1 ] -- ;
s2[ max ( 1ll , l- len+ 1 ) ] += i;
s2[ r+ 1 ] -= i;
}
int maxv1= 0 , maxv2= 0 , res= 1 ;
for ( int i= 1 ; i<= 5000000 ; i++ )
{
s1[ i] = s1[ i] + s1[ i- 1 ] ;
s2[ i] = s2[ i] + s2[ i- 1 ] ;
if ( s1[ i] > maxv1|| ( s1[ i] == maxv1&& s2[ i] > maxv2) )
{
maxv1= s1[ i] ;
maxv2= s2[ i] ;
res= 1 ;
}
else if ( s1[ i] == maxv1&& s2[ i] == maxv2) res++ ;
}
cout<< res<< '\n' ;
}
E.或与异或
# include <iostream>
# include <cstring>
# include <algorithm>
# include <set>
# include <functional>
using namespace std;
# define int long long
signed main ( )
{
int T; cin>> T;
while ( T-- )
{
int a, b, q; cin>> a>> b>> q;
set< pair< int , int >> S;
function< bool ( int , int ) > dfs = [ & ] ( int a, int b) -> bool
{
if ( a== q|| b== q) return true ;
if ( a> b) swap ( a, b) ;
if ( S. count ( { a, b} ) ) return false ;
S. insert ( { a, b} ) ;
if ( dfs ( a& b, a) ) return true ;
if ( dfs ( a& b, b) ) return true ;
if ( dfs ( a| b, a) ) return true ;
if ( dfs ( a| b, b) ) return true ;
if ( dfs ( a^ b, a) ) return true ;
if ( dfs ( a^ b, b) ) return true ;
return false ;
} ;
if ( dfs ( a, b) ) cout<< "YES" << '\n' ;
else cout<< "NO" << '\n' ;
}
}
F.孤独的树
# include <iostream>
# include <cstring>
# include <algorithm>
# include <vector>
# include <functional>
using namespace std;
# define int long long
const int N = 100010 ;
int w[ N] ;
int f[ N] [ 2 ] ;
vector< int > g[ N] ;
vector< int > rt[ N] ;
bool vis[ N] ;
int primes[ N] , idx= 0 , minp[ N] ;
void prime ( )
{
for ( int i= 2 ; i< N; i++ )
{
if ( ! minp[ i] ) primes[ idx++ ] = i, minp[ i] = i;
for ( int j= 0 ; i* primes[ j] < N; j++ )
{
minp[ primes[ j] * i] = primes[ j] ;
if ( i% primes[ j] == 0 ) break ;
}
}
}
signed main ( )
{
prime ( ) ;
int n; cin>> n;
for ( int i= 1 ; i<= n; i++ )
{
cin>> w[ i] ;
int x= w[ i] ;
while ( x> 1 )
{
rt[ minp[ x] ] . push_back ( i) ;
x/= minp[ x] ;
}
}
for ( int i= 1 , a, b; i<= n; i++ )
{
cin>> a>> b;
g[ a] . push_back ( b) ;
g[ b] . push_back ( a) ;
}
function< void ( int , int , int ) > dfs = [ & ] ( int u, int fa, int p)
{
int x= w[ u] , cnt= 0 ;
vis[ u] = true ;
while ( x% p== 0 ) cnt++ , x/= p;
f[ u] [ 0 ] = cnt, f[ u] [ 1 ] = 0 ;
for ( auto v : g[ u] )
{
if ( w[ v] % p|| v== fa|| vis[ v] ) continue ;
dfs ( v, u, p) ;
f[ u] [ 0 ] += min ( f[ v] [ 0 ] , f[ v] [ 1 ] ) ;
f[ u] [ 1 ] += f[ v] [ 0 ] ;
}
} ;
int res= 0 ;
for ( int i= 0 ; i< idx; i++ )
{
for ( auto u : rt[ primes[ i] ] ) vis[ u] = 0 , f[ u] [ 0 ] = f[ u] [ 1 ] = 0 ;
for ( auto u : rt[ primes[ i] ] ) if ( ! vis[ u] ) dfs ( u, 0 , primes[ i] ) , res+= min ( f[ u] [ 0 ] , f[ u] [ 1 ] ) ;
}
cout<< res<< '\n' ;
}