http://poj.org/problem?id=2104 (题目链接)

题意

  求区间第k大数。

Solution1

  主席树裸题。

  主席树当时我学是学的要死,那个时候不晓得百度出什么bug了,搜个主席树出来的全是什么习主席巴拉巴拉的东西。。。于是找了个模板问同学自己磨出来的。

  有个博客我觉得写得还不错:想学主席树戳这里

  另外,这里的主席树储存的是值域,而不是别的。

代码

// poj2761
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#define LL long long
#define lim 1000000000
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
int n,m,sz,rt[maxn];
struct node {
int son[2],s;
int& operator [] (int x) {return son[x];}
}tr[maxn*40]; void build(int &u,int v,int l,int r,int val) {
u=++sz;
if (l==r) {tr[u].s=tr[v].s+1;return;}
int mid=(l+r)>>1;
if (val<=mid) build(tr[u][0],tr[v][0],l,mid,val),tr[u][1]=tr[v][1];
else build(tr[u][1],tr[v][1],mid+1,r,val),tr[u][0]=tr[v][0];
tr[u].s=tr[tr[u][0]].s+tr[tr[u][1]].s;
}
int query(int u,int v,int l,int r,int k) {
if (l==r) return l;
int mid=(l+r)>>1,c=tr[tr[v][0]].s-tr[tr[u][0]].s;
if (c>=k) return query(tr[u][0],tr[v][0],l,mid,k);
else return query(tr[u][1],tr[v][1],mid+1,r,k-c);
}
int main() {
scanf("%d%d",&n,&m);
for (int x,i=1;i<=n;i++) {
scanf("%d",&x);
build(rt[i],rt[i-1],-lim,lim,x);
}
for (int x,y,k,i=1;i<=m;i++) {
scanf("%d%d%d",&x,&y,&k);
printf("%d\n",query(rt[x-1],rt[y],-lim,lim,k));
}
return 0;
}

  


Solution2

  权值分块+莫队算法。

  好像静态的主席树都可以用 权值分块+莫队 解决,只是时间上和空间上有差异。这道题具体做法与bzoj2809差不多。

代码

// poj2104
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LL long long
#define inf 2147483640
#define Pi acos(-1.0)
#define free(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout);
using namespace std; const int maxn=100010;
struct ask {int l,r,k,id;}t[maxn];
struct data {int w,id;}a[maxn];
int pos[maxn],cnts[maxn],b[maxn],p[maxn],ma[maxn],ans[maxn],n,m,s,q,block; bool wcmp(data a,data b) {
return a.w<b.w;
}
bool poscmp(ask a,ask b) {
return pos[a.l]==pos[b.l] ? a.r<b.r : pos[a.l]<pos[b.l];
}
void build() {
block=(int)sqrt((float)m);
for (int i=1;i<=m;i++) pos[i]=(i-1)/block+1;
s=m%block ? m/block+1 : m/block;
for (int i=1;i<=s;i++) cnts[i]=0;
}
void update(int x,int val) {
cnts[pos[x]]+=val;
b[x]+=val;
}
int query(int k) {
int tot=0;
for (int i=1;i<=s;i++) {
tot+=cnts[i];
if (tot>=k) {
tot-=cnts[i];
for (int j=(i-1)*block+1;j<=min(i*block,m);j++) {
if (tot+b[j]>=k) return ma[j];
else tot+=b[j];
}
}
}
}
int main() {
scanf("%d%d",&n,&q);
for (int i=1;i<=n;i++) scanf("%d",&a[i].w),a[i].id=i;
for (int i=1;i<=q;i++) scanf("%d%d%d",&t[i].l,&t[i].r,&t[i].k),t[i].id=i;
sort(a+1,a+1+n,wcmp);
m=0;
ma[p[a[1].id]=++m]=a[1].w;
for (int i=2;i<=n;i++) {
if (a[i].w!=a[i-1].w) m++;
ma[p[a[i].id]=m]=a[i].w;
}
block=(int)sqrt((float)n);
for (int i=1;i<=n;i++) pos[i]=(i-1)/block+1;
sort(t+1,t+1+q,poscmp);
build();
for (int l=1,r=0,i=1;i<=q;i++) {
for (;r<t[i].r;r++) update(p[r+1],1);
for (;r>t[i].r;r--) update(p[r],-1);
for (;l<t[i].l;l++) update(p[l],-1);
for (;l>t[i].l;l--) update(p[l-1],1);
ans[t[i].id]=query(t[i].k);
}
for (int i=1;i<=q;i++) printf("%d\n",ans[i]);
return 0;
}

  

