A.如何才能穿过传送门—模拟
# include <iostream>
# include <cstring>
# include <algorithm>
# include <map>
using namespace std;
# define int long long
const int N = 100010 ;
int id[ N] ;
map< int , int > mp;
signed main ( )
{
ios:: sync_with_stdio ( 0 ) ;
cin. tie ( 0 ) ; cout. tie ( 0 ) ;
int n, m, q;
cin>> n>> m>> q;
for ( int i= 1 ; i<= m; i++ )
{
int x, y; cin>> x>> y;
mp[ x] = y; mp[ y] = x;
id[ x] = 1 ; id[ y] = 1 ;
}
for ( int i= 1 ; i<= q; i++ )
{
int x; cin>> x;
id[ x] = 2 ;
}
for ( int i= 0 ; i<= n; i++ )
{
if ( id[ i] == 1 ) i= mp[ i] ;
else if ( id[ i] == 2 )
{
cout<< "NO" << '\n' ;
return 0 ;
}
}
cout<< "YES" << '\n' ;
}
B.逃离魔爪—二维树状数组求前缀和
# include <iostream>
# include <cstring>
# include <algorithm>
using namespace std;
# define int long long
const int N = 2010 , mod = 1e9 + 7 ;
int tr1[ N] [ N] , tr2[ N] [ N] , tr3[ N] [ N] , tr4[ N] [ N] ;
int n, m, q;
int lowbit ( int x) { return x& - x; }
void tr_add ( int x, int y, int v)
{
for ( int i= x; i<= n; i+= lowbit ( i) )
for ( int j= y; j<= m; j+= lowbit ( j) )
{
tr1[ i] [ j] += v;
tr2[ i] [ j] += x* v;
tr3[ i] [ j] += y* v;
tr4[ i] [ j] += x* y* v;
}
}
void add ( int x1, int y1, int x2, int y2, int v)
{
tr_add ( x1, y1, v) ;
tr_add ( x1, y2+ 1 , - v) ;
tr_add ( x2+ 1 , y1, - v) ;
tr_add ( x2+ 1 , y2+ 1 , v) ;
}
int sum_query ( int x, int y)
{
int res= 0 ;
for ( int i= x; i; i-= lowbit ( i) )
for ( int j= y; j; j-= lowbit ( j) )
res+= ( x+ 1 ) * ( y+ 1 ) * tr1[ i] [ j] - ( y+ 1 ) * tr2[ i] [ j] - ( x+ 1 ) * tr3[ i] [ j] + tr4[ i] [ j] ;
return res;
}
int query ( int x1, int y1, int x2, int y2)
{
return sum_query ( x2, y2) - sum_query ( x2, y1- 1 ) - sum_query ( x1- 1 , y2) + sum_query ( x1- 1 , y1- 1 ) ;
}
signed main ( )
{
cin>> n>> m>> q;
while ( q-- )
{
int op, x1, x2, y1, y2;
cin>> op>> x1>> y1>> x2>> y2;
if ( op== 1 ) add ( x1, y1, x2, y2, 1 ) ;
else cout<< query ( x1, y1, x2, y2) % 2 << '\n' ;
}
}
古老的恩尼格玛机—模拟
# include <iostream>
# include <cstring>
# include <algorithm>
# include <cmath>
# include <vector>
# include <map>
# include <queue>
# include <bitset>
# include <functional>
using namespace std;
# define int long long
signed main ( )
{
ios:: sync_with_stdio ( 0 ) ;
cin. tie ( 0 ) ; cout. tie ( 0 ) ;
map< char , char > mp;
for ( int i= 0 ; i< 13 ; i++ )
{
char a, b;
cin>> a>> b;
mp[ a] = b;
mp[ b] = a;
}
int n; cin>> n;
for ( int i= 0 ; i< n; i++ )
{
string s; cin>> s;
for ( int j= 0 ; j< s. size ( ) ; j++ )
{
s[ j] = mp[ s[ j] ] ;
}
cout<< s<< " " ;
}
}
D.并不智能的卡牌 AI—签到
# include <iostream>
# include <cstring>
# include <algorithm>
# include <cmath>
# include <vector>
# include <map>
# include <queue>
# include <bitset>
# include <functional>
using namespace std;
# define int long long
signed main ( )
{
ios:: sync_with_stdio ( 0 ) ;
cin. tie ( 0 ) ; cout. tie ( 0 ) ;
int m, n;
cin>> m>> n;
if ( m== 0 )
{
cout<< 0 << endl;
return 0 ;
}
if ( n== 0 )
{
cout<< - 1 << endl;
return 0 ;
}
cout<< ( m+ n- 1 ) / n<< '\n' ;
}
G.多吃蘑菇—树形DP
# include <iostream>
# include <cstring>
# include <algorithm>
# include <vector>
# include <functional>
using namespace std;
# define int long long
const int N = 200010 , mod = 1e9 + 7 ;
int w[ N] , c[ N] ;
vector< int > g[ N] ;
int col[ N] = { 0 } ;
int f[ N] = { 0 } ;
signed main ( )
{
ios:: sync_with_stdio ( 0 ) ;
cin. tie ( 0 ) ; cout. tie ( 0 ) ;
int n; cin>> n;
for ( int i= 1 ; i<= n; i++ ) cin>> w[ i] ;
for ( int i= 1 ; i<= n; i++ ) cin>> c[ i] ;
for ( int i= 1 ; i< n; i++ )
{
int u, v; cin>> u>> v;
g[ u] . push_back ( v) ;
g[ v] . push_back ( u) ;
}
f[ 1 ] = w[ 1 ] ;
col[ c[ 1 ] ] = w[ 1 ] ;
function< void ( int , int ) > dfs = [ & ] ( int u, int fa)
{
for ( auto v : g[ u] )
{
if ( v== fa) continue ;
int pre= col[ c[ v] ] ;
int maxv= max ( col[ c[ v] ] , w[ v] ) - col[ c[ v] ] ;
f[ v] = f[ u] + maxv;
col[ c[ v] ] = max ( col[ c[ v] ] , w[ v] ) ;
dfs ( v, u) ;
col[ c[ v] ] = pre;
}
} ;
dfs ( 1 , 0 ) ;
for ( int i= 1 ; i<= n; i++ ) cout<< f[ i] << '\n' ;
}
H.差不多得了—签到
# include <iostream>
# include <cstring>
# include <algorithm>
# include <cmath>
# include <vector>
using namespace std;
# define int long long
void solve ( )
{
int n; cin>> n;
vector< int > a ( n) ;
int sum= 0 ;
for ( int i= 0 ; i< n; i++ ) cin>> a[ i] , sum+= a[ i] ;
int res= 0 ;
for ( int i= 0 ; i< n; i++ )
{
if ( a[ i] == 1 )
{
res++ ;
while ( a[ i] == 1 ) i++ ;
i-- ;
}
}
cout<< res<< endl;
}
signed main ( )
{
ios:: sync_with_stdio ( 0 ) ;
cin. tie ( 0 ) ; cout. tie ( 0 ) ;
int T; cin>> T;
while ( T-- ) solve ( ) ;
}
I.数学题真难啊—思维推理
# include <iostream>
# include <cstring>
# include <algorithm>
using namespace std;
# define int long long
const int mod = 998244353 ;
signed main ( )
{
int n; cin>> n;
int s1= 0 , s2= 0 ;
for ( int i= 0 ; i< n/ 2 ; i++ ) s1= ( s1* 10 + 1 ) % mod;
for ( int i= 0 ; i< n- n/ 2 ; i++ ) s2= ( s2* 10 + 3 ) % mod;
cout<< ( s1+ 1 ) * ( s2+ 1 ) % mod<< '\n' ;
}