一个只供删除的双向链表,为了简单不再引入head节点,而且也不进行next的套娃操作。空间使用略微多了一些,但是无伤大雅。

struct LinkedList {
static const int MAXN = 100000;
int n, prev[MAXN + 5], next[MAXN + 5]; void Init(int _n) {
n = _n;
for(int i = 1; i <= n; ++i) {
prev[i] = i - 1;
next[i] = i + 1;
}
prev[1] = -1;
next[n] = -1;
} void Remove(int x) {
prev[next[x]] = prev[x];
next[prev[x]] = next[x];
}
};

正常的链表是单向链表,删除操作是删除当前节点的下一个节点,没有必要。全部写双向的。b事多还可以写个垃圾回收。

struct LinkedList {
static const int MAXN = 100000;
int n, prev[MAXN + 5], next[MAXN + 5];
static const int head = 0, tail = MAXN + 1; void Init() {
n = 0;
prev[head] = -1;
next[head] = tail;
prev[tail] = head;
next[tail] = -1;
} int Insert(int x) {
++n;
prev[next[x]] = n;
next[n] = next[x];
prev[n] = x;
next[x] = n;
return n;
} int Append() {
return Insert(prev[tail]);
} void Remove(int x) {
prev[next[x]] = prev[x];
next[prev[x]] = next[x];
}
};

并查集实现的伪链表。用并查集实现的伪链表虽然删除不是O(1)的,但是不会怕访问到被删除的节点。

struct PrevLinkedList {
int n;
bool vis[100005];
int prev[100005]; void Init(int _n) {
n = _n;
for(int i = 1; i <= n; ++i) {
vis[i] = 0;
prev[i] = i - 1;
}
} int Prev(int x) {
int r = prev[x];
while(vis[r])
r = prev[r];
int t;
while(prev[x] != r) {
t = prev[x];
prev[x] = r;
x = t;
}
return r;
} void Remove(int x) {
vis[x] = 1;
}
};

要使用next的话就再搞另一个伪链表就可以了。和只使用lower_bound,upper_bound,prev,next的平衡树相比,伪链表的速度卓越,缺点是不能插入,也不能维护第k大/前k项和。

最新文章

  1. mysql数据库的安装与使用
  2. 动态SQL语句之sp_executesql的使用
  3. PingUtil in Android
  4. 样例20-汽车SHOW
  5. swift webView 提出这样的要求你能忍吗?
  6. jquery checkbox checked
  7. 禁止选择文本和禁用右键 v2.0
  8. jquery1.9学习笔记 之选择器(基本元素四)
  9. 将String类型的数字字符转换成int
  10. OJ python答题结果&quot;返回非零&quot;
  11. iOS9以后 GDataXMLNode修改方式
  12. POJ1200(hash)
  13. mysql获得自增的下条id的值
  14. iBrand 产品工具包:Laravel Database Logger
  15. Linux下调试.Net core(1):lldb的安装
  16. Possible causes are invalid address of the remote server or browser start-up failure.
  17. HAOI2017 新型城市化 二分图的最大独立集+最大流+强连通缩点
  18. Java内存模型-volatile的内存语义
  19. 多线程二(GCD)代码笔记
  20. robot framework学习笔记2

热门文章

  1. vue.js相关教程
  2. Tomcat - 控制台乱码
  3. 某位前辈的Image识图,,有点意思,先留存
  4. There is no getter for property named &#39;PRODUCT_ID&#39; in &#39;class java.lang.String&#39;
  5. keil5工程移植到IAR工程
  6. Analysis of Autherntication Protocol with Scyther :Case Study ---补充整理
  7. [ipsec][strongswan] 使用VTI配置基于路由的ipsec
  8. CentOS7编译安装httpd-2.4.41 php7.3
  9. HDFS重启集群导致数据损坏,使用fsck命令修复过程
  10. 用python实现多线程爬取影视网站全部视频方法【笔记】