AC日记——中庸之道 codevs 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之间的整数。
思路:
主席树裸题;
来,上代码:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; struct TreeNodeType {
int l,r,dis; TreeNodeType *left,*right;
};
struct TreeNodeType *root[]; int n,q,if_z,num[],num_[],size; char Cget; inline void read_int(int &now)
{
now=,if_z=,Cget=getchar();
while(Cget>''||Cget<'')
{
if(Cget=='-') if_z=-;
Cget=getchar();
}
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
now*=if_z;
} void tree_build(TreeNodeType *&now,int l,int r)
{
now=new TreeNodeType;
now->l=l,now->r=r,now->dis=;
if(l==r) return ;
int mid=(l+r)>>;
tree_build(now->left,l,mid);
tree_build(now->right,mid+,r);
} void tree_change(TreeNodeType *&pre,int to,TreeNodeType *&now)
{
now=new TreeNodeType;
now->l=pre->l,now->r=pre->r,now->dis=pre->dis+;
if(now->l==now->r) return ;
int mid=(now->l+now->r)>>;
if(to<=mid)
{
now->right=pre->right;
tree_change(pre->left,to,now->left);
}
else
{
now->left=pre->left;
tree_change(pre->right,to,now->right);
}
} int tree_query(TreeNodeType *pre,TreeNodeType *now,int to)
{
if(now->l==now->r) return now->l;
int dis=now->left->dis-pre->left->dis;
if(to<=dis) return tree_query(pre->left,now->left,to);
else return tree_query(pre->right,now->right,to-dis);
} int main()
{
read_int(n),read_int(q);
for(int i=;i<=n;i++)
{
read_int(num[i]);
num_[i]=num[i];
}
sort(num+,num+n+);
size=unique(num+,num+n+)-num-;
tree_build(root[],,size);
for(int i=;i<=n;i++)
{
num_[i]=lower_bound(num+,num+size+,num_[i])-num;
tree_change(root[i-],num_[i],root[i]);
}
int l,r;
for(int i=;i<=q;i++)
{
read_int(l),read_int(r);
int mid=((r-l+)>>)+;
printf("%d\n",num[tree_query(root[l-],root[r],mid)]);
}
return ;
}
AC日记——中庸之道 codevs 2021的更多相关文章
- AC日记——楼房 codevs 2995
2995 楼房 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 地平线(x轴)上有n个矩(lou ...
- AC日记——传话 codevs 1506 (tarjan求环)
1506 传话 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题解 题目描述 Description 一个朋友网络,如果a认识b,那么如果a第 ...
- AC日记——绿色通道 codevs 3342
3342 绿色通道 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description <思远高考绿色通道&g ...
- AC日记——蓬莱山辉夜 codevs 2830
2830 蓬莱山辉夜 时间限制: 1 s 空间限制: 32000 KB 题目等级 : 黄金 Gold 题解 查看运行结果 题目描述 Description 在幻想乡中,蓬莱山辉夜是月球 ...
- AC日记——苹果树 codevs 1228
1228 苹果树 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 在卡卡的房子外面,有一棵 ...
- AC日记——刺激 codevs 1958
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description saffah的一个朋友S酷爱滑雪,并且追求刺激(exitement,由于刺激 ...
- AC日记——红与黑 codevs 2806
2806 红与黑 时间限制: 1 s 空间限制: 64000 KB 题目等级 : 白银 Silver 题解 查看运行结果 题目描述 Description 有一个矩形房间,覆盖正方形瓷 ...
- AC日记——热浪 codevs 1557 (最短路模板题)
1557 热浪 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 德克萨斯纯朴的民眾们这个夏 ...
- AC日记——字典 codevs 4189
4189 字典 时间限制: 1 s 空间限制: 256000 KB 题目等级 : 大师 Master 题解 查看运行结果 题目描述 Description 最经,skyzhong得到了 ...
随机推荐
- python——用递归的方法求x的y次幂
def function(x,y): : : )*x ): number = int(input('请输入x的值:')) y = int(input('请输入y的值:')) print('x的y次幂的 ...
- C语言中strtod()函数的用法详解
函数原型: #include <stdlib.h> double strtod(const char *nptr, char **endptr); C语言及C++中的重要函数. 名称含义 ...
- 水题:HDU-1088-Write a simple HTML Browser(模拟题)
解题心得: 1.仔细读题,细心细心...... 2.题的几个要求:超过八十个字符换一行,<br>换行,<hr>打印一个分割线,最后打印一个新的空行.主要是输出要求比较多. 3. ...
- java中equals和==区别
equals 方法是 java.lang.Object 类的方法. 有两种用法说明: (1)对于字符串变量来说,使用“==”和“equals()”方法比较字符串时,其比较方法不同. “==”比较两个变 ...
- TCP/IP网络编程之多线程服务端的实现(一)
为什么引入线程 为了实现服务端并发处理客户端请求,我们介绍了多进程模型.select和epoll,这三种办法各有优缺点.创建(复制)进程的工作本身会给操作系统带来相当沉重的负担.而且,每个进程有独立的 ...
- STL学习笔记6 -- 栈stack 、队列queue 和优先级priority_queue 三者比较
栈stack .队列queue 和优先级priority_queue 三者比较 默认下stack 和queue 基于deque 容器实现,priority_queue 则基于vector 容器实现 ...
- python2.X中文乱码
在IDE下,加上# -- coding: UTF-8 -- 并且保证IDE也是utf-8编码. 在CMD下,这样执行会有乱码,为啥呢,因为cmd下是gbk编码的,你写的代码必须也是gbk编码的,你可以 ...
- Qt数据库查询之事务操作
在做借书系统的时候,用到了事务操作,不会使用qt中事务操作怎么写,查了一些博客帖子,并不起作用,后来发现,在进行事务成功判断时,出现问题,正确代码如下 if(QSqlDatabase::databas ...
- 一些NGINX配置
一些nginx配置 使用独立目录, 然后include具体配置 gzip on for multi processers static file cache proxy pass 静态目录 or 文件 ...
- [转]jQuery中attr() 和 val() 的区别
[转](http://www.codeproject.com/Tips/780652/Difference-between-attr-and-val-in-jQuery)