poj 2104 K-th Number(划分树)

本文介绍了一种使用划分树解决K-thNumber问题的方法,该问题要求在给定数组的指定区间内找出第k大的数。文章详细解释了划分树的构建和查询过程,并附带实现代码。
K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 28725 Accepted: 8602
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 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

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

题意:给你一个数列,每次询问一个区间内第k大的数。。。

分析:这题有多种搞法,不过我没去多想,主要是为了学划分树才做这题的,所有,如果会划分树的话,这个直接就是个模板题了。。。

PS:在网上找关于划分树的资料,结果没找到合适的,最后找了个代码研究了下,才明白,划分树是个简单的东西。。。

划分树有两个步骤,建树和查询:

建树比较简单,就是对于每个区间,把小于等于它的中位数的数移到左边构成左子树,其余的构成右子树,(注意数的前后位置不能调换,查询时用到)

接下来就是如何查询了,假设要查询区间 [ L,R ]的第k大,当前查找到划分树的 [ l,r ] 子树,那么首先要计算两个值,一个w1为 区间 [ l, L-1 ]中排在[ l,r ] 的左子树中的个数,

一个w2为 区间 [ L, R ]中排在[ l,r ] 的左子树中的个数,那么如果k大于 w2的话,第k大的数肯定在右子树,否则在左子树。

如果在左子树的话,那么区间 [ L,R ]相应的变成 [ l +w1, l+w1+w2-1 ],而k不变。。。(因为数的摆放顺序相对不变,所以区间移到左子树时,前面肯定有w1个数了,而区间只关心移过去的w2个数)

如果在右子树,相应的,区间变成 [ m+w1+1,m +w1+w2 ]具体与左子树相同


代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int mm=111111;
int a[mm],s[22][mm],g[22][mm];
int i,j,k,n,m;
void build(int l,int r,int d)
{
    if(l==r)return;
    int i,t1,t2,same=0,m=(l+r)>>1;
    for(i=l;i<=r;++i)
        g[d][i]=0,same+=(s[d][i]<a[m]);
    same=m-l+1-same;
    for(t1=i=l,t2=m+1;i<=r;++i)
        if(s[d][i]==a[m]&&same)--same,s[d+1][t1++]=s[d][i],g[d][i]=1;
        else if(s[d][i]<a[m])
            s[d+1][t1++]=s[d][i],g[d][i]=1;
        else s[d+1][t2++]=s[d][i];
    for(i=l;i<r;++i)
        g[d][i+1]+=g[d][i];
    build(l,m,d+1);
    build(m+1,r,d+1);
}
int query(int L,int R,int k,int l,int r,int d)
{
    if(l==r)return s[d][l];
    int m=(l+r)>>1,w1=0,w2=g[d][R];
    if(L>l)w2-=(w1=g[d][L-1]);
    if(w2>=k)return query(l+w1,l+w1+w2-1,k,l,m,d+1);
    k-=w2;
    w1=L-l-w1;
    w2=R-L+1-w2;
    return query(m+w1+1,m+w1+w2,k,m+1,r,d+1);
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(i=1;i<=n;++i)
        {
            scanf("%d",&a[i]);
            s[0][i]=a[i];
        }
        sort(a+1,a+n+1);
        build(1,n,0);
        while(m--)
        {
            scanf("%d%d%d",&i,&j,&k);
            printf("%d\n",query(i,j,k,1,n,0));
        }
    }
    return 0;
}


混合动力汽车(HEV)模型的Simscape模型(Matlab代码、Simulink仿真实现)内容概要:本文档介绍了一个混合动力汽车(HEV)的Simscape模型,该模型通过Matlab代码和Simulink仿真工具实现,旨在对混合动力汽车的动力系统进行建模与仿真分析。模型涵盖了发动机、电机、电池、传动系统等关键部件,能够模拟车辆在不同工况下的能量流动与控制策略,适用于动力系统设计、能耗优化及控制算法验证等研究方向。文档还提及该资源属于一个涵盖多个科研领域的MATLAB仿真资源包,涉及电力系统、机器学习、路径规划、信号处理等多个技术方向,配套提供网盘下载链接,便于用户获取完整资源。; 适合人群:具备Matlab/Simulink使用基础的高校研究生、科研人员及从事新能源汽车系统仿真的工程技术人员。; 使用场景及目标:①开展混合动力汽车能量管理策略的研究与仿真验证;②学习基于Simscape的物理系统建模方法;③作为教学案例用于车辆工程或自动化相关课程的实践环节;④与其他优化算法(如智能优化、强化学习)结合,实现控制策略的优化设计。; 阅读建议:建议使用者先熟悉Matlab/Simulink及Simscape基础操作,结合文档中的模型结构逐步理解各模块功能,可在此基础上修改参数或替换控制算法以满足具体研究需求,同时推荐访问提供的网盘链接获取完整代码与示例文件以便深入学习与调试。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值