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划分到左子树的元素个数(包括j)

void build(int c,int l,int r)
{
    int mid,i;
    int lm; // 记录左区间共有多少个数=as{mid]
    int lp; //  左区间的mark
    int rp; //  右区间的mark

    mid=(l+r)/2;
    lm=mid-l+1;
    lp=l;
    rp=mid+1;
    for (i=l;i<=mid;i++)
        if (as[i]<as[mid])
        lm--;//先假设左边的(mid - l + 1)个数都等于as[mid],然后把实际上小于as[mid]的减去
    for (i=l;i<=r;i++)
    {
        if (i==l)
            sum[c][i]=0;
        else
            sum[c][i]=sum[c][i-1];

        if (tree[c][i]==as[mid])
        {
            if (lm)
            {
                lm--;
                sum[c][i]++;
                tree[c+1][lp++]=tree[c][i];
            }
            else
                tree[c+1][rp++]=tree[c][i];
        }
        else
        if (tree[c][i]<as[mid])
        {
            sum[c][i]++;
            tree[c+1][lp++]=tree[c][i];
        }
        else
            tree[c+1][rp++]=tree[c][i];
    }
    if (l==r) return ;
    build(c+1,l,mid);
    build(c+1,mid+1,r);
}

int query(int c,int l,int r,int ql,int qr,int k)// 在l-r区间内的  ql-qr区间中的第K小的数
{
    int s;//[l, ql)内将被划分到左子树的元素数目
    int ss;//[ql, qr]内将被划分到左子树的元素数目
    int mid=(l+r)/2;

    if (l==r)
        return tree[c][l];

    if (l==ql)
    {
        s=0;
        ss=sum[c][qr];
    }
    else
    {
        s=sum[c][ql-1];
        ss=sum[c][qr]-s;
    }

    if (k<=ss)
        return query(c+1,l,mid,l+s,l+s+ss-1,k);
    else
        return query(c+1,mid+1,r,mid-l+1+ql-s,mid-l+1+qr-s-ss,k-ss);
}

int main()
{
    int n,m,i,l,r,k;
    while (scanf("%d%d",&n,&m)!=EOF)
    {
        for (i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            tree[0][i]=as[i]=a[i];
        }
        sort(as+1,as+1+n);
        build(0,1,n);
        while (m--)
        {
            scanf("%d%d%d",&l,&r,&k);
            printf("%d\n",query(0,1,n,l,r,k));
        }
    }
    return 0;
}
时间: 2024-10-12 19:27:45

POJ 2104 划分树模板题的相关文章

poj 2104 划分树模板

划分树的模板题. 1 #include <iostream> 2 #include <algorithm> 3 #include <cstdio> 4 using namespace std; 5 6 const int N = 100001; 7 int tree[20][N]; 8 int sum[20][N]; 9 int as[N]; 10 11 void build( int p, int l, int r ) 12 { 13 int mid = ( ( l

划分树模板题

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

K-th Number POJ - 2104 划分树

K-th Number 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

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 3264 线段树模板题

之前做的那道是区间求和的,这道题是求区间最大值和最小值之差的,感觉这道题更简单.只需在插入时把每个区间的最大值最小值求出来保存在根节点上就可以啦~\(^o^)/ Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 39475   Accepted: 18524 Case Time Limit: 2000MS Description For the daily milking, Farmer Jo

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&

字典树模板题 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']