终末のcode

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long #define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin); #else #define D_e_Line ;
#define D_e(x) ;
#define Pause() ;
#define FileOpen() ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N = 100007; struct Tree{
int ch[2], fa, val, tot, siz;
}t[N];
int treeIndex, root;
inline void Pushup(int rt){
t[rt].siz = t[t[rt].ch[0]].siz + t[t[rt].ch[1]].siz + t[rt].tot;
}
inline int Ident(int x){
return x == t[t[x].fa].ch[1];
}
inline void Rotate(int x){
int y = t[x].fa, z = t[y].fa, k = Ident(x);
t[z].ch[Ident(y)] = x, t[x].fa = z;
t[y].ch[k] = t[x].ch[k ^ 1], t[t[x].ch[k ^ 1]].fa = y;
t[x].ch[k ^ 1] = y, t[y].fa = x;
Pushup(y), Pushup(x);
}
inline void Splay(int x, int pos){
while(t[x].fa != pos){
int y = t[x].fa, z = t[y].fa;
if(z != pos){
Ident(x) == Ident(y) ? Rotate(y) : Rotate(x);
}
Rotate(x);
}
if(!pos) root = x;
}
inline void Find(int x){
int u = root;
if(!u) return;
while(t[u].ch[x > t[u].val] && t[u].val != x) u = t[u].ch[x > t[u].val];
Splay(u, 0);
}
inline void Insert(int x){
int u = root, fa = 0;
while(u && t[u].val != x){
fa = u;
u = t[u].ch[x > t[u].val];
}
if(u){
++t[u].tot;
}
else{
u = ++treeIndex;
t[u].fa = fa;
t[u].val = x;
t[u].siz = t[u].tot = 1;
t[u].ch[0] = t[u].ch[1] = 0;
if(fa) t[fa].ch[x > t[fa].val] = u;
}
Splay(u, 0);
}
inline int Kth(int x){
int u = root;
if(t[u].siz < x) return 0;
while(1){
int v = t[u].ch[0];
if(t[v].siz + t[u].tot < x){
x -= t[v].siz + t[u].tot;
u = t[u].ch[1];
}
else if(t[v].siz >= x){
u = v;
}
else
return t[u].val;
}
}
inline int Next(int x, int type){
Find(x);
int u = root;
if(t[u].val > x && type) return u;
if(t[u].val < x && !type) return u;
u = t[u].ch[type];
while(t[u].ch[type ^ 1]) u = t[u].ch[type ^ 1];
return u;
}
inline void Delete(int x){
int pre = Next(x, 0), nxt = Next(x, 1);
Splay(pre, 0), Splay(nxt, pre);
int u = t[nxt].ch[0];
if(t[u].tot > 1){
--t[u].tot;
Splay(u, 0);
}
else{
t[nxt].ch[0] = 0;
}
}
int main(){
int m;
io >> m;
Insert(1e9);
Insert(-1e9);
while(m--){
int opt, x;
io >> opt >> x;
switch(opt){
case 1:{
Insert(x);
break;
}
case 2:{
Delete(x);
break;
}
case 3:{
Find(x);
printf("%d\n", t[t[root].ch[0]].siz);
break;
}
case 4:{
printf("%d\n", Kth(x + 1));
break;
}
case 5:{
printf("%d\n", t[Next(x, 0)].val);
break;
}
case 6:{
printf("%d\n", t[Next(x, 1)].val);
break;
}
}
}
return 0;
}
50
1 577793
1 408221
1 880861
2 408221
1 460353
1 223489
6 577713
4 2
5 889905
2 880861
1 100033
1 73956
1 22575
5 583761
6 571549
1 812645
4 3
1 643621
1 451623
6 14895
1 556691
4 1
1 225789
2 22575
1 632329
3 73956
1 316785
5 101413
4 11
5 639414
6 636353
1 272382
1 434049
2 643621
1 99617
2 577793
1 921581
1 894033
3 223489
1 767367
3 272382
1 642721
1 272033
3 632329
1 737721
1 864513
5 746457
1 877545
1 51097
1 484817 ~
577793
460353
880861
577793
577793
100033
22575
22575
1
100033
643621
632329
643621
4
6
13
737721 */

