codves 2021中庸之道
2021 中庸之道
给定一个长度为N的序列,有Q次询问,每次询问区间[L,R]的中位数。
数据保证序列中任意两个数不相同,且询问的所有区间长度为奇数。
第一行为N,Q。
第二行N个数表示序列。
接下来Q行,每行为L,R,表示一次询问。
输出Q行,对应每次询问的中位数。
5 3
1 4 8 16 2
1 5
3 5
3 3
4
8
8
40%的数据,N,Q≤100;
70%的数据,N≤100;
100%的数据,N≤1000,Q≤100000,序列中的元素为1到10^9之间的整数。
套上主席树求第K大模板
区间[l,r]中位数转化:k=(r-l)/2+1
例:[5,9]中位数是第7个,k=(9-5)/2+1=3,是第3大
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#define N 100001
using namespace std;
int n,m,l_child[N*],r_child[N*],root[N],sum[N*],tot,id;
int a[N],has[N];
int x,y,k,ans,T;
void build(int pre,int &now,int key,int l,int r)
{
sum[now=++id]=sum[pre]+;
if(l==r) return;
int mid=(l+r)/;
if(key<=mid)
{
r_child[now]=r_child[pre];
build(l_child[pre],l_child[now],key,l,mid);
}
else
{
l_child[now]=l_child[pre];
build(r_child[pre],r_child[now],key,mid+,r);
}
}
void discrete()
{
sort(a+,a+n+);
tot=unique(a+,a+n+)-(a+);
for(int i=;i<=n;i++) has[i]=lower_bound(a+,a+tot+,has[i])-a;
}
int search(int s,int t,int k,int l,int r)
{
if(l==r) return l;
int tmp=sum[l_child[t]]-sum[l_child[s]];
int mid=(l+r)/;
if(tmp>=k) return search(l_child[s],l_child[t],k,l,mid);
else return search(r_child[s],r_child[t],k-tmp,mid+,r);
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) scanf("%d",&a[i]),has[i]=a[i];
discrete();
for(int i=;i<=n;i++) build(root[i-],root[i],has[i],,tot);
for(int i=;i<=m;i++)
{
scanf("%d%d",&x,&y);
k=(y-x)/+;//转化
ans=search(root[x-],root[y],k,,tot);
printf("%d\n",a[ans]);
}
}
codves 2021中庸之道的更多相关文章
- codevs 2021 中庸之道
2021 中庸之道 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题目描述 Description 给定一个长度为N的序列,有Q次询问,每次 ...
- AC日记——中庸之道 codevs 2021
2021 中庸之道 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 给定一个长度为N的序列 ...
- 中庸之道(codevs 2021)
题目描述 Description 给定一个长度为N的序列,有Q次询问,每次询问区间[L,R]的中位数. 数据保证序列中任意两个数不相同,且询问的所有区间长度为奇数. 输入描述 Input Descri ...
- Bzoj 1982: [Spoj 2021]Moving Pebbles 博弈论
1982: [Spoj 2021]Moving Pebbles Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 130 Solved: 88[Submi ...
- poj 2021 Relative Relatives(暴力)
题目链接:http://poj.org/problem?id=2021 思路分析:由于数据较小,采用O(N^2)的暴力算法,算出所有后代的年龄,再排序输出. 代码分析: #include <io ...
- codves 3044 矩形面积求并
codves 3044 矩形面积求并 题目等级 : 钻石 Diamond 题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Desc ...
- iPhone屏蔽IOS更新、iPhone系统更新的提示(免越狱,有效期更新至2021年)
iPhone屏蔽IOS更新.iPhone系统更新的提示(免越狱,有效期更新至2021年) 1.在Safari浏览器中粘贴如下链接,按提示打开链接. 输入http://apt.dataage.pub 2 ...
- HDU 2021 发工资咯:)
http://acm.hdu.edu.cn/showproblem.php?pid=2021 Problem Description 作为杭电的老师,最盼望的日子就是每月的8号了,因为这一天是发工资的 ...
- Math.abs(~2020) 按位取反后的绝对值是多少 2021, 按位取反后,比正数多1
Math.abs(~2020) 按位取反后的绝对值是多少 2021, 按位取反后,比正数多1 int 值的取值 范围: -128 --- 127 之间, 0000 0000 按位取 ...
随机推荐
- 项目Beta冲刺(团队)第五天
1.昨天的困难 服务器得不到前端返回的数据 教务处网页的源代码的研究有些困难,有些具体数据还不能得到,jsoup还在探索 2.今天解决的进度 成员 进度 陈家权 点赞界面设计 赖晓连 问答功能各项完善 ...
- 第一个spring冲刺总结
讨论成员:罗凯旋.罗林杰.吴伟锋.黎文衷 第一阶段总体是做到了运算的功能,只是一些基本的功能实现,包括APP进入动画,以及界面的基本效果设计,还有核心算法已经实现(可以计算括号 乘除法等等)“: 燃尽 ...
- 【动态规划】POJ-2385
一.题目 Description It is a little known fact that cows love apples. Farmer John has two apple trees (w ...
- 推荐一个Markdown数学公式编辑器——Haroopad & Mathjax
要在Markdown里插入数学公式,如果没有好用的的引擎or编辑器,那么只能插入图片了,十分麻烦.这里推荐一个十分强大的数学公式引擎--Mathjax. 配置 有道云笔记目前不支持浏览MathJax公 ...
- ACM Shenyang Onsite 2016 题目
A. Thickest Burger 1000ms 262144K ACM ICPC is launching a thick burger. The thickness (or the heig ...
- javascript数据基本定义以及对象{}和数组[]的含义和使用
一.基本的数据类型 原始类型(简单数据类型.基本数据类型) Undefined类型: 表示声明了变量但未对其初始化时赋予该变量的值.undefined为Undefined类型下的唯一的一个值. Nul ...
- angularJS1笔记-(10)-自定义指令(templateUrl属性)
index.html: <!DOCTYPE html> <html lang="en"> <head> <meta charset=&qu ...
- Android bp语法介绍
参考网址: http://note.qidong.name/demo/soong_build/ 谷歌官网文档: https://android.googlesource.com/platform/bu ...
- 一道面试题:StringBuffer a=new StringBuffer ("A"); StringBuffer b=new StringBuffer
前几天又看到这个面试题,再次看看 public class Jtest{ public static void main(String[] args) { StringBuffer a=new Str ...
- 手写简单的promise
function Promise(fn) { var that = this; this.status = "pedding"; this.value = undefined; / ...