划分树模板题

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

K-th Number

Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 37130   Accepted: 11974
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 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
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <limits.h>
#include <ctype.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#include <string.h>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <vector>
#include <set>
#include <map>
using namespace std;
#define mid ((l+r)>>1)
#define maxn 100010
int t[20][maxn],sum[20][maxn];
int a[maxn],as[maxn];
//以下为查找区间第k小划分树
void build(int p,int l,int r)
{
    int lm=0,i,ls=l,rs=mid+1;//lm表示应被放入左子树且与中位数相等的数有多少个,ls为左子树的起始位置,rs为右子树的起始位置
    for(i=mid; i>=l; i--) //求lm
    {
        if(as[i]==as[mid])
            lm++;
        else
            break;
    }
    for(i=l; i<=r; i++)
    {
        if(i==l)//这里要特殊讨论
            sum[p][i]=0;
        else
            sum[p][i]=sum[p][i-1];
        if(t[p][i]==as[mid])//若与中位数相等则判断是否应该被放入左子树
        {
            if(lm)
            {
                lm--;
                sum[p][i]++;
                t[p+1][ls++]=t[p][i];
            }
            else
                t[p+1][rs++]=t[p][i];
        }
        else if(t[p][i]<as[mid])//查找区间第K大即为>
        {
            sum[p][i]++;
            t[p+1][ls++]=t[p][i];
        }
        else
            t[p+1][rs++]=t[p][i];
    }
    if(l==r)
        return;
    build(p+1,l,mid);
    build(p+1,mid+1,r);
}
int query(int p,int l,int r,int ql,int qr,int k)
{
    int s,ss;//s表示l到ql-1的区间内放入左子树的个数,ss表示区间[ql,qr]被放入左子树的个数
    if(l==r)//找到所求的数
        return t[p][l];
    if(ql==l)
        s=0,ss=sum[p][qr];
    else
        s=sum[p][ql-1],ss=sum[p][qr]-s;
    if(k<=ss)//要找的数在左子树中
        return query(p+1,l,mid,l+s,l+sum[p][qr]-1,k);
    else//要找的数在右子树中
        return query(p+1,mid+1,r,mid+1-l+ql-s,mid+1-l+qr-sum[p][qr],k-ss);
}
int main(){
    int i,n,m,j;
    scanf("%d%d",&n,&m);
    for(i=1;i<=n;i++){
        scanf("%d",&as[i]);
        t[0][i] = as[i];
    }
    sort(as+1,as+1+n);
    build(0,1,n);
    while(m--){
        int l,r,k;;
        scanf("%d%d%d",&l,&r,&k);
        int ans = query(0,1,n,l,r,k);
        printf("%d\n",ans);
    }

    return 0;
}

划分树模板题

时间: 2024-11-05 16:53:20

划分树模板题的相关文章

POJ 2104 划分树模板题

给出n,m n个数字 m次询问,每次询问(l,r)区间的第k小的数 划分树模板 mark一下 #include "stdio.h" #include "string.h" #include "algorithm" using namespace std; int a[100010],as[100010]; int tree[20][100010];// 记录第i层元素序列 int sum[20][100010];// 记录第i层的1~j划分到左子

POJ2104 K-th Number 划分树 模板题啊

/*Source Code Problem: 2104 User: 96655 Memory: 14808K Time: 1282MS Language: G++ Result: Accepted Source Code*/ #include<cstdio> #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<stack>

hdu 2665 划分树模板题(可作为模板)

Kth number Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 6951    Accepted Submission(s): 2214 Problem Description Give you a sequence and ask you the kth big number of a inteval. Input The fi

poj 2104 K-th Number(划分树模板)

划分树模板题,敲上模板就ok了. #include<algorithm> #include<iostream> #include<cstring> #include<vector> #include<cstdio> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #define MP

POJ--2104--K-th Number【划分树模板】

链接:http://poj.org/problem?id=2104 题意:给一个n个元素的数组,m次询问,每次查询区间[i,j]中第k大的数,输出这个数. 思路:划分树模板题 划分树讲解:传送门 我用的是kuangbin大神的模板 #include<cstring> #include<string> #include<fstream> #include<iostream> #include<iomanip> #include<cstdio&

poj3630 Phone List (trie树模板题)

Phone List Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 26328   Accepted: 7938 Description Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix of another. Let's say the phone catalogu

HDU 1251 Trie树模板题

1.HDU 1251 统计难题  Trie树模板题,或者map 2.总结:用C++过了,G++就爆内存.. 题意:查找给定前缀的单词数量. #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b #define F(i,a,b

HDU 4085 斯坦纳树模板题

Dig The Wells Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 971    Accepted Submission(s): 416 Problem Description You may all know the famous story "Three monks". Recently they find som

字典树模板题 POJ 2503

1 #include <cstdio> 2 #include <cstring> 3 4 char en[11],fr[11]; 5 int st; 6 struct Tire{ 7 int next[26]; 8 char eng[11]; 9 }node[200005]; 10 void insert(char *s,int cur) 11 { 12 if(*s){ 13 if(!node[cur].next[*s-'a']) 14 node[cur].next[*s-'a']