×
AI > 人工智能 > 详情

弹飞绵羊LCT

发布时间:2020-05-22 发布时间:
|

  题意:

  一共n个位置,每个位置一个属性k[i],表示在i位置会被瞬间转移到i+k[i](然后又依次转移)。问从一个点开始多少次会出界。并且支持修改k[i]。

  题解:

  把i向i+k[i]连边,若i+k[i]出界就向外面的总根连边。询问就是求深度。

  把一个节点splay到根之后(前提是根是原树最开始的根),左边一定是比它浅的(且一定是它到原树根的一条链(因为access操作)),所以左儿子的size就是它的深度(根的深度为0)。

  居然以前要写一天的LCT半个小时就写好了,要不是BZOJ的编译器栈空间不够就1A了

  /**************************************************************

  Problem: 2002

  User: Lazer2001

  Language: C++

  Result: Accepted

  Time:1988 ms

  Memory:22324 kb

  ****************************************************************/

  # include 《bits/stdc++.h》

  inline int read ( ) {

  register int x, c ;

  while ( isspace ( c = getchar ( ) ) ) ;

  for ( x = -48 + c ; isdigit ( c = getchar ( ) ) ; ( x *= 10 ) += c - 48 ) ;

  return x ;

  }

  template 《 class T 》 inline T min ( const T& a, const T& b ) { return a 《 b ? a : b ; }

  template 《 class T 》 inline void swap ( T& a, T& b ) { T c ( a ) ; a = b, b = c ; }

  # define N 200010

  class LinkCutTree {

  private :

  struct node {

  int siz ;

  bool rev_flag ;

  node *ch [2], *fa ;

  inline void update ( ) {

  siz = ch [0] -》 siz + ch [1] -》 siz + 1 ;

  }

  } pool [N 《《 1], *root [N], *null ;

  int n ;

  inline void push_down ( node*& p ) {

  if ( p -》 rev_flag ) {

  swap ( p -》 ch [0], p -》 ch [1] ) ;

  if ( p -》 ch [0] != null ) p -》 ch [0] -》 rev_flag ^= 1 ;

  if ( p -》 ch [1] != null ) p -》 ch [1] -》 rev_flag ^= 1 ;

  p -》 rev_flag = 0 ;

  }

  }

  inline node* newnode ( node*& fa ) {

  static node* tp ( pool + 1 ) ;

  tp -》 siz = 1 ; tp -》 rev_flag = 0 ;

  return tp -》 fa = fa, tp -》 ch [0] = tp -》 ch [1] = null, tp ++ ;

  }

  # define isroot( p ) ( p -》 fa == null || ( p -》 fa -》 ch [0] != p && p -》 fa -》 ch [1] != p ) )

  # define isrs( p ) ( p == p -》 fa -》 ch [1] )

  inline void rotate ( node* p ) {

  if ( p == null || isroot ( p ) ) return ;

  bool d ( isrs ( p ) ) ;

  node* par = p -》 fa ;

  par -》 ch [d] = p -》 ch [! d] ;

  if ( p -》 ch [! d] != null ) p -》 ch [! d] -》 fa = par ;

  if ( ! isroot ( par ) ) par -》 fa -》 ch [isrs ( par )] = p ; // !isroot(par)

  p -》 fa = par -》 fa ;

  par -》 fa = p ;

  p -》 ch [! d] = par ;

  par -》 update ( ) ; p -》 update ( ) ;

  }

  node* st [N 《《 1] ;

  inline void splay ( node* p ) {

  if ( p == null ) return ;

  // static node* st [N 《《 1] ; static int tp ( 0 ) ; RE!!!!

  int tp ;

  st [tp = 1] = p ;

  for ( node* t = p ; ! isroot ( t ) ; t = t -》 fa ) st [++ tp] = t -》 fa ;

  while ( tp ) push_down ( st [tp --] ) ;

  while ( ! isroot ( p ) ) {

  if ( isrs ( p ) == isrs ( p -》 fa ) && ! isroot ( p -》 fa ) ) rotate ( p -》 fa ) ;

  rotate ( p ) ;

  }

  }

  # undef isrs

  # undef isroot

  inline void access ( node* p ) {

  for ( node* pre = null ; p != null ; pre = p, p = p -》 fa )

  splay ( p ), p -》 ch [1] = pre, p -》 update ( ) ;

  }

  inline void makeroot ( node* p ) {

  access ( p ) ; splay ( p ) ; p -》 rev_flag ^= 1 ;

  }

  inline void cut ( node* p, node* q ) {

  makeroot ( p ) ; access ( q ) ; splay ( q ) ;

  q -》 ch [0] = null, p -》 fa = null ;

  q -》 update ( ) ;

  }

  inline void link ( node* p, node* q ) {

  makeroot ( p ) ; p -》 fa = q ;

  }

  inline int dep ( node* p ) {

  makeroot ( root [n + 1] ) ;

  access ( p ) ;

  splay ( p ) ;

  return p -》 ch [0] -》 siz ;

  }

  public :

  LinkCutTree ( ) {

  null = pool ;

  null -》 siz = 0 ; null -》 rev_flag = 0 ;

  null -》 ch [0] = null -》 ch [1] = null -》 fa = null ;

  }

  LinkCutTree ( int n ) : n ( n ) {

  null = pool ;

  null -》 siz = 0 ; null -》 rev_flag = 0 ;

  null -》 ch [0] = null -》 ch [1] = null -》 fa = null ;

  for ( register int i = 1 ; i 《= n + 5 ; ++ i ) root [i] = newnode ( null ) ;

  }

  inline void access ( int u ) { access ( root [u] ) ; }

  inline void makeroot ( int u ) { makeroot ( root [u] ) ; }

  inline void cut ( int u, int v ) { cut ( root [u], root [v] ) ; }

  inline void link ( int u, int v ) { link ( root [u], root [v] ) ; }

  inline int dep ( int u ) { return dep ( root [u] ) ; }

  } T ;

  int a [N] ;

  # undef N

  //# define ZJC_LOCAL

  int main ( ) {

  # ifdef ZJC_LOCAL

  freopen ( “in.txt”, “r”, stdin ) ;

  freopen ( “out.txt”, “w”, stdout ) ;

  # endif

  int n ( read ( ) ) ;

  T = LinkCutTree ( n ) ;

  for ( int i = 1 ; i 《= n ; ++ i ) {

  T.link ( i, min ( i + ( a [i] = read ( ) ), n + 1 ) ) ;

  }

  for ( int m = read ( ) ; m ; -- m ) {

  int opt ( read ( ) ) ;

  if ( opt == 1 ) {

  int u ( read ( ) + 1 ) ; // + 1 !!!

  printf ( “%d\n”, T.dep ( u ) ) ;

  } else {

  int u ( read ( ) + 1 ), k ( read ( ) ) ;

  T.cut ( u, min ( u + a [u], n + 1 ) ) ;

  T.link ( u, min ( u + ( a [u] = k ), n + 1 ) ) ;

  }

  }

  }

  数据生成器(随机)

  # include 《bits/stdc++.h》

  inline int rd ( ) {

  return rand ( ) 《《 15 | rand ( ) ;

  }

  int main ( ) {

  srand ( time ( 0 ) ) ;

  freopen ( “in.txt”, “w”, stdout ) ;

  int n = 200000, m = 100000 ;

  printf ( “%d\n”, n ) ;

  for ( int i = 1 ; i 《= n ; ++ i ) printf ( “%d ”, rd ( ) % 150 + 1 ) ;

  printf ( “\n%d\n”, m ) ;

  while ( m -- ) {

  int opt ( rand ( ) % 2 + 1 ) ;

  printf ( “%d ”, opt ) ;

  if ( opt == 1 ) {

  printf ( “%d\n”, rd ( ) % n ) ;

  } else {

  printf ( “%d %d\n”, rd ( ) % n, rd ( ) % 150 + 1 ) ;

  }

  }

  }


『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
假如用上AI出狱追踪技术.江西连环杀人案疑凶曾春亮会不会重新犯罪?