题目描述
给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从小到大排序后处在中间位置的数。(若序列长度为偶数,则指处在中间位置的两个数中较小的那个) 例1:1 2 13 14 15 16 中位数为13 例2:1 3 5 7 10 11 17 中位数为7 例3:1 1 1 2 3 中位数为1 输入输出格式
输入格式:
第一行为初始序列长度N。第二行为N个整数,表示整数序列,数字之间用空格分隔。第三行为操作数M,即要进行M次操作。下面为M行,每行输入格式如题意所述。 输出格式:
对于每个mid操作输出中位数的值 输入输出样例
输入样例#1:
6
1 2 13 14 15 16
5
add 5
add 3
mid
add 20
mid
输出样例#1:
5
13
说明
对于30%的数据,1 ≤ N ≤ 10,000,0 ≤ M ≤ 1,000 对于100%的数据,1 ≤ N ≤ 100,000,0 ≤ M ≤ 10,000 序列中整数的绝对值不超过1,000,000,000,序列中的数可能有重复 每个测试点时限1秒

Splay,kth

#include<iostream>
#include<cstdio> using namespace std; inline int rd() {
int ret=0,f=1;
char c;
while(c=getchar(),!isdigit(c))f=c=='-'?-1:1;
while(isdigit(c))ret=ret*10+c-'0',c=getchar();
return ret*f;
} const int MAXN=1000005; int num;
int val[MAXN],cnt[MAXN],siz[MAXN];
int ch[MAXN][2],fa[MAXN];
int root,tot;
inline int newnode(int x){num++;val[++tot]=x;cnt[tot]=siz[tot]=1;return tot;}
inline bool check(int x){return x==ch[fa[x]][1];}
inline void pushup(int x){siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+cnt[x];}
void rotate(int x){
int y=fa[x],z=fa[fa[x]];
bool ck=check(x);
fa[ch[x][ck^1]]=y;ch[y][ck]=ch[x][ck^1];
ch[x][ck^1]=y;fa[y]=x;fa[x]=z;
if(z) ch[z][ch[z][1]==y]=x;
pushup(y);pushup(x);
}
void splay(int x){
for(int f=fa[x];f;rotate(x),f=fa[x])
if(fa[f]) rotate(check(x)==check(f)?f:x);
root=x;
}
void insert(int x){
if(!root){root=newnode(x);return;}
int cur=root,f=0;
while(1){
if(val[cur]==x){num++;cnt[cur]++;pushup(cur);pushup(f);splay(cur);return;}
f=cur;cur=ch[cur][x>val[cur]];
if(!cur){cur=newnode(x);fa[cur]=f;ch[f][x>val[f]]=cur;pushup(f);splay(cur);return;}
}
}
int kth(int x){
int cur=root;
while(1){
if(x<=siz[ch[cur][0]]) cur=ch[cur][0];
else{
x-=siz[ch[cur][0]]+cnt[cur];
if(x<=0) return val[cur];
cur=ch[cur][1];
}
}
} int n,m; int main(){
n=rd();
for(int i=1;i<=n;i++) insert(rd());
m=rd();
char s[10];
for(int i=1;i<=m;i++){
scanf("%s",s);
if(s[0]=='a') insert(rd());
else printf("%d\n",kth((num+1)/2));
}
return 0;
}

