POJ2104-K-th Number(静态主席树模板 区间第k小)

题目链接

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


题意

给n个数,求区间 [ i , j ] 的第k小是多少

思路

主席树思路的理解:https://blog.csdn.net/creatorx/article/details/75446472

主席树的图示:https://blog.csdn.net/a_forever_dream/article/details/80450549

求第k小值,是权值线段树的作用,权值线段树与线段树区别是,保存的是数出现的次数

 

如果用sort排序,用前缀和的思想,只建立n棵权值线段树。如果知道[1,l - 1 ] 和[1,r ]区间比k小的个数,相减就是[l,r]区间比k小的个数,因为结点维护的区间范围是一样的

n棵线段树空间复杂度O(n*n*logn),仍然太大,但是每次第i棵线段树和第i+1棵线段树的区别其实只有一条链不同,所以可用重复利用相同的部分,节省空间。

盗上面链接一张图...

每次的根都是不一样的,但是根都复制了前面根的左右子树的编号,如果需要修改就再新建,不需要修改就复用之前的。

主席树的建树空间复杂度O(nlogn),建树时间复杂度O(nlogn),访问时间复杂度O(logn)。

线段树空间复杂度O(n*4),时间复杂度一样。

 

主席树可以理解为,多棵权值线段树的空间优化。


#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
const int N = 1e5+10;

struct Tree
{
    int l,r,sum;
}tr[N*20];

struct node
{
    int x,id;
}a[N];
bool cmp(node v1,node v2){return v1.x<v2.x;}

int rk[N],root[N],cnt;

void update(int &k,int l,int r,int v)
{
    tr[cnt] = tr[k]; tr[cnt].sum++;
    k = cnt++;
    if(l==r) return;
    int mid = (l+r)/2;
    if(v<=mid) update(tr[k].l,l,mid,v);
    else update(tr[k].r,mid+1,r,v);
}

int query(int L,int R,int l,int r,int k)
{

    if(l==r) return l;
    int d = tr[tr[R].l].sum - tr[tr[L].l].sum;
    int mid = (l+r)/2;
    if(k<=d) return query(tr[L].l,tr[R].l,l,mid,k);
    else return query(tr[L].r,tr[R].r,mid+1,r,k-d);
}

int main()
{
    int n,m; scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++){
        scanf("%d",&a[i].x);
        a[i].id = i;
    }
    sort(a+1,a+1+n,cmp);
    for(int i=1;i<=n;i++) rk[a[i].id] = i;

    cnt = 1;

    for(int i=1;i<=n;i++){
        root[i] = root[i-1];
        update(root[i],1,n,rk[i]);
    }

    for(int i=1;i<=m;i++){
        int x,y,v; scanf("%d%d%d",&x,&y,&v);
        int t = query(root[x-1],root[y],1,n,v);
        printf("%d\n",a[t]);
    }
    return 0;
}

 

内容概要:本文详细介绍了施耐德M580系列PLC的存储结构、系统硬件架构、上电写入程序及CPU冗余特性。在存储结构方面,涵盖拓扑寻址、Device DDT远程寻址以及寄存器寻址三种方式,详细解释了不同类型的寻址方法及其应用场景。系统硬件架构部分,阐述了最小系统的构建要素,包括CPU、机架和模块的选择与配置,并介绍了常见的系统拓扑结构,如简单的机架间拓扑和远程子站以太网菊花链等。上电写入程序环节,说明了通过USB和以太网两种接口进行程序下载的具体步骤,特别是针对初次下载时IP地址的设置方法。最后,CPU冗余部分重点描述了热备功能的实现机制,包括IP通讯地址配置和热备拓扑结构。 适合人群:从事工业自动化领域工作的技术人员,特别是对PLC编程及系统集成有一定了解的工程师。 使用场景及目标:①帮助工程师理解施耐德M580系列PLC的寻址机制,以便更好地进行模块配置和编程;②指导工程师完成最小系统的搭建,优化系统拓扑结构的设计;③提供详细的上电写入程序指南,确保程序下载顺利进行;④解释CPU冗余的实现方式,提高系统的稳定性和可靠性。 其他说明:文中还涉及一些特殊模块的功能介绍,如定时器事件和Modbus串口通讯模块,这些内容有助于用户深入了解M580系列PLC的高级应用。此外,附录部分提供了远程子站和热备冗余系统的实物图片,便于用户直观理解相关概念。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值