FhqTreap全WA...死掉了...

P3391 【模板】文艺平衡树

Gypsophila @ 2018-12-23 17:47:21

自己测了几个小样例没啥毛病,交上去就是过不了

有神仙帮忙挑个错吗?

#include <bits/stdc++.h>
using namespace std;
const int N = 100100; 
int n, m, cnt; 
struct node {
  int tag, siz, d, rnd; node *ch[2];
  inline void upd() {
    int ret = 1;
    if(ch[0]) ret += ch[0]->siz; 
    if(ch[1]) ret += ch[1]->siz; 
    siz = ret; 
  } 
  inline void pushd() {
    if(!tag) return ; swap(ch[0], ch[1]); 
    if(ch[0]) ch[0]->tag ^= 1; 
    if(ch[1]) ch[1]->tag ^= 1; 
    tag = 0; 
  }
} pool[N]; 
inline int siz(node *r) {
  if(r) return r->siz; return 0; 
}
struct FhqTreap {
  node *root; 
  inline node* merge(node *p, node *q) {
    if(!p) return q; if(!q) return p; 
    if(p->rnd < q->rnd) { p->pushd(); p->ch[1] = merge(p->ch[1], q); p->upd(); return p; }
    else { q->pushd(); q->ch[0] = merge(q->ch[0], p); q->upd(); return q; }
  }
  inline void split(node *r, int k, node *&p, node *&q) {
    if(!r) { p = q = NULL; return ; } r->pushd(); 
    if(siz(r->ch[0]) < k) p = r, split(r->ch[1], k - siz(r->ch[0]) - 1, r->ch[1], q);
    else q = r, split(r->ch[0], k, p, r->ch[0]); 
    r->upd();  
  }
  inline void output(node *r) {
    r->pushd(); 
    if(r->ch[0]) output(r->ch[0]); 
    printf("%d ", r->d);
    if(r->ch[1]) output(r->ch[1]);  
  }
} T; 
int main() {
  scanf("%d %d", &n, &m);
  T.root = &pool[0]; T.root->siz = 1; T.root->d = 1; 
  for(int i = 2; i <= n; i++) {
    node *p = &pool[++cnt]; 
    p->siz = 1, p->rnd = rand(); 
    p->d = i, p->ch[0] = p->ch[1] = NULL; 
    T.root = T.merge(T.root, p);
  }
  for(int i = 1; i <= m; i++) {
    int l, r; scanf("%d %d", &l, &r); 
    node *p, *q, *s; 
    T.split(T.root, l - 1, p, q); 
    T.split(q, r - l + 1, q, s); 
    q->tag ^= 1; 
    T.merge(p, T.merge(q, s)); 
  } T.output(T.root); 
  return 0; 
}

by Everlasting_Snow @ 2018-12-23 17:50:23

您太巨了(蒟蒻指针不会


by Gypsophila @ 2018-12-23 17:51:26

@fengsongquan 您太巨了(蒟蒻数组不会


by hyfhaha @ 2018-12-23 18:05:24

IST(被管理认为是复杂度没有保证的退化版fhq treap)党路过


by 良月澪二 @ 2018-12-23 18:09:35

为什么要写p,q这么难认的变量


by xenonex @ 2018-12-23 18:13:50

merge写跪了


by GKxx @ 2018-12-23 18:25:27

merge里面

else { q->pushd(); q->ch[0] = merge(q->ch[0], p); q->upd(); return q; }

应该改为

else { q->pushd(); q->ch[0] = merge(p, q->ch[0]); q->upd(); return q; }

by 初墨 @ 2018-12-23 18:25:59

@fengsongquan 您太巨了(蒟蒻输入不会


by Rbu_nas @ 2018-12-23 18:31:51

压行真难受


by Gypsophila @ 2018-12-23 18:33:04

@GKxx 感谢!


by Gypsophila @ 2018-12-23 18:33:30

@Rem° 我这个还没有特别变态吧


| 下一页