[LUOGU] P3871 [TJOI2010]中位数的更多相关文章

  1. 【题解】Luogu P3871 [TJOI2010]中位数

    平衡树板题 原题传送门 这道题要用Splay,我博客里有对Splay的详细介绍 每次加入一个数,把数插入平衡树中 并且要记录一共有多少个数 每次查询就查询平衡树中第(总数-1)/2+1个数 十分暴力 ...

  2. 洛谷 P3871 [TJOI2010]中位数 解题报告

    P3871 [TJOI2010]中位数 题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前 ...

  3. 洛谷——P3871 [TJOI2010]中位数

    P3871 [TJOI2010]中位数 一眼秒掉,这不是splay水题吗,套模板 #include<bits/stdc++.h> #define IL inline #define N 1 ...

  4. 洛谷P3871 [TJOI2010]中位数(splay)

    题目描述 给定一个由N个元素组成的整数序列,现在有两种操作: 1 add a 在该序列的最后添加一个整数a,组成长度为N + 1的整数序列 2 mid 输出当前序列的中位数 中位数是指将一个序列按照从 ...

  5. P3871 [TJOI2010]中位数

    傻逼题 维护两个系统堆即可 #include<bits/stdc++.h> #define il inline #define vd void typedef long long ll; ...

  6. luoguP3871 [TJOI2010]中位数

    题目链接 luoguP3871 [TJOI2010]中位数 题解 平衡树 代码 #include<vector> #include<cstdio> #include<cs ...

  7. 题解 P3871 【[TJOI2010]中位数】

    orz各位大佬,题解太强了,主席树,堆,线段树,splay,还有暴力,太巨了.所以我用的是fhq treap(好像更高级).算了. 反正都是平衡树,这道题就是动态求中位数,不会做的同学可以先做弱化版P ...

  8. 洛谷 题解 P3871 【[TJOI2010]中位数】

    这题先定义一个大根堆(maxn)维护mid(n为奇数mid+1)的元素.再定义一个小根堆(minn)维护mid(n为奇数mid+1)到n的元素.然后对于插入元素的情况进行分类讨论. 当add x时 一 ...

  9. TJOI2010中位数

    中位数 上面是题目链接. 这一题比较水. 思路非常显然. 用mid查询时,只要返回中间值就行了. 主要就是add操作. 我们肯定不能插在末尾,然后用系统快排,这样只有30分. 那么正确的操作应该是二分 ...

随机推荐

  1. (水题)Codeforces - 650A - Watchmen

    http://codeforces.com/contest/650/problem/A 一开始想了很久都没有考虑到重复点的影响,解欧拉距离和曼哈顿距离相等可以得到 $x_i=x_j$ 或 $y_i=y ...

  2. bzoj 3992: [SDOI2015]序列统计【原根+生成函数+NTT+快速幂】

    还是没有理解透原根--题目提示其实挺明显的,M是质数,然后1<=x<=M-1 这种计数就容易想到生成函数,但是生成函数是加法,而这里是乘法,所以要想办法变成加法 首先因为0和任何数乘都是0 ...

  3. gcc降版本方法 - [学习]

    [转载]转载时请以超链接形式标明文章原始出处和作者信息及本声明http://www.blogbus.com/huangw10-logs/182474992.html 周末折腾了一下午加一夜,终于弄明白 ...

  4. 模拟赛01 T3 盖房子

    题面 http://zhengruioi.com/problem/248 题解 三重容斥(说是两重也行吧) 我们来看题目的约束 ①有k个位置不能放(k≤8) ②每行每列至少一个 ③正负对角线至少一个 ...

  5. UvalLive4670(AC自动机模板)

    放上刘汝佳的模板: #include <cstdio> #include <cstring> #include <string> #include <algo ...

  6. python收发邮件的方法

    def acptmail(): email = 'xxx@163.com' #input('Email:') password = 'xxx' #input('Password: ') pop3_se ...

  7. phpcms v9模板制作教程

    phpcms v9模板制作教程(转载) 第一节 1.首先下载phpcms v9的集成安装包并安装,这里就不详细说明了. 2.本地调试建议大家使用APMserver,或者wampserver等,可以到P ...

  8. jsp <%@ include %> 例子

    <%@ include %>:所有代码包含进来之后一起进行处理,最终编译成一个servlet. jsp文件中添加top和bottom.jsp页面 empList.jsp <%@ pa ...

  9. 浏览器报错问题解决:Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight respons

    FAQ: Failed to load http://www.erpshop.com/index.php: Request header field Content-Type is not allow ...

  10. windows系统下查看或删除自己电脑的共享文件以及文件夹

    (1)查看所有共享 net share (2)删除指定共享 例如:删除C盘共享 net share C$ /delete     net share 共享名 /delete (/del)