博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spoj MKTHNUM - K-th Number
阅读量:5096 次
发布时间:2019-06-13

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

题目描述

  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.

输入输出格式

输入格式:

 

The first line of the input contains n — the size of the array, and m — the number of questions to answer (1 ≤ n ≤ 100000, 1 ≤ m ≤ 5000).

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).

SAMPLE INPUT7 31 5 2 6 3 7 42 5 34 4 11 7 3

 

输出格式:

 

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

Note : naive solution will not work!!!

 

 

打个版,留给以后复制粘贴2333

#include
#include
#include
#include
#include
#include
#define ll long long#define maxn 100005using namespace std;struct node{ node *lc,*rc; int s;}nil[maxn*30],*rot[maxn],*cnt;int a[maxn],num[maxn],n,ky;int m,le,ri,k;char ch;node *update(node *u,int l,int r){ node *ret=++cnt; *ret=*u; ret->s++; if(l==r) return ret; int mid=l+r>>1; if(le<=mid) ret->lc=update(ret->lc,l,mid); else ret->rc=update(ret->rc,mid+1,r); return ret;}int query(node *u,node *v,int l,int r){ if(l==r) return num[l]; int mid=l+r>>1,c=v->lc->s-u->lc->s; if(k<=c) return query(u->lc,v->lc,l,mid); else{ k-=c; return query(u->rc,v->rc,mid+1,r); }}inline void prework(){ cnt=rot[0]=nil->lc=nil->rc=nil; nil->s=0; for(int i=1;i<=n;i++){ le=a[i]; rot[i]=update(rot[i-1],1,ky); }}inline void solve(){ while(m--){ scanf("%d%d%d",&le,&ri,&k); printf("%d\n",query(rot[le-1],rot[ri],1,ky)); }}int main(){ scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) scanf("%d",a+i),num[i]=a[i]; sort(num+1,num+n+1); ky=unique(num+1,num+n+1)-num-1; for(int i=1;i<=n;i++) a[i]=lower_bound(num+1,num+ky+1,a[i])-num; prework(); solve(); return 0;}

  

转载于:https://www.cnblogs.com/JYYHH/p/8467353.html

你可能感兴趣的文章
Vue音乐项目笔记(三)
查看>>
遍历Map对象
查看>>
计算剪贴板里仿制的代码行数
查看>>
MySQL索引背后的数据结构及算法原理
查看>>
#Leetcode# 209. Minimum Size Subarray Sum
查看>>
C#语言-04.OOP基础
查看>>
1)session总结
查看>>
PHP深浅拷贝
查看>>
SDN第四次作业
查看>>
ActiveMQ(4) ActiveMQ JDBC 持久化 Mysql 数据库
查看>>
DM8168 DVRRDK软件框架研究
查看>>
django迁移数据库错误
查看>>
epoll学习01
查看>>
yii 跳转页面
查看>>
闭包问题
查看>>
C#一个FTP操作封装类FTPHelper
查看>>
Linux运维基础入门(二):网络基础知识梳理02
查看>>
洛谷 1449——后缀表达式(线性数据结构)
查看>>
[最小割][Kruskal] Luogu P5039 最小生成树
查看>>
Data truncation: Out of range value for column 'Quality' at row 1
查看>>