终末の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. Typora详细教程以及下载

    ​ 发现一篇非常不错的 Typora 教程,分享给大家. 原文链接:https://www.cnblogs.com/hyacinthLJP/p/16123932.html 作者:MElephant T ...

  2. 从零搭建react+ts组件库(封装antd)

    为什么会有这样一篇文章?因为网上的教程/示例只说了怎么做,没有系统详细的介绍引入这些依赖.为什么要这样配置,甚至有些文章还是错的!迫于技术洁癖,我希望更多的开发小伙伴能够真正的理解一个项目搭建各个方面 ...

  3. 在Visual C++ 6.0中无法使用gets()函数的解决办法

    问题 昨晚遇到一个有意思的问题,明明在Visual Studio 2019运行好好的C语言代码,Copy到Visual C++ 6.0中就无法编译通过了,错误提示信息如下: error C2143: ...

  4. 经典漏洞-后台备份数据库getshell

    由于接触安全时间不多,一些老的getshell方法不是很清楚.这次碰到了个老站,正好学习了一下. 首先这边是用户名可以猜测出来的,因为输入错误的用户名显示用户名不存在,如果存在的话会显示密码错误. 爆 ...

  5. Ubuntu-apt-get出错

    ubuntu apt-get的时候出现: E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission ...

  6. 关于Node.js 链接mysql超时处理(默认8小时)

    备注:这是在pm2配置node环境下,超过8小时mysql自动关闭的情况下出现的解决方法:1.封装mysql.js var mysql = require('mysql'); var connecti ...

  7. AspNetCore&云效Flow持续集成

    如今有了越来越多的持续集成工具,给的个人开发者的福利也是很足了,如无必要,自建工具有时只是作为练手了. 众多持续集成工具 现在可用的持续集成工具繁多,各大云服务商都推出了持续集成,甚至是一定条件内都是 ...

  8. Linux文本三剑客-sed

    sed工作原理: sed: Stream Editor.流编辑器 --- 属于行编辑工具 sed和vim一样都是文本编辑工具. 行编辑工具:一行一行处理文件内容 全屏编辑工具:一次性将文件内容加载到内 ...

  9. windows下docker部署报错

    报错信息:Error response from daemon: Ports are not available: exposing port TCP 0.0.0.0:8848 -> 0.0.0 ...

  10. halcon简易标定代码

    read_image (Image, 'C:/Users/HJ/Desktop/demo1/4.bmp') threshold(Image, Region, 110, 255) closing_cir ...