Poj 2104 K-th Number

POJ 2104 K-th Number ——划分树 POJ 2104 题解 ——划分树 本文是一个系列的第一篇文章。本系列基于上交老师出品的《程序设计解题策略》书籍。该书介绍了49个ACM/ICPC算法设计策略。本系列打算用一个更简单的方式介绍书中的解题策略。当然,针对每个策略,也会给出相应的测试题目及其代码。 文章目录POJ 2104 题解 ——划分树划分树算法问题与复杂度建树查找完整AC代码 划分树算法 问题与复杂度 划分树主要应用于求解整数... 阅读详情

K-th Number

Time Limit:20000MS

 

Memory Limit:65536K

Total Submissions:28294

 

Accepted:8434

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

 

标准的划分树:


#include<stdio.h>
#include<algorithm>
using namespace std;

#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define MAXN 110000

int sorted[MAXN];
int val[20][MAXN];
int toL[20][MAXN];
int n,m;

void build(int l,int r,int rt,int d)
{
    if(l==r)
    {
        return ;
    }
    int i,count,mid=(l+r)>>1;
    int tmp=sorted[mid];
    count=mid-l+1;
    for(i=l;i<=r;i++)
    {
        if(val[d][i]<tmp)
            count--;
    }
    int lpos=l,rpos=mid+1;
    toL[d][0]=0;
    for(i=l;i<=r;i++)
    {
        if(val[d][i]<tmp)
        {
            val[d+1][lpos++]=val[d][i];
            toL[d][i]=toL[d][i-1]+1;
        }
        else if(val[d][i]>tmp)
        {
            val[d+1][rpos++]=val[d][i];
            toL[d][i]=toL[d][i-1];
        }
        else if(count>0)
        {
            val[d+1][lpos++]=val[d][i];
            toL[d][i]=toL[d][i-1]+1;
            count--;
        }
        else
        {
            val[d+1][rpos++]=val[d][i];
            toL[d][i]=toL[d][i-1];
        }

    }
    build(lson,d+1);
    build(rson,d+1);
}

int query(int L,int R,int k,int l,int r,int rt,int d)
{
    if(l==r)
        return val[d][L];
    int s1;
    int s2;
    if(L!=l)
    {
        s1=toL[d][R]-toL[d][L-1];
        s2=toL[d][L-1]-toL[d][l-1];
    }
    else
    {
        s1=toL[d][R]-toL[d][L-1];
        s2=0;
    }
    int new_L,new_R;
    int mid=(l+r)>>1;
    if(k<=s1)
    {
        new_L=l+s2;
        new_R=l+s1+s2-1;
        return query(new_L,new_R,k,lson,d+1);
    }
    else
    {
        k=k-s1;
        s1=(R-L+1)-s1;
        s2=L-l-s2;
        new_L=(mid+1)+s2;
        new_R=(mid+1)+s1+s2-1;
        return query(new_L,new_R,k,rson,d+1);
    }

}
int main(void)
{
//    freopen("d:\\in.txt","r",stdin);
    while(scanf("%d%d",&n,&m)==2)
    {
       
        int i;
        for(i=1;i<=n;i++)
        {
            scanf("%d",&val[0][i]);
            sorted[i]=val[0][i];
        }
        sort(sorted+1,sorted+1+n);
        build(1,n,1,0);
        int a,b,k;
        while(m--)
        {
            scanf("%d%d%d",&a,&b,&k);
            printf("%d\n",query(a,b,k,1,n,1,0));
        }
    }
    return 0;
}


从SolidWorks到Simulink:手把手教你用URDF文件搭建六轴机械臂仿真模型(含STL文件配置避坑) 本文详细介绍了从SolidWorks到Simulink的六轴机械臂URDF仿真全流程,包括URDF文件导出、Simscape模型生成与调试、动力学参数获取与验证等关键步骤。重点解决了STL文件配置、关节接口设置等工程化难题,帮助开发者高效构建可控制的数字孪生体。通过实战案例和优化技巧,提升机械臂仿真效率与准确性。 阅读详情

相关推荐

ethercat linux lan9252 - lan9253调试

linux 的ethercat测试

linsheng_111的专栏 1931

