【题解】Luogu P3871 [TJOI2010]中位数
平衡树板题
原题传送门
这道题要用Splay,我博客里有对Splay的详细介绍
每次加入一个数,把数插入平衡树中
并且要记录一共有多少个数
每次查询就查询平衡树中第(总数-1)/2+1个数
十分暴力
#include <bits/stdc++.h>
#define N 110005
#define root tree[0].ch[1]
using namespace std;
inline int read()
{
register int x=0,f=1;register char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();
return x*f;
}
inline void write(register int x)
{
if(!x)putchar('0');if(x<0)x=-x,putchar('-');
static int sta[36];int tot=0;
while(x)sta[tot++]=x%10,x/=10;
while(tot)putchar(sta[--tot]+48);
}
struct splay{
int v,fa,ch[2],rec,sum;
}tree[N];
int tot=0;
inline bool findd(register int x)
{
return x==tree[tree[x].fa].ch[0]?0:1;
}
inline void connect(register int x,register int fa,register int son)
{
tree[x].fa=fa;
tree[fa].ch[son]=x;
}
inline void update(register int x)
{
tree[x].sum=tree[tree[x].ch[0]].sum+tree[tree[x].ch[1]].sum+tree[x].rec;
}
inline void rotate(register int x)
{
int Y=tree[x].fa;
int R=tree[Y].fa;
int Yson=findd(x);
int Rson=findd(Y);
int B=tree[x].ch[Yson^1];
connect(B,Y,Yson);
connect(Y,x,Yson^1);
connect(x,R,Rson);
update(Y),update(x);
}
inline void splay(register int x,register int to)
{
to=tree[to].fa;
while(tree[x].fa!=to)
{
int y=tree[x].fa;
if(tree[y].fa==to)
rotate(x);
else if(findd(x)==findd(y))
rotate(y),rotate(x);
else
rotate(x),rotate(x);
}
}
inline int newpoint(register int v,register int fa)
{
tree[++tot].v=v;
tree[tot].fa=fa;
tree[tot].sum=tree[tot].rec=1;
return tot;
}
inline void Insert(register int x)
{
int now=root;
if(root==0)
{
newpoint(x,0);
root=tot;
}
else
{
while(19260817)
{
++tree[now].sum;
if(x==tree[now].v)
{
++tree[now].rec;
splay(now,root);
return;
}
int nxt=x<tree[now].v?0:1;
if(!tree[now].ch[nxt])
{
int p=newpoint(x,now);
tree[now].ch[nxt]=p;
splay(p,root);
return;
}
now=tree[now].ch[nxt];
}
}
}
inline int arank(register int x)
{
int now=root;
while(19260817)
{
int used=tree[now].sum-tree[tree[now].ch[1]].sum;
if(x>tree[tree[now].ch[0]].sum&&x<=used)
{
splay(now,root);
return tree[now].v;
}
if(x<used)
now=tree[now].ch[0];
else
x-=used,now=tree[now].ch[1];
}
}
int main()
{
int n=read();
int sum=0;
for(register int i=1;i<=n;++i)
{
int x=read();
Insert(x);
++sum;
}
int m=read();
while(m--)
{
char ch=getchar();
while(ch!='a'&&ch!='m')
ch=getchar();
if(ch=='a')
{
int x=read();
Insert(x);
++sum;
}
else if(ch=='m')
{
write(arank((sum-1)/2+1));
printf("\n");
}
}
return 0;
}
【题解】Luogu P3871 [TJOI2010]中位数的更多相关文章
- [LUOGU] P3871 [TJOI2010]中位数
题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从 ...
- 洛谷 P3871 [TJOI2010]中位数 解题报告
P3871 [TJOI2010]中位数 题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前 ...
- 洛谷——P3871 [TJOI2010]中位数
P3871 [TJOI2010]中位数 一眼秒掉,这不是splay水题吗,套模板 #include<bits/stdc++.h> #define IL inline #define N 1 ...
- 洛谷P3871 [TJOI2010]中位数(splay)
题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从 ...
- P3871 [TJOI2010]中位数
傻逼题 维护两个系统堆即可 #include<bits/stdc++.h> #define il inline #define vd void typedef long long ll; ...
- luoguP3871 [TJOI2010]中位数
题目链接 luoguP3871 [TJOI2010]中位数 题解 平衡树 代码 #include<vector> #include<cstdio> #include<cs ...
- [题解] Luogu P5446 [THUPC2018]绿绿和串串
[题解] Luogu P5446 [THUPC2018]绿绿和串串 ·题目大意 定义一个翻转操作\(f(S_n)\),表示对于一个字符串\(S_n\), 有\(f(S)= \{S_1,S_2,..., ...
- 题解 P3871 【[TJOI2010]中位数】
orz各位大佬,题解太强了,主席树,堆,线段树,splay,还有暴力,太巨了.所以我用的是fhq treap(好像更高级).算了. 反正都是平衡树,这道题就是动态求中位数,不会做的同学可以先做弱化版P ...
- 洛谷 题解 P3871 【[TJOI2010]中位数】
这题先定义一个大根堆(maxn)维护mid(n为奇数mid+1)的元素.再定义一个小根堆(minn)维护mid(n为奇数mid+1)到n的元素.然后对于插入元素的情况进行分类讨论. 当add x时 一 ...
随机推荐
- git 冲突解决办法
1. Pull is not possible because you have unmerged files. 症状:pull的时候 $ git pull Pull is not possible ...
- import Tkinter error, no module named tkinter: "Python may not be configured for Tk”
install required devel module in your linux: yum install tk-devel yum install tcl-devel then,reconfi ...
- 一步一步学Python(3) 基础补充
最近在系统学习Python,以MOOC上面的一套Python3的课程为基础.本文主要总结一下基础部分的关键点. 1.python基本数据类型 2.python运算符 3.构建简洁高效的IDE环境 4. ...
- 《linux就该这么学》找到一本不错的Linux电子书,《Linux就该这么学》。
本帖不是广告贴,只是感觉有好的工具书而已 本书是由全国多名红帽架构师(RHCA)基于最新Linux系统共同编写的高质量Linux技术自学教程,极其适合用于Linux技术入门教程或讲课辅助教材,目前是国 ...
- Delegate,Action,Func,匿名方法,匿名委托,事件
一.委托Delegate 一般的方法(Method)中,我们的参数总是string,int,DateTime...这些基本的数据类型(或者没有参数),比如 public void HelloWorld ...
- activiti 项目变更控制器
package com.xinwei.process.controller; import java.util.Calendar; import java.util.HashMap; import j ...
- 记无法用被动方式登录远程linux主机的原因
[环境]: linux主机:华为企业云 ftp服务端:vsftpd 客户端:ftp命令行工具,安卓端ES文件浏览器 [现象]: 在ES文件浏览器中,使用被动方式没法连接,使用主动方式可以连接,但是没法 ...
- Python之函数&参数&参数解构
1.1函数定义 def 函数名(参数列表): 函数体(代码块) [return 返回值] p 函数名就是标识符,命名要求一样 语句块必须缩进,约定4个空格 Python的函数没有return语句,隐式 ...
- 《Semantic Sentence Matching with Densely-connected Recurrent and Co-attentive Information》DRCN 句子匹配
模型结构 首先是模型图: 传统的注意力机制无法保存多层原始的特征,根据DenseNet的启发,作者将循环网络的隐层的输出与最后一层连接. 另外加入注意力机制,代替原来的卷积.由于最后的特征维度过大,加 ...
- form的action属性值对应servlet的web.xml的url-pattern
<form action="abc">在web.xml里面<servlet><servlet-name>123</servlet-name ...