luogu P3391 【模板】文艺平衡树(Splay)

嘟嘟嘟


突然觉得splay挺有意思的……


这道题只有一个任务:区间翻转。
首先应该知道的是,splay和线段树一样,都可以打标记,然后走到每一个节点之前先下传。
那怎么打标记呢?还应该有“区间”的思想。
对于区间\([L, R]\),想办法把这个区间所在的子树提取出来,然后打个标记即可。
那怎么提取呢?其实也不难。只要找出\(L\)的前驱\(a = L - 1\)\(R\)的后继\(b = R + 1\),然后把\(a\)旋到根,再把\(b\)旋到根的右子节点,这样\(b\)的左子树就是当前区间了。
但是找前驱和后继只能像bst那么找,因为这棵splay的key值是下标,而下标并没有存起来,而是通过子树大小体现的。所以上述找前驱和后继操作相当于查询第\(k\)大。因为事先加了\(-INF\)\(INF\)防止越界,所以找前驱就是查询第\(L\)大的,后继就是第\(R + 2\)大的。

int getRank(int k)
{
  int now = root;
  while(1)
    {
      pushdown(now);
      if(t[t[now].ch[0]].siz >= k) now = t[now].ch[0];
      else if(t[t[now].ch[0]].siz + 1 == k) return now;
      else k -= t[t[now].ch[0]].siz + 1, now = t[now].ch[1];
    }
}
void update(int L, int R)
{
  int a = getRank(L), b = getRank(R + 2); //pre(L), nxt(R)
  splay(a, 0); splay(b, a); //现在b的左子树就是当前区间
  pushdown(root); pushdown(t[root].ch[1]);
  int now = t[t[root].ch[1]].ch[0];
  t[now].lzy ^= 1;
}



还有一件事就是建树,虽然可以像这道题一样每一次插入一个数,不过有更可爱的方法。
仿照线段树的建树方法,但有一个显著的区别是线段树的每一个节点表示一个区间,而splay就表示一个点,所以递归的时候把当前区间的\(a[mid]\)作为线段树该节点的权值,然后到\([L, mid - 1]\)\([mid + 1, R]\)中建立左右子树。

int build(int L, int R, int f)
{
  if(L > R) return 0;
  int mid = (L + R) >> 1, now = ++ncnt;
  t[now].val = a[mid]; t[now].fa = f;
  t[now].ch[0] = build(L, mid - 1, now);
  t[now].ch[1] = build(mid + 1, R, now);
  pushup(now);
  return now;
}



最后一件事就是输出。利用splay自身的性质,中序遍历就是答案。


完整代码

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<stack>
#include<queue>
using namespace std;
#define enter puts("") 
#define space putchar(' ')
#define Mem(a, x) memset(a, x, sizeof(a))
#define rg register
typedef long long ll;
typedef double db;
const int INF = 0x3f3f3f3f;
const db eps = 1e-8;
const int maxn = 1e5 + 5;
inline ll read()
{
  ll ans = 0;
  char ch = getchar(), last = ' ';
  while(!isdigit(ch)) last = ch, ch = getchar();
  while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
  if(last == '-') ans = -ans;
  return ans;
}
inline void write(ll x)
{
  if(x < 0) x = -x, putchar('-');
  if(x >= 10) write(x / 10);
  putchar(x % 10 + '0');
}

int n, m, a[maxn];
struct Tree
{
  int ch[2], fa;
  int val, siz, lzy;
}t[maxn];
int root, ncnt = 0;
void _PrintTr(int now)
{
  if(!now) return;
  printf("nd:%d val:%d ls:%d rs:%d\n", now, t[now].val, t[t[now].ch[0]].val, t[t[now].ch[1]].val);
  _PrintTr(t[now].ch[0]); _PrintTr(t[now].ch[1]);
}
void pushdown(int now)
{
  if(now && t[now].lzy)
    {
      t[t[now].ch[0]].lzy ^= 1; t[t[now].ch[1]].lzy ^= 1;
      swap(t[now].ch[0], t[now].ch[1]);
      t[now].lzy = 0;
    }
}
void pushup(int now)
{
  t[now].siz = t[t[now].ch[0]].siz + t[t[now].ch[1]].siz + 1;
}
void rotate(int x)
{
  int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
  t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
  t[y].ch[k] = t[x].ch[k ^ 1]; t[t[y].ch[k]].fa = y;
  t[x].ch[k ^ 1] = y; t[y].fa = x;
  pushup(y); pushup(x);
}
void splay(int x, int s)  //旋转的时候不用pushdown.(因为是自底向上的)
{
  while(t[x].fa != s)
    {
      int y = t[x].fa, z = t[y].fa;
      if(z != s)
    {
      if((t[z].ch[0] == y) ^ (t[y].ch[0] == x)) rotate(x);
      else rotate(y);
    }
      rotate(x);
    }
  if(s == 0) root = x;
}
int build(int L, int R, int f)
{
  if(L > R) return 0;
  int mid = (L + R) >> 1, now = ++ncnt;
  t[now].val = a[mid]; t[now].fa = f;
  t[now].ch[0] = build(L, mid - 1, now);
  t[now].ch[1] = build(mid + 1, R, now);
  pushup(now);
  return now;
}
int getRank(int k)
{
  int now = root;
  while(1)
    {
      pushdown(now);
      if(t[t[now].ch[0]].siz >= k) now = t[now].ch[0];
      else if(t[t[now].ch[0]].siz + 1 == k) return now;
      else k -= t[t[now].ch[0]].siz + 1, now = t[now].ch[1];
    }
}
void update(int L, int R)
{
  int a = getRank(L), b = getRank(R + 2); //pre(L), nxt(R)
  splay(a, 0); splay(b, a); //现在b的左子树就是当前区间
  pushdown(root); pushdown(t[root].ch[1]);
  int now = t[t[root].ch[1]].ch[0];
  t[now].lzy ^= 1;
}
void print(int now)
{
  pushdown(now);
  if(t[now].ch[0]) print(t[now].ch[0]);
  if(t[now].val != INF && t[now].val != -INF) write(t[now].val), space;
  if(t[now].ch[1]) print(t[now].ch[1]);
}

int main()
{
  n = read(); m = read();
  a[1] = -INF; a[n + 2] = INF;
  for(int i = 1; i <= n; ++i) a[i + 1] = i;
  root = build(1, n + 2, 0);
  //_PrintTr(root);
  for(int i = 1, L, R; i <= m; ++i) L = read(), R = read(), update(L, R);
  print(root), enter;
  return 0;
}

猜你喜欢

转载自www.cnblogs.com/mrclr/p/10060582.html