POJ 2104 K-th Number (划分树)

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 46308   Accepted: 15433 Case Time Limit: 2000MS Description You are working for Macrohard c

aozil_yang的博客 804

玩转 Milvus(二):在 Ubuntu 22.04(WSL2)上安装 Milvus

本文详细介绍了在Ubuntu 22.04(WSL2)环境下安装Milvus向量数据库的两种单机模式:Milvus Lite和Milvus Standalone。Milvus Lite作为轻量级Python库,适合快速学习和小规模实验,只需通过pip安装即可使用。Milvus Standalone则支持Docker和Docker Compose两种部署方式,适合开发测试或小规模生产环境。文章对比了不同安装方式的优缺点,并提供了验证案例和常见问题解答,帮助开发者快速上手,实现简单的语义搜索功能。

企业级后端与 AI 应用开发双线探索 2506

【POJ 2104】K-th Number 题意&题解&代码(c++)

K-th NumberTime Limit: 20000MS Memory Limit: 65536K Case Time Limit: 2000MSDescriptionYou are working for Macrohard company in data structures department. After failing your previous task about ke

曾经追梦的happy时光 808

从零构建个人知识库:Obsidian与Git实践打造可生长的第二大脑

在信息爆炸的时代,如何有效管理个人知识资产,构建一个可检索、可关联、可生长的个人知识库,成为提升学习效率和创造力的关键。其核心原理在于通过系统化的信息架构与工作流设计,将碎片化信息转化为结构化的知识网络。从技术价值看,这不仅是笔记方法的升级,更是个人知识管理(PKM)和思维模式的革新,能有效对抗信息过载与知识孤岛。实践中,以Obsidian为代表的本地Markdown笔记软件,结合双向链接和关系图谱功能,为知识网络的构建提供了强大支撑;而引入Git进行版本控制,则确保了知识资产的历史可追溯性与安全备份。这种

weixin_28836989的博客 301

Rasa Core开发指南

文章目录1. Rasa Core简介1.1 Rasa Core消息处理流程1.2 安装Rasa Core2. Dialogue模型训练2.1 Story样本数据2.2 Domain2.2.0 intents2.2.1 actions2.2.2 templates2.2.3 entities2.2.4 slots3 .训练和使用对话模型3.1 训练对话模型3.2 使用对话模型4. 搭建CustomA...

老蒋也疯狂 1万+

poj 2104 K-th Number 主席树+超级详细解释

传送门:K-th Number题目大意:给出一段数列,让你求[L,R]区间内第几大的数字!在这里先介绍一下主席树! 如果想了解什么是主席树,就先要知道线段树,主席树就是n棵线段树,因为线段树只能维护最大值或者最小值,要想求出第二大的数字怎么办呢?两颗线段树呗!好,那么第n大呢,就可以构造n棵线段树,这样的内存是显然会爆掉的,那么怎么办呢?因为每一次更新都是更新的是从叶子节点到根节点的一条路,路的长

woshinannan的专栏 5265

Dr_can Kalman Filter学习笔记(一)

本文是在b站Dr_can的卡尔曼滤波课程学习过程中的学习笔记

苏大侠的博客 1051

POJ 2104 K-th Number【划分树】

http://poj.org/problem?id=2104POJ 2104 K-th Number大意:有n个数字排成一列,有m个询问,格式为:left right k即问在区间[left,right]第k大的数据为多少? 分析:划分树具体的介绍开心->OK介绍得很清楚,这里把他PPT里的部分贴上来,邪恶一把,嘿嘿。。建图:建树的过程比较简单,对于区间[l,r],首先通过对原数...

a48634331的博客 195

COMSOL介电弹性体数值仿真指南:预拉伸圆形DEA的稳态变形分析(II)

本期博客是《COMSOL介电弹性体数值仿真指南》系列博客中的一期。聚焦于介电弹性体驱动器(DEA)中最为经典的预拉伸圆形DEA驱动器构型,对其在COMSOL平台中的具体建模步骤进行介绍与讨论。

忘了面孔的Batou 353

CONN工具箱分析fmri数据笔记1

前言一、安装二、预处理三、去噪 前言 CONN:functional connectivity toolbox,是基于MATLAB和SPM的一个对功能磁共振数据进行分析的一个工具箱。可以进行以下分析: Seed-based correlations (SBC), ROI-to-ROI correlations, graph analyses, Independent component analyses(group-ICA), Masked (ICA) Amplitude of Low-Frequency.

