AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369
思路:
劳资敲了一个多星期;
劳资终于a了;
劳资一直不a是因为一个小错误;
劳资最后看的模板;
劳资现在很愤怒;
劳资不想谈思路!!!
来,上代码:
- #include <cstdio>
- using namespace std;
- #define maxn 1000005
- struct SplayTreeNodeType {
- int w,key,opi,size,ch[];
- };
- struct SplayTreeNodeType tree[maxn];
- int n,root,tot;
- inline void updata(int now)
- {
- tree[now].size=tree[now].w;
- if(tree[now].ch[]) tree[now].size+=tree[tree[now].ch[]].size;
- if(tree[now].ch[]) tree[now].size+=tree[tree[now].ch[]].size;
- }
- inline int pre()
- {
- int now=tree[root].ch[];
- while(tree[now].ch[]) now=tree[now].ch[];
- return now;
- }
- inline int suc()
- {
- int now=tree[root].ch[];
- while(tree[now].ch[]) now=tree[now].ch[];
- return now;
- }
- inline int getson(int now)
- {
- return tree[tree[now].opi].ch[]==now;
- }
- void rotate(int now)
- {
- int opi=tree[now].opi,fopi=tree[opi].opi,pos=getson(now);
- tree[opi].ch[pos]=tree[now].ch[pos^];
- tree[tree[opi].ch[pos]].opi=opi;
- tree[now].ch[pos^]=opi;tree[opi].opi=now;
- tree[now].opi=fopi;
- if(fopi) tree[fopi].ch[tree[fopi].ch[]==opi]=now;
- updata(opi),updata(now);
- }
- void splay(int now)
- {
- for(int opi;opi=tree[now].opi;rotate(now))
- {
- if(tree[opi].opi) rotate(getson(now)==getson(opi)?opi:now);
- }
- root=now;
- }
- int rank(int x)
- {
- int now=root,ans=;
- while()
- {
- if(x<tree[now].key) now=tree[now].ch[];
- else
- {
- ans+=tree[now].ch[]?tree[tree[now].ch[]].size:;
- if(x==tree[now].key)
- {
- splay(now);
- return ans+;
- }
- ans+=tree[now].w;
- now=tree[now].ch[];
- }
- }
- }
- int rank_(int x)
- {
- int now=root;
- while()
- {
- if(tree[now].ch[]&&x<=tree[tree[now].ch[]].size) now=tree[now].ch[];
- else
- {
- int tmp=(tree[now].ch[]?tree[tree[now].ch[]].size:)+tree[now].w;
- if(x<=tmp) return tree[now].key;
- x-=tmp;now=tree[now].ch[];
- }
- }
- }
- inline void clear(int now)
- {
- tree[now].ch[]=tree[now].ch[]=tree[now].w=tree[now].size=tree[now].key=tree[now].opi=;
- }
- inline void create(int x)
- {
- tree[++tot].key=x;
- tree[tot].w=tree[tot].size=;
- tree[tot].ch[]=tree[tot].ch[]=tree[tot].opi=;
- }
- void insert(int x)
- {
- if(!root) create(x),root=tot;
- else
- {
- int now=root,opi=;
- while()
- {
- if(tree[now].key==x)
- {
- tree[now].w++;
- tree[now].size++;
- splay(now);
- break;
- }
- opi=now;
- now=tree[now].ch[x>tree[opi].key];
- if(!now)
- {
- create(x);
- tree[tot].opi=opi;
- tree[opi].ch[x>tree[opi].key]=tot;
- splay(tot);
- break;
- }
- }
- }
- }
- void del(int x)
- {
- int t=rank(x);
- if(tree[root].w>)
- {
- tree[root].w--;
- tree[root].size--;
- return ;
- }
- if(!tree[root].ch[]&&!tree[root].ch[])
- {
- clear(root);
- root=;
- return ;
- }
- if(!tree[root].ch[])
- {
- int tmp=root;
- root=tree[root].ch[];
- tree[root].opi=;
- clear(tmp);
- return ;
- }
- if(!tree[root].ch[])
- {
- int tmp=root;
- root=tree[root].ch[];
- tree[root].opi=;
- clear(tmp);
- return ;
- }
- int pre1=pre(),tmp=root;
- splay(pre1);
- tree[root].ch[]=tree[tmp].ch[];
- tree[tree[tmp].ch[]].opi=root;
- clear(tmp);updata(root);
- }
- inline void in(int &now)
- {
- register int if_z=;now=;
- register char Cget=getchar();
- while(Cget>''||Cget<'')
- {
- if(Cget=='-') if_z=-;
- Cget=getchar();
- }
- while(Cget>=''&&Cget<='')
- {
- now=now*+Cget-'';
- Cget=getchar();
- }
- now*=if_z;
- }
- int main()
- {
- in(n);int ty,x;
- while(n--)
- {
- in(ty),in(x);
- if(ty==) insert(x);
- if(ty==) del(x);
- if(ty==) printf("%d\n",rank(x));
- if(ty==) printf("%d\n",rank_(x));
- if(ty==) insert(x),printf("%d\n",tree[pre()].key),del(x);
- if(ty==) insert(x),printf("%d\n",tree[suc()].key),del(x);
- }
- return ;
- }
AC日记——【模板】普通平衡树(Treap/SBT) 洛谷 P3369的更多相关文章
- luoguP3369[模板]普通平衡树(Treap/SBT) 题解
链接一下题目:luoguP3369[模板]普通平衡树(Treap/SBT) 平衡树解析 #include<iostream> #include<cstdlib> #includ ...
- AC日记——[USACO15DEC]最大流Max Flow 洛谷 P3128
题目描述 Farmer John has installed a new system of pipes to transport milk between the stalls in his b ...
- AC日记——[USACO10MAR]仓配置Barn Allocation 洛谷 P1937
[USACO10MAR]仓配置Barn Allocation 思路: 贪心+线段树维护: 代码: #include <bits/stdc++.h> using namespace std; ...
- AC日记——[ZJOI2015]幻想乡战略游戏 洛谷 P3345
[ZJOI2015]幻想乡战略游戏 思路: 树剖暴力转移: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1 ...
- AC日记——[HNOI2010]BOUNCE 弹飞绵羊 洛谷 P3203
[HNOI2010]BOUNCE 弹飞绵羊 思路: SBlct: 代码: #include <bits/stdc++.h> using namespace std; #define max ...
- AC日记——斐波那契数列 洛谷 P1962
斐波那契数列 思路: 矩阵快速幂: 来,上代码: #include <cstdio> #include <cstring> #include <iostream> ...
- AC日记——[JLOI2014]松鼠的新家 洛谷 P3258
题目描述 松鼠的新家是一棵树,前几天刚刚装修了新家,新家有n个房间,并且有n-1根树枝连接,每个房间都可以相互到达,且俩个房间之间的路线都是唯一的.天哪,他居然真的住在”树“上. 松鼠想邀请小熊维尼前 ...
- AC日记——[USACO09JAN]全流Total Flow 洛谷 P2936
题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 < ...
- AC日记——[USACO11DEC]牧草种植Grass Planting 洛谷 P3038
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...
- AC日记——让我们异或吧 洛谷 P2420
题目描述 异或是一种神奇的运算,大部分人把它总结成不进位加法. 在生活中…xor运算也很常见.比如,对于一个问题的回答,是为1,否为0.那么: (A是否是男生 )xor( B是否是男生)=A和B是否能 ...
随机推荐
- Federated引擎
Federated就像他的名字所说“联盟”,其作用就是把两个不同区域的数据库联系起来,以至可以访问在远程数据库的表中的数据,而不是本地的表. 1.进入mysql命令行,查看是否已安装Federated ...
- UVA11825 Hacker's Crackdown 二进制集合+关于子集的动态规划
题意:有N台服务器,全部服务器都直接运行着完全相同的N个任务.对于每台电脑,你都可以进行“一次”操作,使得某(自己选定)一种任务停止,且同时会使得其他和这台服务器直接相连的电脑上面相同的服务完全终止. ...
- 读书笔记jvm探秘之二: 对象创建
对象是面向对象设计语言无法回避的东西,可见其重要性,JAVA的对象相较于C++来说,不算很复杂,但是我们看到一句话背后往往有很多东西值得探讨(NEW关键字). 对象如何被创建? 首先一句简单的NEW语 ...
- Codeforces 653G Move by Prime 组合数学
题意: 有一个长度为\(n\)的正整数序列\(a\),有这样一种操作: 每次可以选序列中的某一个数乘上或除以某一个素数. 求对于每一个子序列使其所有元素相等的最少操作次数之和. 分析: 因为两个素数之 ...
- log4j2用Log4jContextSelector启动参数配置全局异步日志是如何使用disruptor
与 log4j2用asyncRoot配置异步日志是如何使用disruptor差异有几个: 给disruptor实例的EventFactory不同 此处EventFactory采用的是RingBuffe ...
- jmeter快捷键
快捷键 功能 备注 Ctrl + C 复制 可复制组件 Ctrl + V 粘贴 可粘贴组件 Ctrl + Shift + C 复制粘贴当前组件到下一行 Ctrl + R 运行测试计划 Ctrl ...
- Java类和对象 详解(一)---写的很好通俗易懂---https://blog.csdn.net/wei_zhi/article/details/52745268
https://blog.csdn.net/wei_zhi/article/details/52745268
- backpropagation算法示例
backpropagation算法示例 下面举个例子,假设在某个mini-batch的有样本X和标签Y,其中\(X\in R^{m\times 2}, Y\in R^{m\times 1}\),现在有 ...
- CentOS7 haproxy+keepalived实现高可用集群搭建
一.搭建环境 CentOS7 64位 Keepalived 1.3.5 Haproxy 1.5.18 后端负载主机:192.168.166.21 192.168.166.22 两台节点上安装rabbi ...
- BZOJ 2190:[SDOI2008]仪仗队(欧拉函数)
[SDOI2008]仪仗队 Description 作为体育委员,C君负责这次运动会仪仗队的训练.仪仗队是由学生组成的N * N的方阵,为了保证队伍在行进中整齐划一,C君会跟在仪仗队的左后方,根据其视 ...