• Treap模板


    #include <cstdio>
    #include <iostream>
    using namespace std;
    
    const int infinity = 0x3f3f3f3f;
    int lc[200010], rc[200010], sz[200010], w[200010], h[200010];
    int n, tot, rt;
    
    void pushup(int x) { sz[x] = sz[lc[x]] + sz[rc[x]] + 1; }
    void lotate(int &x) { int lson = lc[x]; lc[x] = rc[lson]; rc[lson] = x; pushup(x); pushup(lson); x = lson; }
    void rotate(int &x) { int rson = rc[x]; rc[x] = lc[rson]; lc[rson] = x; pushup(x); pushup(rson); x = rson; }
    void insert(int &x, int key)
    {
    	if (x == 0) { x = ++tot; sz[x] = 1; w[x] = key; h[x] = rand(); return; }
    	if (key <= w[x]) { insert(lc[x], key); if (h[lc[x]] < h[x]) { lotate(x); } }
    	else { insert(rc[x], key); if (h[rc[x]] < h[x]) { rotate(x); } } pushup(x);
    }
    void erase(int &x, int key)
    {
    	if (w[x] == key)
    	{
    		if (lc[x] == 0 || rc[x] == 0) { x = lc[x] + rc[x]; return; }
    		else { if (h[lc[x]] < h[rc[x]]) rotate(x), erase(lc[x], key); else lotate(x), erase(rc[x], key); }
    	}
    	else { if (w[x] > key) erase(lc[x], key); else erase(rc[x], key); } pushup(x);
    }
    int qrank(int x, int key) { if (x == 0) { return 1; } if (w[x] >= key) return qrank(lc[x], key); else return qrank(rc[x], key) + sz[lc[x]] + 1; }
    int query(int x, int rk)
    { if (sz[lc[x]] + 1 == rk) return w[x]; if (sz[lc[x]] + 1 < rk) return query(rc[x], rk - sz[lc[x]] - 1); else return query(lc[x], rk); }
    int left(int x, int key) { if (x == 0) { return -infinity; } if (w[x] < key) return max(w[x], left(rc[x], key)); else return left(lc[x], key); }
    int right(int x, int key) { if (x == 0) { return infinity; } if (w[x] > key) return min(w[x], right(lc[x], key)); else return right(rc[x], key); }
    
    int main()
    {
    	scanf("%d", &n);
    	for (int x, y, i = 1; i <= n; i++)
    	{
    		scanf("%d%d", &x, &y);
    		if (x == 1) insert(rt, y);
    		if (x == 2) erase(rt, y);
    		if (x == 3) printf("%d
    ", qrank(rt, y));
    		if (x == 4) printf("%d
    ", query(rt, y));
    		if (x == 5) printf("%d
    ", left(rt, y));
    		if (x == 6) printf("%d
    ", right(rt, y));
    	}
    	return 0;
    }
    
  • 相关阅读:
    Expect学习笔记(1)
    Awk 实例,第 3 部分
    ELF文件格式(中文版)
    sed 实例,第 1 部分
    Expect 教程中文版
    csc工具一般使用说明zz
    Microsoft Office Word 2010(zz)
    记录XPO查询 日志
    C#中判断文件或文件夹是否存在
    Discuz数据库结构1
  • 原文地址:https://www.cnblogs.com/oier/p/10536092.html
Copyright © 2020-2023  润新知