weixin_52073820的博客 6585

【主席树_模板题】POJ 2104 K-th Number

主席树_模板题【查询区间第k小】

L_X_ 292

华为嵌入式面试题及参考答案(持续更新)

异步是指在程序执行过程中,某些任务不需要等待立即完成就可以继续执行其他任务的过程。在计算机编程中,异步通常用于处理长时间运行的操作,如文件读写、网络通信等,以避免阻塞主线程。异步处理通常涉及到回调函数、事件循环、协程或未来的概念。使用宏定义来避免命名冲突。#else#endif堆和栈是计算机程序中用于存储数据的两种不同类型的内存区域。

大模型大数据攻城狮的专栏 2480

POJ2104 K-th Number —— 划分树(模板题)

题目链接:http://poj.org/problem?id=2104. K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 59744   Accepted: 20847 Case Time Limit: 2000MS

A man can be destroyed, but not defeated. 459

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

lwip-2.0.3

lwip是瑞典计算机科学院(SICS)的Adam Dunkels 开发的一个小型开源的TCP/IP协议栈。实现的重点是在保持TCP协议主要功能的基础上减少对RAM 的占用。

POJ2104 K-th Number (划分树模板)

POJ2104 K-th Number (题目链接) 求区间第k大 /************************************************************************* File Name: A.cpp ID: obsoles1 PROG: LANG: C++ Mail: 38

obsolescence的博客 349

复分析 阿尔福斯(Ahlfors) 第三版答案 英文

答案分为十章,有部分习题没有答案,全英文复分析阿尔福斯(Ahlfors) 第三版答案

POJ-2104-K-th Number

POJ-2104-K-th Number http://poj.org/problem?id=2104 划分树的基本思想就是对于某个区间,把它划分成两个子区间,左边区间的数小于右边区间的数。查找的时候通过记录进入左子树的数的个数,确定下一个查找区间,最后范围缩小到1,就找到了。 建树的过程比较简单,对于区间[l,r],首先通过对原数组的排序找到这个区间的中位数a[mid],小于a[mid]的

comeace的程序人生 847

poj 2104 K-th Number

类型:划分树【学习资料】 题目:http://poj.org/problem?id=2104 来源:Northeastern Europe 2004, Northern Subregion 思路    代码源自:http://blog.sina.com.cn/s/blog_5f5353cc0100ki2e.html  http://blog.youkuaiyun.com/zxy_sno

gold_bridge 371

POJ 2104 K-th Number

POJ 2104 K-th Number 题目链接 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 sta

旺崽的博客 2674

poj 2104 K-th Number(划分线段树)

//poj 2104 K-th Number(划分线段树) #include #include #include using namespace std; const int MAXN=100100; int st[MAXN];//对所存序列进行排序(取基准元素所用) struct node { int l,r; }tree[MAXN*4];//建立线段树所用空间 int arrsub[3

u014068781的专栏 422

【poj2104】K-th Number 分块

DescriptionYou 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 r

DQS的树洞 1321

POJ2104 K-th Number —— 区间第k小 整体二分

题目链接:https://vjudge.net/problem/POJ-2104 K-th Number Time Limit:20000MS Memory Limit:65536K Total Submissions:64110 Accepted:22556 Case Time Limit:2000MS ...

alince20008的博客 111

POJ2104K-th Number

K-th Number Time Limit: 20000MS Memory Limit: 65536K Case Time Limit:2000MS Description You are working for Macrohard company in data structures department. After failing your previous t

Mrazer 494

[POJ 2104] K-th Number

[题目链接] http://poj.org/problem?id=2104 [算法] 考虑离线 ,将询问按右端点排序 对于每个询问(l , r , k) , 若我们知道区间[l , r]中 <= x的数有多少个 , 就可以通过二分解决 进一步 , 我们可以将区间[l , r]中 <= x 的数的个数...

103
上一篇: Hdu 1213 How Many Tables
下一篇: Hdu 4251 The Famous ICPC Team Again
Magic_____
博客等级 码龄14年 11粉丝 160原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值