A mater' s BIT... I do not understand at all...

Just record it here anyway.

const int MAX = 1 << 25;
const int dealZero = 10000007; int t[MAX+7];
inline void Updata(int x,int w){
for(x += dealZero; x <= MAX; x += x&-x) t[x] += w;
}
inline int Query(int x){
x+=dealZero;
int sum=0;
while(x) sum+=t[x],x&=x-1;
return sum;
}
inline int Kth(int k,int rt=MAX){
for(int i=rt;i>>=1;)
if(t[rt-i]>=k)rt-=i;
else k-=t[rt-i];
return rt-dealZero;
}
int n,opt,x;
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i){
scanf("%d%d",&opt,&x);
if(opt==1)Updata(x,1);
if(opt==2)Updata(x,-1);
if(opt==3)printf("%d\n",Query(x-1)+1);
if(opt==4)printf("%d\n",Kth(x));
if(opt==5)printf("%d\n",Kth(Query(x-1)));
if(opt==6)printf("%d\n",Kth(Query(x)+1));
}
}

还有一坨骗分神器.jpg

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long
#define ON_DEBUG #ifdef ON_DEBUG #define D_e_Line printf("\n\n----------\n\n")
#define D_e(x) cout << #x << " = " << x << endl
#define Pause() system("pause") #else #define D_e_Line ; #endif struct ios{
template<typename ATP>ios& operator >> (ATP &x){
x = 0; int f = 1; char c;
for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-') f = -1;
while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
x*= f;
return *this;
}
}io;
using namespace std; const int N=100007; #include<vector>
vector<int> t; int main(){
int m;
io >> m;
t.reserve(N);
while(m--){
int opt, x;
io >> opt >> x;
switch(opt){
case 1:{
t.insert(upper_bound(t.begin(), t.end(), x), x);
break;
}
case 2:{
t.erase(lower_bound(t.begin(), t.end(), x));
break;
}
case 3:{
printf("%lld\n", lower_bound(t.begin(), t.end(), x) - t.begin() + 1);
break;
}
case 4:{
printf("%d\n", t[x-1]);
break;
}
case 5:{
printf("%d\n", *(--lower_bound(t.begin(), t.end(), x)));
break;
}
case 6:{
printf("%d\n", *(upper_bound(t.begin(), t.end(), x)));
break;
}
}
}
}

