[HNOI2002]营业额统计

大早晨被数据坑,测试数据结尾有丢0的情况;

 if(scanf("%d",&x) == EOF) x = 0; 加上这句就能过。

维护有序序列,查找小于x的最大值,大于x的最小值。

虚拟结点的v要赋值为-INF, null结点的v赋值为INF;

splay只需要维护 结点总数s  信息,不需要pushdown()操作。

int find_low(int x) {//小于x的最大值
  Node *p = ss.root, *ret = null;
  while ( p != null ) {
    if ( p->v <= x ) ret = p, p = p->ch[1];
    else p = p-> ch[0];
  }
  return ret->v;
}
int find_up(int x) {//大于x的最小值
  Node *p = ss.root, *ret = null;
  while ( p != null ) {
    if ( p->v >= x ) ret = p, p = p->ch[0];
    else p = p-> ch[1];
  }
  //if ( ret->v <= x ) return INF;
  return ret->v;
}

另外,如果当前波动值为0,那么不需要把x插入,这样可以节省很多时间- -

原文地址:https://www.cnblogs.com/Accoral/p/3149247.html