TLE#11求大佬求调

P6136 【模板】普通平衡树(数据加强版)

__yabnto__ @ 2024-06-19 00:39:34

#include <iostream>

using namespace std;

const int MaxN = 1e6 + 1e5 + 10;

struct Tree {
  struct Node {
    int w, ch[2], fa, cnt, sum, x;
  } a[MaxN];

  int tot, root;

  Node operator[](int k) {
    return a[k];
  }

  void update(int k) {
    a[k].sum = a[a[k].ch[0]].sum + a[a[k].ch[1]].sum + a[k].cnt;
  }

  void rotatr(int x) {
    int y = a[x].fa, z = a[y].fa, k = a[y].ch[1] == x;
    a[z].ch[y == a[z].ch[1]] = x, a[x].fa = z;
    a[y].ch[k] = a[x].ch[k ^ 1], a[a[x].ch[k ^ 1]].fa = y;
    a[x].ch[k ^ 1] = y, a[y].fa = x;
    update(y), update(x);
  }

  void splay(int x, int to = 0) {
    for (int y, z; a[x].fa != to; y = a[x].fa, z = a[y].fa, (z != to) ? (((y == a[z].ch[1]) == (x == a[y].ch[1])) ? rotatr(x) : rotatr(y)) : void(), rotatr(x)) {
    }
    (to) || (root = x);
  }

  void At(int x) {
    int k = root;
    if (!k) return;
    for (; a[k].ch[x > a[k].x] && a[k].x != x; k = a[k].ch[x > a[k].x]) {
    }
    splay(k);
  }

  void insert(int x) {
    int k = root, y = 0;
    for (; k && a[k].x != x; y = k, k = a[k].ch[x > a[k].x]) {
    }
    (k) ? (a[k].cnt++) : (k = ++tot, a[k] = {x, {0, 0}, y, 1, 1, x}, (y) && (a[y].ch[x > a[y].x] = k));
    splay(k);
  }

  Tree() {
    root = tot = 0;
    a[0] = {0, {0, 0}, 0, 0, 0, 0};
    insert(-2e9), insert(2e9);
  }

  int kth(int x) {
    int k = root;
    for (int tx, kll; k && !(a[a[k].ch[0]].sum < x && x <= a[a[k].ch[0]].sum + a[k].cnt); kll = a[k].cnt + a[a[k].ch[0]].sum, tx = x, (kll < tx) && (x -= kll), k = a[k].ch[kll < tx]) {
    }
    return splay(k), a[k].x;
  }

  int pn(int x, bool flag) {  // 0 为前驱,1 为后继
    At(x);
    int k = root;
    if (a[k].x > x && flag || a[k].x < x && !flag) return splay(k), k;
    k = a[k].ch[flag];
    for (; a[k].ch[!flag]; k = a[k].ch[!flag]) {
    }
    return splay(k), k;
  }

  void erase(int x) {
    int pre = pn(x, 0), nxt = pn(x, 1);
    splay(pre), splay(nxt, pre);
    int k = a[nxt].ch[0];
    if (a[k].cnt > 1) {
      a[k].cnt--;
      splay(k);
    } else {
      a[nxt].ch[0] = 0;
      update(nxt), update(pre);
    }
  }
} tree;

int n, m, ans;

int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> n >> m;
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    tree.insert(x);
  }
  for (int i = 1, op, x, last = 0; i <= m; i++) {
    cin >> op >> x, x ^= last;
    if (op == 1) {
      tree.insert(x);
    } else if (op == 2) {
      tree.erase(x);
    } else if (op == 3) {
      tree.pn(x, 0);
      last = tree[tree[tree.root].ch[0]].sum + tree[tree.root].cnt;
      ans ^= last;
    } else if (op == 4) {
      last = tree.kth(x + 1);
      ans ^= last;
    } else if (op == 5) {
      last = tree[tree.pn(x, 0)].x;
      ans ^= last;
    } else {
      last = tree[tree.pn(x, 1)].x;
      ans ^= last;
    }
  }
  cout << ans << endl;
  return 0;
}

献上丑陋代码一份


by mouse_boy @ 2024-06-19 09:18:18

%%%


|