【POJ】2104 K-th Number(区间k大+主席树)

本文介绍了一种使用主席树解决区间k-th问题的方法,通过预处理和更新操作,可以快速查询给定区间内第k小的元素。具体实现包括了节点结构定义、更新和查询操作,并附带完整代码及样例输入输出。

http://poj.org/problem?id=2104

裸题不说。主席树水过。

#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define dbg(x) cout << #x << "=" << x << endl
#define read(x) x=getint()
#define for1(i, a, n) for(int i=a; i<=(n); ++i)
#define MID (l+r)>>1
inline int getint() { char c; int ret=0, k=1; for(c=getchar(); c<'0' || c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0' && c<='9'; c=getchar()) ret=ret*10+c-'0'; return ret*k; }

const int N=100010;
int id[N], a[N], b[N], n, m, root[N], cnt, key;
struct node { int l, r, s; } T[N*50];
void update(const int &l, const int &r, int &pos) {
	T[++cnt]=T[pos]; pos=cnt; ++T[pos].s;
	if(l==r) return;
	int m=MID;
	if(key<=m) update(l, m, T[pos].l); else update(m+1, r, T[pos].r);
}
int query(const int &l, const int &r, const int &x, const int &y, const int &k) {
	if(l==r) return l;
	int m=MID, s=T[T[y].l].s-T[T[x].l].s;
	if(k<=s) return query(l, m, T[x].l, T[y].l, k); else return query(m+1, r, T[x].r, T[y].r, k-s);
}
bool cmp(const int &x, const int &y) { return a[x]<a[y]; }
int main() {
	read(n); read(m);
	for1(i, 1, n) read(a[i]), id[i]=i;
	sort(id+1, id+1+n, cmp);
	for1(i, 1, n) b[id[i]]=i;
	for1(i, 1, n) {
		root[i]=root[i-1]; key=b[i];
		update(1, n, root[i]);
	}
	int x, y;
	while(m--) {
		read(x); read(y); read(key);
		printf("%d\n", a[id[query(1, n, root[x-1], root[y], key)]]);
	}
	return 0;
}

 

 


 

 

Description

You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.

Input

The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 10 9 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).

Output

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

Sample Input

7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Sample Output

5
6
3

Hint

This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.

Source

Northeastern Europe 2004, Northern Subregion

转载于:https://www.cnblogs.com/iwtwiioi/p/3923809.html

无界云图(开源在线图片编辑器源码)是由四川爱趣五科技推出的一款类似可画、创客贴、图怪兽的在线图片编辑器。该项目采用了React Hooks、Typescript、Vite、Leaferjs等主流技术进行开发,旨在提供一个开箱即用的图片编辑解决方案。项目采用 MIT 协议,可免费商用。 无界云图提供了一系列强的图片编辑功能,包括但不限于: 素材管理:支持用户上传、删除和批量管理素材。 操作便捷:提供右键菜单,支持撤销、重做、导出图层、删除、复制、剪切、锁定、上移一层、下移一层、置顶、置底等操作。 保存机制:支持定时保存,确保用户的工作不会丢失。 主题切换:提供黑白主题切换功能,满足不同用户的视觉偏好。 多语言支持:支持多种语言,方便全球用户使用。 快捷键操作:支持快捷键操作,提高工作效率。 产品特色 开箱即用:无界云图采用了先进的前端技术,用户无需进行复杂的配置即可直接使用。 免费商用:项目采用MIT协议,用户可以免费使用和商用,降低了使用成本。 技术文档齐全:提供了详细的技术文档,包括技术文档、插件开发文档和SDK使用文档,方便开发者进行二次开发和集成。 社区支持:提供了微信技术交流群,用户可以在群里进行技术交流和问题讨论。 环境要求 Node.js:需要安装Node.js环境,用于运行和打包项目。 Yarn:建议使用Yarn作为包管理工具,用于安装项目依赖。 安装使用 // 安装依赖 yarn install // 启动项目 yarn dev // 打包项目 yarn build 总结 无界云图是一款功能强且易于使用的开源在线图片编辑器。它不仅提供了丰富的图片编辑功能,还支持免费商用,极地降低了用户的使用成本。同时,详细的文档和活跃的社区支持也为开发者提供了便利的二次开发和集成条件。无论是个人用户还是企业用户,都可以通过无界云图轻
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值