【bzoj2104】 K-th Number的更多相关文章

  1. 【CF245H】Queries for Number of Palindromes(回文树)

    [CF245H]Queries for Number of Palindromes(回文树) 题面 洛谷 题解 回文树,很类似原来一道后缀自动机的题目 后缀自动机那道题 看到\(n\)的范围很小,但是 ...

  2. 【转】oracle数据库NUMBER数据类型

    原文:http://www.jb51.net/article/37633.htm NUMBER ( precision, scale)a)  precision表示数字中的有效位;如果没有指定prec ...

  3. 【BZOJ3110】K大数查询(整体二分)

    [BZOJ3110]K大数查询(整体二分) 题面 BZOJ 题解 看了很久整体二分 一直不知道哪里写错了 ... 又把树状数组当成线段树区间加法来用了.. 整体二分还是要想清楚在干什么: 我们考虑第\ ...

  4. 【CF1133E】K Balanced Teams(动态规划,单调队列)

    [CF1133E]K Balanced Teams(动态规划,单调队列) 题面 CF 让你把一堆数选一些出来分成不超过\(K\)组,每一组里面的最大值和最小值之差不超过\(5\),求最多有多少个人元素 ...

  5. 【BZOJ4520】K远点对(KD-Tree)

    [BZOJ4520]K远点对(KD-Tree) 题面 BZOJ 洛谷 题解 考虑暴力. 维护一个大小为\(K\)的小根堆,然后每次把两个点之间的距离插进去,然后弹出堆顶 这样子可以用\(KD-Tree ...

  6. 【BZOJ4504】K个串 可持久化线段树+堆

    [BZOJ4504]K个串 Description 兔子们在玩k个串的游戏.首先,它们拿出了一个长度为n的数字序列,选出其中的一个连续子串,然后统计其子串中所有数字之和(注意这里重复出现的数字只被统计 ...

  7. 【BZOJ4026】dC Loves Number Theory 分解质因数+主席树

    [BZOJ4026]dC Loves Number Theory Description  dC 在秒了BZOJ 上所有的数论题后,感觉萌萌哒,想出了这么一道水题,来拯救日益枯竭的水题资源.    给 ...

  8. 【LeetCode】375. Guess Number Higher or Lower II 解题报告(Python)

    [LeetCode]375. Guess Number Higher or Lower II 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://f ...

  9. 【LeetCode】137. Single Number II 解题报告(Python)

    [LeetCode]137. Single Number II 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/single- ...

  10. 【LeetCode】306. Additive Number 解题报告(Python)

    [LeetCode]306. Additive Number 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http: ...

随机推荐

  1. http://www.cnblogs.com/figure9/p/developer-reading-list.html

    http://www.cnblogs.com/figure9/p/developer-reading-list.html

  2. SQL Server 2005、2008 的 datetime 值范围(转)

    SQL Server 2005.2008 的 datetime 最小值是:1753-01-01 00:00:00 最大值是:9999-12-31 23:59:59.997 这与 .NET 中的 Dat ...

  3. noi题库(noi.openjudge.cn) 1.8编程基础之多维数组T01——T10

    T01 矩阵交换行 描述 给定一个5*5的矩阵(数学上,一个r×c的矩阵是一个由r行c列元素排列成的矩形阵列),将第n行和第m行交换,输出交换后的结果. 输入 输入共6行,前5行为矩阵的每一行元素,元 ...

  4. echo使用说明,参数详解

    简介 echo [OPTION]... [STRING]... 描述 -n 末尾不加换行 -e 开启输出字串中对反斜杠的转译 -E 禁用反斜杠转译 只有开启-e参数的时候,下面的命令才能起作用: \0 ...

  5. Vue系列: 如何通过组件的属性props设置样式

    比如我们要在vue中显示百度地图,然后将相关的代码包装成组件,然后需要由外部来设置组件的高度,关于props的介绍,可以参考: http://cn.vuejs.org/guide/components ...

  6. [MetaHook] Surface hook

    Hook ISurface function. #include <metahook.h> #include <vgui/ISurface.h> using namespace ...

  7. c++ this *this

    在一个类里每个成员函数都有一个this指针.this指针指向调用对象.如果方法需要引用整个调用 对象可以使用*this.this是对象的地址,而不是对象本身. *this是对象本身.可以将*this ...

  8. html文本标准模式,首行空两格,两端对齐,行高

    font-size: 13px; line-height: 1.6; text-align: justify; text-indent: 2em;

  9. C#微信公众号开发系列教程四(接收普通消息)

    微信公众号开发系列教程一(调试环境部署) 微信公众号开发系列教程一(调试环境部署续:vs远程调试) C#微信公众号开发系列教程二(新手接入指南) C#微信公众号开发系列教程三(消息体签名及加解密) C ...

  10. 如何把自己打造成技术圈的 papi 酱

    最近半年,一个叫papi酱的平胸女子连续在微博.朋友圈.创业圈刷屏,当之无愧成了中文互联网的第一大网红.呃,你以为我会巴拉巴拉说一堆网工创业的事?NO,今天想借papi酱的话题跟大家一起聊聊程序员如何 ...