"#define int long long" 导致100pts \(\rightarrow\) 80pts

#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 Treap{
int ch[2], fa, val, siz, tot;
}t[N << 2];
int root, treeIndex;
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 t[t[x].fa].ch[1] == x;
}
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 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].val = x;
t[u].fa =fa;
t[u].ch[0] = t[u].ch[1] = 0;
t[u].siz = t[u].tot = 1;
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[1];
if(t[v].siz + t[u].tot < x){
x -= t[v].siz + t[u].tot;
u = t[u].ch[0]; // !
}
else if(t[v].siz >= x){
u = v;
}
else{
return t[u].val;
}
}
}
int main(){
int n;
io >> n;
Insert(2147483647);
Insert(-2147483647);
R(i,1,n){
int x;
io >> x;
Insert(x);
}
int m;
io >> m;
while(m--){
char str[7];
scanf("%s", str + 1);
if(str[1] == 'a'){
int x;
io >> x;
Insert(x);
++n;
}
else{
if(n & 1){
printf("%d\n", Kth(((n + 1) >> 1) + 1));
}
else{
int ans1 = Kth((n >> 1) + 1);
int ans2 = Kth(((n + 2) >> 1) + 1);
printf("%d\n", Min(ans1, ans2));
}
}
} return 0;
}

Luogu3871 [TJOI2010]中位数 (平衡树)的更多相关文章

  1. luoguP3871 [TJOI2010]中位数

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

  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]中位数】

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

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

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

  6. 洛谷 3871 [TJOI2010]中位数

    [题解] 平衡树模板题,不过因为可以离线,所以有别的做法.把询问倒着做,变成删掉数字.求中位数,于是可以二分+树状数组. #include<cstdio> #include<cstr ...

  7. CDOJ1339 郭大侠与线上游戏(维护一个set中的中位数——平衡树/双set)

    http://acm.uestc.edu.cn/#/problem/show/1339 题意:有三种操作,分别是. 1.向队列推入一个数x. 2.弹出这个队列的第一个数字 3.查询这个队列的中位数是多 ...

  8. TJOI2010中位数

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

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

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

随机推荐

  1. 【Azure 存储服务】Java Azure Storage SDK V12使用Endpoint连接Blob Service遇见 The Azure Storage endpoint url is malformed

    问题描述 使用Azure Storage Account的共享访问签名(Share Access Signature) 生成的终结点,连接时遇见  The Azure Storage endpoint ...

  2. 使用C#和MonoGame开发俄罗斯方块游戏

    小的时候就看到有同学使用C语言在DOS下做过一款俄罗斯方块的游戏,当时是启用了DOS的图形化模式,感觉也挺有意思.最近上海疫情封控在家,周末也稍微有点空余时间,于是使用Visual Studio 20 ...

  3. 关于『Markdown』:第一弹

    关于『Markdown』:第一弹 建议缩放90%食用 声明: 在我之前已有数位大佬发布 "Markdown" 的语法知识点, 在此, 仅整理归类以及补缺, 方便阅读. 感谢 C20 ...

  4. GitHub-SSH密钥获取

    SSH密钥 需要先安装git的客户端,下载: http://git-scm.com/download/ 使用下列步骤完成密钥的添加. 检查系统是否存在密钥 运行 Git Bash, 在弹出的终端中输入 ...

  5. 雪花算法及微服务集群唯一ID解决方案

    雪花算法(SnowFlake) 简介 现在的服务基本是分布式.微服务形式的,而且大数据量也导致分库分表的产生,对于水平分表就需要保证表中 id 的全局唯一性. 对于 MySQL 而言,一个表中的主键 ...

  6. 关于vue打包上线遇到的坑

    打包上线经常会经常遇到路径找不到,页面空白,那么下面我们就解决一下. 第一步.先找到config目录的index.js 改成如上图红框标注所示 第二步.找到build下的utils.js文件 加上如上 ...

  7. Redis布隆过滤器和布谷鸟过滤器

    一.过滤器使用场景:比如有如下几个需求:1.原本有10亿个号码,现在又来了10万个号码,要快速准确判断这10万个号码是否在10亿个号码库中? 解决办法一:将10亿个号码存入数据库中,进行数据库查询,准 ...

  8. Python语法糖,提升编程幸福感!!!

    转载请注明出处️ 作者:测试蔡坨坨 原文链接:caituotuo.top/a52bc938.html 大家好,我是测试蔡坨坨. 今天,我们来盘点一下Python中的那些语法糖. 什么是语法糖?语法糖不 ...

  9. loguru备忘

    loguru是个非常好用的三方日志管理包,简单易用,奈何老是记不住,在这记录一下吧 #coding:utf-8 ''' @version: python3.8 @author: 'eric' @lic ...

  10. 使用xpath查找元素的子元素,找不到

    List<WebElement> allUserList = driver.findElements(By.xpath("xxx")); for (WebElement ...