【题解】 

   平衡树模板题,不过因为可以离线,所以有别的做法。把询问倒着做,变成删掉数字、求中位数,于是可以二分+树状数组。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define rg register
#define N 500010
using namespace std;
int n,m,cnt,top,b[N],t[N],ans[N];
struct rec{
int num,pos;
}a[N];
struct que{
int opt,num;
}q[N];
inline int read(){
int k=,f=; char c=getchar();
while(c<''||c>'')c=='-'&&(f=-),c=getchar();
while(''<=c&&c<='')k=k*+c-'',c=getchar();
return k*f;
}
inline void add(int x,int y){for(;x<=n;x+=x&-x)t[x]+=y;}
inline int query(int x){int ret=;for(;x;x-=x&-x)ret+=t[x]; return ret;}
inline int search(int x){
int l=,r=n;
while(l+<r){
int mid=(l+r)>>;
if(query(mid)>=x) r=mid; else l=mid;
}
// printf("l=%d r=%d\n",l,r);
return r;
}
inline bool cmp(rec a,rec b){return a.num<b.num;}
int main(){
n=read();
for(rg int i=;i<=n;i++) a[i].num=read();
m=read();
for(rg int i=;i<=m;i++){
char c=getchar();
while(c!='a'&&c!='m') c=getchar();
if(c=='a'){
q[i].opt=,q[i].num=a[++n].num=read();
a[n].pos=i;
}
else q[i].opt=;
}
sort(a+,a++n,cmp);
for(rg int i=;i<=n;i++) add(i,);
for(rg int i=;i<=n;i++)if(a[i].pos) b[a[i].pos]=i;
// printf("n=%d\n",n);
for(rg int i=m;i;i--){
if(q[i].opt==){
add(b[i],-); cnt++;
}
else ans[++top]=a[search((n-cnt)/+(n-cnt)%)].num;
}
while(top) printf("%d\n",ans[top--]);
return ;
}

洛谷 3871 [TJOI2010]中位数的更多相关文章

  1. 洛谷3871 [TJOI2010]中位数 维护队列的中位数

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

  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. 洛谷 P1627 [CQOI2009]中位数 解题报告

    P1627 [CQOI2009]中位数 题目描述 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 输入输出格式 输入格式 ...

  6. 洛谷——P1627 [CQOI2009]中位数

    P1627 [CQOI2009]中位数 给出1~n的一个排列,统计该排列有多少个长度为奇数的连续子序列的中位数是b.中位数是指把所有元素从小到大排列后,位于中间的数. 中位数的题目有关统计的话,可以转 ...

  7. 洛谷 P3879 [TJOI2010]阅读理解

    P3879 [TJOI2010]阅读理解 题目描述 英语老师留了N篇阅读理解作业,但是每篇英文短文都有很多生词需要查字典,为了节约时间,现在要做个统计,算一算某些生词都在哪几篇短文中出现过. 输入输出 ...

  8. 【刷题】洛谷 P3872 [TJOI2010]电影迷

    题目描述 小A是一个电影迷,他收集了上百部的电影,打算从中挑出若干部在假期看完.他根据自己的口味和网上的介绍,对每部电影X都打了一个分数vX,表示自己喜欢的程度.这个分数的范围在-1000至1000之 ...

  9. [洛谷P3878][TJOI2010]分金币

    题目大意:把$n(n\leqslant30)$个数分成两组,两组个数最多相差$1$,求出两组元素差的绝对值最小使多少 题解:模拟退火 卡点:$\exp$中的两个数相减写反,导致$\exp(x)$中的$ ...

随机推荐

  1. jQuery笔记之animate中的queue

    队列 队列的执行顺序 queue() dequeue() 输出对象里面的内容 依次出队 不过这样写太麻烦了,因为每次都要输出,所以我们看下面的方法 运用到队列输出的 <!DOCTYPE html ...

  2. _bzoj1003 [ZJOI2006]物流运输【预处理】

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1003 预处理出第i天到第j天走一条航线时的最短路. #include <cstdio& ...

  3. 递推+高精度+找规律 UVA 10254 The Priest Mathematician

    题目传送门 /* 题意:汉诺塔问题变形,多了第四个盘子可以放前k个塔,然后n-k个是经典的汉诺塔问题,问最少操作次数 递推+高精度+找规律:f[k]表示前k放在第四个盘子,g[n-k]表示经典三个盘子 ...

  4. 题解报告:hdu 1570 A C

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1570 Problem Description Are you excited when you see ...

  5. Service官方教程(4)两种Service的生命周期函数

    Managing the Lifecycle of a Service The lifecycle of a service is much simpler than that of an activ ...

  6. js中判断数据类型的方法 typeof

    <input type="text" onblur="demo(this)"/><br/> <input type="n ...

  7. 1043 幸运号码 数位DP

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1043 设dp[i][j]表示前i位数中,i位数的和为j时的所有情况. 转 ...

  8. poj1715Hexadecimal Numbers(数位dp)

    链接 好久没写这种逐位计数的了. 先统计出总的数 ,s-n+1,倒着计算的 ,感觉倒着比较符合计算方式,总数为15*A(15,i) (1=<i<=8) 也就是1-8长度所有的排列总数 然后 ...

  9. vue 2.0 路由创建的详解过程

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. 5 Transforms 转移 笔记

    5 Transforms 转移 笔记   Transforms    Unfortunately, no one can be told what the Matrix is. You have to ...