博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 2104 K-th Number 划分树
阅读量:4671 次
发布时间:2019-06-09

本文共 3212 字,大约阅读时间需要 10 分钟。

Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 29149   Accepted: 8799
Case Time Limit: 2000MS

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 31 5 2 6 3 7 42 5 34 4 11 7 3

Sample Output

563

Hint

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

Source

, Northern Subregion
 
//第一道划分树题目
#include 
#include
#include
#include
#define lson l,m,lv+1#define rson m+1,r,lv+1using namespace std;#define N 100002struct node{ int num; int v;};int rc[N];node st[20][N];void build(int l,int r,int lv){ if(l==r) return ; int m=(l+r)>>1; int md=rc[m],ll=l,rr=m+1; for(int i=l;i<=r;i++) if(st[lv][i].v<=md) { i==l?st[lv][i].num=1:st[lv][i].num=st[lv][i-1].num+1; st[lv+1][ll++].v=st[lv][i].v; } else { i==l?st[lv][i].num=0:st[lv][i].num=st[lv][i-1].num; st[lv+1][rr++].v=st[lv][i].v; } build(lson); build(rson);}void query(int ql,int qr,int qk,int l,int r,int lv){ if(l==r) { printf("%d\n",st[lv][l].v); return ; } int m=(l+r)>>1; int md=rc[m],lva=st[lv][l+ql-1].num,rva=st[lv][l+qr-1].num; int num=rva-lva; if(st[lv][l+ql-1].v<=md) num++; if(num>=qk) { if(st[lv][l+ql-1].v<=md) query(lva,rva,qk,lson); else query(lva+1,rva,qk,lson); } else { lva=ql-lva; if(st[lv][l+ql-1].v<=md) lva++; rva=qr-rva; query(lva,rva,qk-num,rson); }}int main(){ int n,m; int l,r,k; while(scanf("%d %d",&n,&m)!=EOF) { for(int i=1;i<=n;i++) { scanf("%d",rc+i);st[0][i].v=rc[i];} sort(rc+1,rc+n+1); build(1,n,0); while(m--) { scanf("%d %d %d",&l,&r,&k); query(l,r,k,1,n,0); } } return 0;}

转载于:https://www.cnblogs.com/372465774y/archive/2012/10/08/2715902.html

你可能感兴趣的文章
手把手教您扩展虚拟内存
查看>>
android-samples-mvp
查看>>
oracle 11g r2安装
查看>>
关于自关联1
查看>>
存储控制器、MMU、flash控制器介绍
查看>>
hdu-1814(2-sat)
查看>>
自我反省
查看>>
反射,得到Type引用的三种方式
查看>>
pl sql练习(2)
查看>>
Problem B: 判断回文字符串
查看>>
谷歌浏览器,添加默认搜索引擎的搜索地址
查看>>
数据结构化与保存
查看>>
C# .net 获取程序运行的路径的几种方法
查看>>
为什么需要Docker?
查看>>
国内5家云服务厂商 HTTPS 安全性测试横向对比
查看>>
how to control project
查看>>
转 python新手容易犯的6个错误
查看>>
第四节 -- 列表
查看>>
Python入门学习笔记4:他人的博客及他人的学习思路
查看>>
webstorm里直接调用命令行
查看>>