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>
using namespace std;
const int maxn=100005;
struct node
{
    int val[maxn],num[maxn];
};
struct Tree
{
    int n,o[maxn];
    node t[20];
    void init(int len)
    {
        n=len;
        for(int i=1; i<=n; i++)
        {
            scanf("%d",&o[i]);
            t[0].val[i]=o[i];
        }
        sort(o+1,o+n+1);
        build(1,n,0);
    }
    void build(int l,int r,int dep)
    {
        if(l==r)return;
        int m=(l+r)>>1;
        int lsame=m-l+1,same=0,ln=l,rn=m+1;
        for(int i=l; i<=r; i++)
            if(t[dep].val[i]<o[m])--lsame;
        for(int i=l; i<=r; i++)
        {
            if(i==l)t[dep].num[i]=0;
            else t[dep].num[i]+=t[dep].num[i-1];
            if(t[dep].val[i]<o[m])
                ++t[dep].num[i],t[dep+1].val[ln++]=t[dep].val[i];
            else if(t[dep].val[i]>o[m])
                t[dep+1].val[rn++]=t[dep].val[i];
            else
            {
                ++same;
                {
                    if(lsame>=same)
                        ++t[dep].num[i],t[dep+1].val[ln++]=t[dep].val[i];
                    else t[dep+1].val[rn++]=t[dep].val[i];
                }
            }
        }
        build(l,m,dep+1);
        build(m+1,r,dep+1);
    }
    int query(int st,int ed,int k,int l,int r,int dep)
    {
        if(l==r)return t[dep].val[l];
        int lx,ly,rx,ry,m=(l+r)>>1;
        if(st==l)lx=0;
        else lx=t[dep].num[st-1];
        ly=t[dep].num[ed];
        if(ly-lx>=k)
            return query(l+lx,l+ly-1,k,l,m,dep+1);
        else
        {
            rx=st-l-lx;
            ry=ed-st+1-(ly-lx);
            return query(m+1+rx,m+rx+ry,k-(ly-lx),m+1,r,dep+1);
        }
    }
}tree;
int main()
{
    int n,q;
    while(~scanf("%d%d",&n,&q))
    {
       tree.init(n);
       while(q--)
       {
           int l,r,k;
           scanf("%d%d%d",&l,&r,&k);
           int ans=tree.query(l,r,k,1,n,0);
           printf("%d\n",ans);
       }
    }
    return 0;
}

时间: 2024-10-18 17:10:22

POJ2104 K-th Number 划分树 模板题啊的相关文章

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

划分树模板题

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

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划分到左子

【POJ 2104】 K-th Number 主席树模板题

达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没有评测,但我立下flag这个代码一定能A.我的同学在自习课上考语文,然而机房党都跑到机房来避难了\(^o^)/~ #include<cstdio> #include<cstring> #include<algorithm> #define for1(i,a,n) for(i

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【划分树模板】

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

[POJ2104] 区间第k大数 [区间第k大数,可持久化线段树模板题]

可持久化线段树模板题. #include <iostream> #include <algorithm> #include <cstdio> #include <cstdlib> #include <cstring> #include <cmath> #include <ctime> #include <vector> using namespace std; int n,q,tot,a[110000]; in

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

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