“玲珑杯”算法比赛 Round #14By:wxh010910
Start Time:2017-05-13 16:00:00 End Time:2017-05-13 18:30:00 Refresh Time:2017-05-20 09:51:24 Public
玲珑OJ比赛提交地址:http://www.ifrog.cc/acm/contest/1016
Round#14题解:http://www.ifrog.cc/acm/solution/19
A -- No Game No Life
Time Limit:1s Memory Limit:256MByte
Submissions:473Solved:226
DESCRIPTION
INPUT
第一行两个数n,m第二行n个数表示a序列后面有m行,
每行格式为1 x y或者2 x分别表示把所有值为x的数变成y或者询问序列上x位置的数值
OUTPUT
对于每个2操作,即询问,输出一个数表示答案
SAMPLE INPUT
5 55 5 5 5 52 31 5 22 31 8 32 1
SAMPLE OUTPUT
522
HINT
n,m <= 1000, a[i] <= 1000000000你作为妹妹当然要AC掉这题啦
SOLUTION
“玲珑杯”ACM比赛 Round #14
标程:
#include
#define MAXN 100010
using namespace std;
int n , m , a[ MAXN ];
map < int , int > wocaonimalegebia;
inline int read()
{
register int x = 0 , ch = getchar();
while( !isdigit( ch ) ) ch = getchar();
while( isdigit( ch ) ) x = x * 10 + ch - '0' , ch = getchar();
return x;
}
int main()
{
n = read() , m = read();
for( register int i = 1 ; i <= n ; i++ ) a[i] = read() , wocaonimalegebia[ a[i] ] = a[i];
while( m-- )
if( read() == 1 )
{
int x = read();
wocaonimalegebia[x] = read();
}
else printf( "%d\n" , wocaonimalegebia[ a[ read() ] ] );
return 0;
}
Time Limit:1s Memory Limit:256MByte
Submissions:368Solved:52
#include
#define ratio 4
#define MAXN 1000010
#define merge( a , b ) new_Node( a -> size + b -> size , b -> value , a , b )
#define new_Node( s , v , a , b ) ( & ( * st[ cnt++ ] = Node( s , v , a , b ) ) )
#define update( cur ) if( cur -> left -> size ) cur -> size = cur -> left -> size + cur -> right -> size , cur -> value = cur -> right -> value;
using namespace std;
struct Node
{
int size , value;
Node * left , * right;
Node( int s , int v , Node * a , Node * b ) : size( s ) , value( v ) , left( a ) , right( b ) {}
Node() {}
} * root , * null , * st[300000] , t[300000];
int n , m , x , cnt;
int find( int x , Node * cur )
{
if( !cur -> left -> size ) return cur -> value;
return x > cur -> left -> size ? find( x - cur -> left -> size , cur -> right ) : find( x , cur -> left );
}
int zhentamashabibuyaoxiajibaqibianlaingminghaobuhaoa( int x , Node * cur )
{
if( !cur -> left -> size ) return ( x > cur -> value ) * cur -> size;
return x > cur -> left -> value ? zhentamashabibuyaoxiajibaqibianlaingminghaobuhaoa( x , cur -> right ) + cur -> left -> size : zhentamashabibuyaoxiajibaqibianlaingminghaobuhaoa( x , cur -> left );
}
void split( int x , Node * cur )
{
if( x > cur -> left -> size ) split( x - cur -> left -> size , cur -> right ) , cur -> left = merge( cur -> left , cur -> right -> left ) , st[ --cnt ] = cur -> right , cur -> right = cur -> right -> right;
else if( x < cur -> left -> size ) split( x , cur -> left ) , cur -> right = merge( cur -> left -> right , cur -> right ) , st[ --cnt ] = cur -> left , cur -> left = cur -> left -> left;
}
void insert( int x , Node * cur )
{
if( !cur -> left -> size )
if( cur -> value < x ) cur -> left = new_Node( cur -> size , cur -> value , null , null ) , cur -> right = new_Node( 1 , x , null , null );
else cur -> left = new_Node( 1 , x , null , null ) , cur -> right = new_Node( cur -> size , cur -> value , null , null );
else insert( x , x > cur -> left -> value ? cur -> right : cur -> left );
update( cur );
}
void dispose( Node * cur )
{
if( cur -> left -> size ) st[ --cnt ] = cur -> left , st[ --cnt ] = cur -> right , dispose( cur -> left ) , dispose( cur -> right );
}
inline void low( int x )
{
Node * cur = root;
int s = zhentamashabibuyaoxiajibaqibianlaingminghaobuhaoa( x + 1 , cur );
if( s == 0 );
else if( s == cur -> size )
dispose( cur ) , * cur = Node( s , x , null , null );
else
split( s , cur ) , dispose( cur -> left ) , * cur -> left = Node( s , x , null , null );
}
inline void up( int x )
{
Node * cur = root;
int s = zhentamashabibuyaoxiajibaqibianlaingminghaobuhaoa( x + 1 , cur );
if( s == 0 )
dispose( cur ) , * cur = Node( cur -> size , x , null , null );
else if( s == cur -> size );
else
split( s , cur ) , dispose( cur -> right ) , * cur -> right = Node( cur -> size - s , x , null , null );
}
struct io
{
char ibuf[1 << 23] , * s , obuf[1 << 22] , * t;
int a[24];
io() : t( obuf )
{
fread( s = ibuf , 1 , 1 << 23 , stdin );
}
~io()
{
fwrite( obuf , 1 , t - obuf , stdout );
}
inline int read()
{
register int u = 0;
while( * s < 48 ) s++;
while( * s > 32 )
u = u * 10 + * s++ - 48;
return u;
}
template< class T >
inline void print( register T u )
{
static int * q = a;
if( !u ) * t++ = 48;
else
{
if( u < 0 )
* t++ = 45 , u *= -1;
while( u ) * q++ = u % 10 + 48 , u /= 10;
while( q != a )
* t++ = * --q;
}
* t++ = '\n';
}
} ip;
#define read ip.read
#define print ip.print
int main()
{
m = read();
for( register int i = 0 ; i < 300000 ; i++ ) st[i] = & t[i];
null = new_Node( 0 , 0 , 0 , 0 );
while( m-- )
{
int opt = read();
if( opt == 1 )
if( root ) insert( read() , root );
else root = new_Node( 1 , read() , null , null );
else if( opt == 2 ) low( read() );
else if( opt == 3 ) up( read() );
else if( opt == 4 ) print( find( read() , root ) );
else print( zhentamashabibuyaoxiajibaqibianlaingminghaobuhaoa( read() , root ) );
}
return 0;
}
Time Limit:3s Memory Limit:256MByte
Submissions:230Solved:20
标程:
#define OPENSTACK
#include
using namespace std;
#define MAXN 500010
using namespace std;
int n , h , size[ MAXN ] , fa[ MAXN ] , dep[ MAXN ] , son[ MAXN ] , top[ MAXN ] , l[ MAXN ] , r[ MAXN ] , tag[ MAXN ] , tot , p[10000000] , cnt;
vector < int > linker[ MAXN ] , wocaonimabi[ MAXN ];
long long ans;
inline int read()
{
int x = 0 , ch = getchar();
while( !isdigit( ch ) ) ch = getchar();
while( isdigit( ch ) ) x = x * 10 + ch - '0' , ch = getchar();
return x;
}
#define cur linker[x][i]
void dfs1( int x )
{
size[x] = 1;
for( int i = 0 ; i < linker[x].size() ; i++ )
if( cur != fa[x] )
{
fa[ cur ] = x , dep[ cur ] = dep[x] + 1;
dfs1( cur ) , size[x] += size[ cur ];
if( size[ cur ] > size[ son[x] ] ) son[x] = cur;
}
}
void dfs2(int x, int t) {
top[x] = t;
l[x] = ++tot;
if (son[x] ) {
dfs2( son[x] , t );
}
for( int i = 0 ; i < linker[x].size() ; i++ )
if( cur != fa[x] && cur != son[x] )
dfs2( cur , cur );
}
inline void modify( int l , int r )
{
tag[l]++ , tag[r + 1]--;
p[ ++cnt ] = l , p[ ++cnt ] = r + 1;
}
void modify( int x )
{
while( x )
{
modify( l[ top[x] ] , l[x] );
x = fa[ top[x] ];
}
}
inline void addedge(int x, int y) {
linker[x].push_back( y );
}
int main()
{
#ifdef OPENSTACK
int size = 128 << 20; // 64MB
char *tangtangtangtanglaoshia = (char*)malloc(size) + size;
#if (defined _WIN64) or (defined __unix)
__asm__("movq %0, %%rsp\n" :: "r"(tangtangtangtanglaoshia));
#else
__asm__("movl %0, %%esp\n" :: "r"(tangtangtangtanglaoshia));
#endif
#endif
n = read() , h = read();
for( int i = 2 ; i <= n ; i++ ) addedge( read() , i );
for( int i = 1 ; i <= n ; i++ ) wocaonimabi[ read() ].push_back( i );
dfs1( 1 );
dfs2( 1 , 1 );
for( int v = 1 ; v <= h ; v++ )
{
int t = read();
for(int i = 0 ; i < wocaonimabi[v].size() ; i++ )
modify( wocaonimabi[v][i] );
p[ ++cnt ] = 1 , p[ ++cnt ] = n + 1;
sort( p + 1 , p + cnt + 1 );
cnt = unique( p + 1 , p + cnt + 1 ) - p;
for(int i = 1 , last = 1 , now = 0 ; i <= cnt ; i++ )
{
if( now >= t ) ans += p[i] - last;
now += tag[ p[i] ] , tag[ p[i] ] = 0;
last = p[i];
}
cnt = 0;
}
cout << ans << endl;
#ifdef OPENSTACK
exit(0);
#else
return 0;
#endif
}
Time Limit:1s Memory Limit:256MByte
Submissions:36Solved:4
标程:
#include
#define MAXN 100010
using namespace std;
int n , m , block , a[ MAXN ], belong[ MAXN ] , ans[ MAXN ] , cnt[ MAXN ] , last[ MAXN ];
struct ask
{
int v , l , r , pos;
} q[ MAXN ];
vector < ask > linker[MAXN ];
inline bool cmp( const ask& a , const ask & b )
{
return belong[ a.l ] ^ belong[ b.l ] ? belong[ a.l ] b.r;
}
inline int read()
{
register int x = 0 , ch = getchar();
while( !isdigit( ch ) ) ch = getchar();
while( isdigit( ch ) ) x = x * 10 + ch - '0' , ch =getchar();
return x;
}
int main()
{
n = read() , m = read();
block = n / sqrt( m / 3 );
for( register int i = 1 ; i <= n ; i++ ) belong[i] = ( i -1 ) / block , a[i] = read();
for( register int i = 1 ; i <= m ; i++ )
q[i].l = read() , q[i].r = read() , q[i].v = read() ,q[i].pos = i;
sort( q + 1 , q + m + 1 , cmp );
for( int i = 1 , l = 1 , r = 0 ; i <= m ; i++ )
{
while( l > q[i].l ) cnt[ a[ --l ] ]++;
while( r < q[i].r ) cnt[ a[ ++r ] ]++;
while( l < q[i].l ) cnt[ a[ l++ ] ]--;
while( r > q[i].r ) cnt[ a[ r-- ] ]--;
if( q[i].v > 300 )
for( register int j = 1 ; j * q[i].v <=100000 && !ans[ q[i].pos ] ; j++ )
if( cnt[j] && cnt[ j * q[i].v] )
ans[ q[i].pos ] = 1;
}
for( int x = 1 ; x <= 300 ; x++ )
{
for( registerint i = 1 ; i <= n ; i++ ) linker[i].clear();
for( register int i = 1 ; i <= m ; i++ )
if( q[i].v == x )
linker[ q[i].l ].push_back( q[i] );
memset( last , 0x3f , sizeof( last ) );
for( register int i = n , j = n + 1 ; i ; i-- )
{
if( a[i] * 1ll * x <= 100000 ) j = min( j ,last[ a[i] * 1ll * x ] );
if( a[i] % x == 0 ) j = min( j , last[ a[i] /x ] );
for( register int k = 0 ; k = j )
ans[ linker[i][k].pos ] = 1;
last[ a[i] ] = i;
}
}
for( register int i = 1 ; i <= m ; i++ )
puts( ans[i] ? "flip" : "flap" );
return 0;
}
Time Limit:2s Memory Limit:256MByte
Submissions:69Solved:3
标程:
#include
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define fill( x, y ) memset( x, y, sizeof x )
#define copy( x, y ) memcpy( x, y, sizeof x )
using namespace std;
typedef long long LL;
typedef pair < int, int > pa;
inline int read()
{
int sc = 0, f = 1; char ch = getchar();
while( ch < '0' || ch > '9' ) { if( ch == '-' ) f = -1; ch = getchar(); }
while( ch >= '0' && ch <= '9' ) sc = sc * 10 + ch - '0', ch = getchar();
return sc * f;
}
const int mod = 998244353;
const int MAXN = 262145;
inline int qpow(int x, int y, int mod) { int ret = 1; for( ; y ; y >>= 1, x = 1LL * x * x % mod ) if( y & 1 ) ret = 1LL * ret * x % mod; return ret; }
namespace NTT
{
int R[MAXN], n, L, inv;
inline void getR() { for( int i = 0 ; i < n ; i++ ) R[ i ] = ( R[ i >> 1 ] >> 1 ) | ( ( i & 1 ) << L - 1 ); inv = qpow( n, mod - 2, mod ); }
inline void NTT(int *a, int f)
{
for( int i = 0 ; i < n ; i++ ) if( i < R[ i ] ) swap( a[ i ], a[ R[ i ] ] );
for( int i = 1, wn = qpow( 3, mod - 1 + f * ( mod - 1 ) / ( i << 1 ), mod ) ; i < n ; i <<= 1, wn = qpow( 3, mod - 1 + f * ( mod - 1 ) / ( i << 1 ), mod ) )
for( int j = 0, w = 1; j < n ; j += i << 1, w = 1 )
for( int k = 0 ; k < i ; k++, w = 1LL * w * wn % mod )
{
int x = a[ j + k ], y = 1LL * w * a[ j + k + i ] % mod;
a[ j + k ] = ( x + y ) % mod; a[ j + k + i ] = ( x - y + mod ) % mod;
}
if( f < 0 ) for( int i = 0 ; i < n ; i++ ) a[ i ] = 1LL * a[ i ] * inv % mod;
}
inline void mul(int *ret, int *a, int *b)
{
NTT( a, 1 ); NTT( b, 1 );
for( int i = 0 ; i < n ; i++ ) ret[ i ] = 1LL * a[ i ] * b[ i ] % mod;
NTT( ret, -1 );
}
}
inline void inc(int &x, int y) { x += y; if( x >= mod ) x -= mod; }
inline void dec(int &x, int y) { x -= y; if( x < 0 ) x += mod; }
int n, f[MAXN], g[MAXN], fac[MAXN], inv[MAXN], fnv[MAXN], p[MAXN], p_cnt, phi[MAXN], a[MAXN], b[MAXN], c[MAXN];
inline int C(int x, int y) { return 1LL * fac[ x ] * fnv[ y ] % mod * fnv[ x - y ] % mod; }
inline void init()
{
fac[ 0 ] = inv[ 0 ] = inv[ 1 ] = fnv[ 0 ] = phi[ 1 ] = f[ 0 ] = 1;
for( int i = 1 ; i <= ( n << 1 ) ; i++ ) fac[ i ] = 1LL * fac[ i - 1 ] * i % mod;
for( int i = 2 ; i <= ( n << 1 ) ; i++ ) inv[ i ] = 1LL * ( mod - mod / i ) * inv[ mod % i ] % mod;
for( int i = 1 ; i <= ( n << 1 ) ; i++ ) fnv[ i ] = 1LL * fnv[ i - 1 ] * inv[ i ] % mod;
for( int i = 2 ; i <= n ; i++ )
{
if( !phi[ i ] ) phi[ p[ ++p_cnt ] = i ] = i - 1;
for( int j = 1 ; i * p[ j ] <= n ; j++ )
{
if( i % p[ j ] == 0 )
{
phi[ i * p[ j ] ] = phi[ i ] * p[ j ];
break;
}
phi[ i * p[ j ] ] = phi[ i ] * ( p[ j ] - 1 );
}
}
for( int i = 1 ; i <= n ; i++ )
{
int t = C( i << 1, i );
for( int j = i, k = 1 ; j <= n ; j += i, k++ )
if( i & 1 ) dec( g[ j ], 1LL * phi[ k ] * t % mod );
else inc( g[ j ], 1LL * phi[ k ] * t % mod );
if( i & 1 ) g[ i ] = ( mod - g[ i ] ) % mod;
g[ i ] = 1LL * g[ i ] * inv[ i << 1 ] % mod;
}
}
inline void solve(int l, int r)
{
if( l == r ) { f[ l ] = 1LL * f[ l ] * inv[ l ] % mod; return ; }
int mid = l + r >> 1;
solve( l, mid );
for( NTT::n = 1, NTT::L = 0 ; NTT::n <= r - l + 1 ; NTT::n <<= 1 ) NTT::L++;
NTT::getR();
for( int i = 0 ; i < NTT::n ; i++ )
a[ i ] = i + l <= mid ? f[ i + l ] : 0, b[ i ] = ( i + l + 1 ) <= r ? g[ i + 1 ] : 0;
NTT::mul( c, a, b );
for( int i = mid + 1 ; i <= r ; i++ )
inc( f[ i ], c[ i - l - 1 ] );
solve( mid + 1, r );
}
int main()
{
n = read();
init();
solve( 0, n );
return printf( "%d\n", f[ n ] ), 0;
}