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

【解题报告】

查询区间第k小。
主席树其实相当于建立了n棵线段树,第i棵线段树是根据区间[1,i]按值建立的。对于每一棵线段树我们记录它对应的区间每个数出现的次数,所以首先要对所有的数离散化。
先考虑最简单的情况,只查询[1,n]的第k小,对于[1,n]我们按值建立一棵线段树,对于a[i]我们在位置a[i]上加1。查询第k小那么先看左子区间出现了多少个数cnt,假设左区间出现的数cnt>=k,那么直接递归到左区间查询(因为是按值建立的,左区间的数肯定小于右区间),否则递归到右区间查询第k-cnt小(左区间已经有了最小的cnt个数了)
对于任意区间查询[l,r],我们只需要比较第l-1棵线段树和第r棵线段树,[l,r]之间的数就是第r棵线段树相比于第l-1棵多出来的数。只需要对比两颗树同一个节点,对比到哪个数为止第r棵比第l-1棵刚好多出k个数。(先比较左区间cntr-cntl,cntr-cntl>=k,则递归到左区间,否则递归查询右区间k-cntr-cntl)
主席树就相当于n棵线段树,但是对比建立在[1,i]的线段树和[1,i+1]的线段树,只多出了一个值,也就是相当于单点更新他们之间只有logn个节点是不同的,所以可以将[1,i+1]的一些节点指针指向前一棵的共同部分。每次新增的空间只需要logn。

代码如下:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 100010

int n,m,cnt,b[N],cpy[N],now,root[N];
struct Tree
{
    int l,r,cnt;
}tree[N*20];
void pushup(int pos)
{
    tree[pos].cnt=tree[tree[pos].l].cnt+tree[tree[pos].r].cnt;
}
int build(int l,int r)
{
    int pos=now++;
    if(l==r) return pos;
    int mid=(l+r)>>1;
    tree[pos].l=build(l,mid);
    tree[pos].r=build(mid+1,r);
    pushup(pos);
    return pos;
}
int insert(int o,int l,int r,int arr)
{
    int pos=now++;
    tree[pos]=tree[o];
    if(l==arr&&r==arr)
    {
        tree[pos].cnt++;
        return pos;
    }
    int mid=(l+r)>>1;
    if(arr<=mid)
        tree[pos].l=insert(tree[o].l,l,mid,arr);
    else 
        tree[pos].r=insert(tree[o].r,mid+1,r,arr);
    pushup(pos);
    return pos;
}
int query(int l,int r,int o,int v,int kth)
{
    if(l==r) return l;
    int mid=(l+r)>>1,res=tree[tree[v].l].cnt-tree[tree[o].l].cnt;
    if(kth<=res) return query(l,mid,tree[o].l,tree[v].l,kth);
    else return query(mid+1,r,tree[o].r,tree[v].r,kth-res);
}
int main()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&b[i]);
        cpy[i]=b[i];    
    }
    sort(cpy+1,cpy+1+n);
    cnt=unique(cpy+1,cpy+1+n)-cpy-1;
    root[0]=build(1,cnt);
    for(int i=1;i<=n;i++)
    root[i]=insert( root[i-1],1,cnt,lower_bound( cpy+1,cpy+1+cnt,b[i] )-cpy );
    while(m--)
    {
        int xx,yy,zz;
        scanf("%d%d%d",&xx,&yy,&zz);
        printf("%d\n",cpy[query(1,cnt,root[xx-1],root[yy],zz)]);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
POJ1753题目为"Flip Game",题目给出了一个4x4的棋盘,每个格子有黑色或白色,每次翻转一个格子会同时翻转它上下左右四个格子的颜色,目标是把整个棋盘都变为同一种颜色,求把棋盘变成同种颜色的最小步数。 解题思路: 一般关于棋盘变色的题目,可以考虑使用搜索来解决。对于POJ1753题目,可以使用广度优先搜索(BFS)来解决。 首先,对于每个格子,定义一个状态,0表示当前格子是白色,1表示当前格子是黑色。 然后,我们可以把棋盘抽象成一个长度为16的二进制数,将所有格子的状态按照从左往右,从上往下的顺序排列,就可以用一个16位的二进制数表示整个棋盘的状态。例如,一个棋盘状态为: 0101 1010 0101 1010 则按照从左往右,从上往下的顺序把所有格子的状态连接起来,即可得到该棋盘的状态为"0101101001011010"。 接着,我们可以使用队列来实现广度优先搜索。首先将初始状态加入队列中,然后对于队列中的每一个状态,我们都尝试将棋盘上的每个格子翻转一次,生成一个新状态,将新状态加入队列中。对于每一个新状态,我们也需要记录它是从哪个状态翻转得到的,以便在得到最终状态时能够输出路径。 在搜索过程中,我们需要维护每个状态离初始状态的步数,即将该状态转换为最终状态需要的最小步数。如果我们找到了最终状态,就可以输出答案,即最小步数。 代码实现:

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值