传送门

模板题一道,1A。

——代码

 #include <cstdio>
#include <algorithm>
#define ls son[now][0], l, mid
#define rs son[now][1], mid + 1, r using namespace std; const int N = ; int n, sz, tot;
int a[N], b[N], sum[ * N], rt[ * N], son[ * N][]; inline void build(int &now, int l, int r)
{
now = ++tot;
if(l == r) return;
int mid = (l + r) >> ;
build(ls);
build(rs);
} inline void update(int &now, int l, int r, int last, int x)
{
now = ++tot;
sum[now] = sum[last] + ;
son[now][] = son[last][];
son[now][] = son[last][];
if(l == r) return;
int mid = (l + r) >> ;
if(x <= mid) update(ls, son[now][], x);
else update(rs, son[now][], x);
} inline int query(int now, int l, int r, int x)
{
if(l == r) return l;
int mid = (l + r) >> , cnt = sum[son[now][]];
if(x <= cnt) return query(ls, x);
else return query(rs, x - cnt);
} int main()
{
int i, k;
scanf("%d", &n);
for(i = ; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i];
sort(b + , b + n + );
sz = unique(b + , b + n + ) - (b + );
build(rt[], , sz);
for(i = ; i <= n; i++)
{
k = lower_bound(b + , b + sz + , a[i]) - b;
update(rt[i], , sz, rt[i - ], k);
if(i % == ) printf("%d\n", b[query(rt[i], , sz, (i + ) >> )]);
}
return ;
}

[luoguP1168]中位数(主席树+离散化)的更多相关文章

  1. Bzoj 2588: Spoj 10628. Count on a tree 主席树,离散化,可持久,倍增LCA

    题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2588 2588: Spoj 10628. Count on a tree Time Limit ...

  2. [HDU4417]Super Mario(主席树+离散化)

    传送门 又是一道主席树模板题,注意数组从0开始,还有主席树耗费空间很大,数组开大点,之前开小了莫名其妙TLE.QAQ ——代码 #include <cstdio> #include < ...

  3. [luoguP2617] Dynamic Ranking(树状数组 套 主席树 + 离散化)

    传送门 BZOJ上是权限题,洛谷赞啊. 求区间 K 大数很简单. 但是如果修改某个数的话,那么就得把这个数及后面所建的主席树都更新一遍 nlogn,显然不行. 所以可以在外面套一个树状数组来优化,树状 ...

  4. FZU 2237 中位数 主席树 树上k大

    #include <cstdio> #include <cstring> #include <queue> #include <set> #includ ...

  5. BZOJ3932 CQOI2015 任务查询系统 - 主席树,离散化

    记录下自己写错的地方吧 1. 区间可能有重复 2. 没有出现的坐标也要计入version (因为询问里可能会有) #include <bits/stdc++.h> using namesp ...

  6. SPOJ 3267 D-query(离散化+主席树求区间内不同数的个数)

    DQUERY - D-query #sorting #tree English Vietnamese Given a sequence of n numbers a1, a2, ..., an and ...

  7. SDUT 2610 Boring Counting(离散化+主席树区间内的区间求和)

    Boring Counting Time Limit: 3000MS Memory Limit: 65536KB Submit Statistic Discuss Problem Descriptio ...

  8. HDU 4417 Super Mario(主席树求区间内的区间查询+离散化)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  9. POJ 2104&HDU 2665 Kth number(主席树入门+离散化)

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

随机推荐

  1. spring boot 的redis 之初理解

    项目到末尾了快, 这几天安排我结合业务场景给项目加上redis 缓存, 我接到这个任务也是懵逼了一会儿: 问了一句让我自己先想办法,没办法硬着头皮查吧, 要不不得不说spring boot 还是好用, ...

  2. Java并发——结合CountDownLatch源码、Semaphore源码及ReentrantLock源码来看AQS原理

    前言: 如果说J.U.C包下的核心是什么?那我想答案只有一个就是AQS.那么AQS是什么呢?接下来让我们一起揭开AQS的神秘面纱 AQS是什么? AQS是AbstractQueuedSynchroni ...

  3. Android学习笔记--Intent

    Intent是android四大组件之间交互的一种重要方式.Intent可以指明当前要执行的动作,也可以指明要传递的数据.Intent可以用来启动活动,启动服务,发送广播. Intent分为两种:1. ...

  4. 安装nginx的一些注意事项

    1.如何彻底屏蔽掉Nginx的banner 为了安全或者某些个人的原因,如果要屏蔽掉nginx的banner,要修改以下几个位置: src/http/ngx_http_header_filter_mo ...

  5. 在Eclipse中设置自动补全

    在Eclipse中菜单中,Window->Preferences->Java->Editor->Content Assist中的Auto activation triggers ...

  6. CPP-基础:关于私有成员的访问

    a.C++的类的成员函数中,允许直接访问该类的对象的私有成员变量. b.在类的成员函数中可以访问同类型实例的私有变量. c.拷贝构造函数里,可以直接访问另外一个同类对象(引用)的私有成员. d.类的成 ...

  7. QT_5_ Qt中信号和槽 + 自定义信号和槽 + lambda 表达式

    1.Qt中信号和槽 1.1 需求:点击按钮关闭窗口 1.2 利用connect进行链接 1.3 参数1 信号发送者(指针) 参数2 发送的信号(信号地址) 参数3 信号的接受者(指针) 参数4 处理槽 ...

  8. EBS ORACLE工单齐套率的计算程序

    PROCEDURE Get_wip_accept_item_date(p_use_id in number, p_org_id IN NUMBER, p_start_date IN DATE, p_e ...

  9. activiti工作流学习链接

    首页: http://www.activiti.org/书籍: activiti in action  入门demo: kft-activiti-demo   http://www.oschina.n ...

  10. 【mybatis】mybatis数据源源码剖析(JNDI、POOLED、UNPOOLED)

    一.概述 二.创建 mybatis数据源的创建过程稍微有些曲折. 1. 数据源的创建过程: 2. mybatis支持哪些数据源,也就是dataSource标签的type属性可以写哪些合法的参数? 弄清 ...