POJ–2104 K-th Number

主席树。

主席树复习,这道题是主席树的经典入门了吧。。

无修改区间第k大。

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 200000 + 10;
const int maxm = 5000000 + 10;

/*
void build(int &x,int L,int R) {
    if(!x) x=++vid;
    if(L==R) return;
    int mid = (L+R)>>1;
    build(lc[x],L,mid);
    build(rc[x],mid+1,R);
}*/

int root[maxn];
int lc[maxm],rc[maxm],s[maxm],vid;
int n,m,q,cur;
int a[maxn],t[maxn];

void modify(int &x,int y,int L,int R,int pos,int val=1) {
    x=++vid; s[x]=s[y]+val;
    lc[x]=lc[y]; rc[x]=rc[y];
    if(L==R) return;
    int mid = (L+R)>>1;
    if(pos<=mid) modify(lc[x],lc[y],L,mid,pos);
    else modify(rc[x],rc[y],mid+1,R,pos);
}

int query(int x,int y,int k) {
    int l=1,r=m;
    while(l<r) {
        cur=s[lc[x]]-s[lc[y]];
        int mid=(l+r)>>1;
        if(k<=cur) {
            x=lc[x]; y=lc[y]; r=mid;
        }
        else {
            x=rc[x]; y=rc[y]; l=mid+1; k-=cur;
        }
    }
    return l;
}

int l,r,k;

int main() {
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++) {
        scanf("%d",&a[i]);
        t[i]=a[i];
    }
    //printf("test\n");
    sort(t+1,t+n+1);
    m=unique(t+1,t+n+1)-(t+1);
    for(int i=1;i<=n;i++) a[i]=lower_bound(t+1,t+m+1,a[i])-t;
    for(int i=1;i<=n;i++) modify(root[i],root[i-1],1,m,a[i]);
    while(q--) {
        scanf("%d%d%d",&l,&r,&k);
        printf("%d\n",t[query(root[r],root[l-1],k)]);
    }
    return 0;
}
时间: 2024-12-22 12:22:11

POJ–2104 K-th Number的相关文章

POJ 2104:K-th Number(主席树静态区间k大)

题目大意:对于一个序列,每次询问区间[l,r]的第k大树. 分析: 主席树模板题 program kthtree; type point=record l,r,s:longint; end; var t:array[0..100000*50]of point; a,b,id,root:array[0..100000]of longint; n,i,m,x,y,k,v,len:longint; procedure qsort(l,h:longint); var i,j,t,m:longint; b

POJ 2104:K-th Number(整体二分)

http://poj.org/problem?id=2104 题意:给出n个数和m个询问求区间第K小. 思路:以前用主席树做过,这次学整体二分来做.整体二分在yr大佬的指点下,终于大概懂了点了.对于二分能够解决的询问,如果有多个,那么如果支持离线处理的话,那么就可以使用整体二分了. 在这题二分可行的答案,根据这个答案,把询问操作丢在左右两个队列里面分别递归继续按这样处理.注释里写的很详细. 1 #include <iostream> 2 #include <cstdlib> 3 #

【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

【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

Poj 2104区间第k大(归并树)

题目链接 K-th Number Time Limit: 20000MS Memory Limit: 65536K Total Submissions: 36890 Accepted: 11860 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key ins

POJ 2104&amp;HDU 2665 Kth number(主席树入门+离散化)

K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Case Time Limit: 2000MS Description You are working for Macrohard company in data structures department. After failing your previous task about key inse

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

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

K-th Number POJ - 2104 (整体二分)

K-th Number POJ - 2104 之前学主席树写了一遍 最近再看CDQ分治和整体二分,一直不是很理解,看着别人代码稍微理解了一些 1 //比主席树慢了挺多 2 #include <iostream> 3 #include <cstring> 4 #include <cstdio> 5 6 using namespace std; 7 8 const int maxn = 1e5 + 10; 9 const int maxq = 5010; 10 const

poj 2104主席树求区间第k小

POJ - 2104 题意:求区间第k小 思路:无修改主席树 AC代码: #include "iostream" #include "iomanip" #include "string.h" #include "stack" #include "queue" #include "string" #include "vector" #include "set&