BZOJ3224/LuoguP3369 普通平衡树 (splay)的更多相关文章

  1. [Bzoj3224][Tyvj1728] 普通平衡树(splay/无旋Treap)

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3224 平衡树入门题,学习学习. splay(学习yyb巨佬) #include<b ...

  2. 【BZOJ3224】Tyvj 1728 普通平衡树 Splay

    Description 您需要写一种数据结构(可参考题目标题),来维护一些数,其中需要提供以下操作:1. 插入x数2. 删除x数(若有多个相同的数,因只删除一个)3. 查询x数的排名(若有多个相同的数 ...

  3. BZOJ3224/洛谷P3391 - 普通平衡树(Splay)

    BZOJ链接 洛谷链接 题意简述 模板题啦~ 代码 //普通平衡树(Splay) #include <cstdio> int const N=1e5+10; int rt,ndCnt; i ...

  4. hiho #1329 : 平衡树·Splay

    #1329 : 平衡树·Splay 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. ...

  5. Hihocoder 1329 平衡树·Splay(平衡树)

    Hihocoder 1329 平衡树·Splay(平衡树) Description 小Ho:小Hi,上一次你跟我讲了Treap,我也实现了.但是我遇到了一个关键的问题. 小Hi:怎么了? 小Ho:小H ...

  6. 【阶梯报告】洛谷P3391【模板】文艺平衡树 splay

    [阶梯报告]洛谷P3391[模板]文艺平衡树 splay 题目链接在这里[链接](https://www.luogu.org/problemnew/show/P3391)最近在学习splay,终于做对 ...

  7. luoguP3391[模板]文艺平衡树(Splay) 题解

    链接一下题目:luoguP3391[模板]文艺平衡树(Splay) 平衡树解析 这里的Splay维护的显然不再是权值排序 现在按照的是序列中的编号排序(不过在这道题目里面就是权值诶...) 那么,继续 ...

  8. 平衡树——splay 三

    前文链接: 平衡树--splay 一 - yi_fan0305 - 博客园 (cnblogs.com) 平衡树--splay 二 - yi_fan0305 - 博客园 (cnblogs.com) 再补 ...

  9. 平衡树——splay 二

    上文传送门:平衡树--splay 一 - yi_fan0305 - 博客园 (cnblogs.com) OK,我们继续上文,来讲一些其他操作. 七.找排名为k的数 和treap的操作很像,都是通过比较 ...

随机推荐

  1. RabitMQ 简介

    每日一句 The secret of being miserable is to have leisure to bother about whether you are happy or not. ...

  2. 项目下载依赖后面加 -S -D -g 分别代表什么意思

    npm install name -S此依赖是在package的dependencies中,不仅在开发中,也在打包上线后的生产环境中,比如vue npm install name -D此依赖是在pac ...

  3. java基础题(3)

    5.面向对象 5.1封装 5.1.1修改Data类的定义 描述 现有一个Data类,内部定义了属性x和y,在main方法中实例化了Data类,并计算了data对象中x和y的和.但是,Data类的定义存 ...

  4. 【clickhouse专栏】单机版的安装与验证

    <clickhouse专栏>第三节内容,先安装一个单机版的clickhouse,是后续学习多副本或者分布式集群安装的基础内容.但基本的clickhouse是不依赖于zookeeper的,只 ...

  5. 互联网公司实行目标管理(OKR)五点原则和基础

    下面从公司文化.组织架构.管理者.落地执行和区别绩效考核五个方面,讲述了如何在公司落地目标管理(OKR),这些是实施OKR的基础,也是原则,虽然写得比较简单,其实是我过去两年多不断观察.实践和摸索的总 ...

  6. C#中的枚举器

    更新记录 本文迁移自Panda666原博客,原发布时间:2021年6月28日. 一.先从可枚举类型讲起 1.1 什么是可枚举类型? 可枚举类型,可以简单的理解为: 有一个类,类中有挺多的数据,用一种统 ...

  7. C# 使用SpecFlow创建BDD测试用例

    将自然语言编写的测试用例转换为可执行的测试,可以大大降低需求与开发之间的沟通成本,这是BDD(行为驱动开发)希望达到的效果.SpecFlow是.Net平台的BDD工具,可以帮助我们创建面向BDD的测试 ...

  8. 1.1 操作系统的第一个功能——虚拟化硬件资源 -《zobolの操作系统学习札记》

    1.1 操作系统的第一个功能--虚拟化硬件资源 目录 1.1 操作系统的第一个功能--虚拟化硬件资源 问1:操作系统一般处于计算机系统的哪一个位置? 问2:管理硬件资源为什么要单独交给操作系统? 问3 ...

  9. C语言学习之我见-strcpy()字符串复制函数

    strcpy()函数,用于两个字符串值的复制. (1)函数原型 char * strcpy(char * _Dest,const char * _Source); (2)头文件 string.h (3 ...

  10. 记一次ms17-010复现过程

    最近碰到业务需要使用msf,以前了解过,后面都忘记了.这次干脆写下来,省的每次去找别人写的. 首先是使用nmap探测端口 nmap -O -sV 192.168.153.130 --script=vu ...