全球最实用的IT互联网信息网站!

AI人工智能P2P分享&下载搜索网页发布信息网站地图

当前位置:诺佳网 > AI人工智能 > AI通用技术 >

弹飞绵羊LCT

时间:2017-11-27 15:30

人气:

作者:admin

标签: lct  C语言 

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

  题意:

  一共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 ) ;

  }

  }

温馨提示:以上内容整理于网络,仅供参考,如果对您有帮助,留下您的阅读感言吧!
相关阅读
本类排行
相关标签
本类推荐

CPU | 内存 | 硬盘 | 显卡 | 显示器 | 主板 | 电源 | 键鼠 | 网站地图

Copyright © 2025-2035 诺佳网 版权所有 备案号:赣ICP备2025066733号
本站资料均来源互联网收集整理,作品版权归作者所有,如果侵犯了您的版权,请跟我们联系。

关注微信