poj2104找了个板子。。,但是各种IO还可以进行优化

/*
找区间[l,r]第k大的数
*/
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
#define maxn 100005
struct Node{int lc,rc,sum;}t[maxn*];
int n,q,a[maxn],b[maxn],m,rt[maxn],size;
int build(int l,int r){
int now=++size;
t[now].lc=t[now].rc=t[now].sum=;
if(l==r)return now;
int mid=l+r>>;
t[now].lc=build(l,mid);
t[now].rc=build(mid+,r);
return now;
}
int update(int last,int l,int r,int pos){//更新到pos点
int now=++size;
t[now]=t[last];t[now].sum++;
if(l==r)return now;
int mid=l+r>>;
if(pos<=mid)t[now].lc=update(t[last].lc,l,mid,pos);
else t[now].rc=update(t[last].rc,mid+,r,pos);
return now;
}
int query(int st,int ed,int l,int r,int k){
if(l==r)return l;
int mid=l+r>>;
int sum=t[t[ed].lc].sum-t[t[st].lc].sum;
if(k<=sum)return query(t[st].lc,t[ed].lc,l,mid,k);
else return query(t[st].rc,t[ed].rc,mid+,r,k-sum);
}
void debug(int x){
cout<<t[t[rt[]].lc].sum-t[t[rt[]].lc].sum;
}
int main(){
cin>>n>>q;
for(int i=;i<=n;i++)cin>>a[i],b[i]=a[i];
sort(b+,b++n);
m=unique(b+,b++n)-(b+);
rt[]=build(,m);
for(int i=;i<=n;i++){
int pos=lower_bound(b+,b++m,a[i])-b;
rt[i]=update(rt[i-],,m,pos);
}
while(q--){
int l,r,k;
cin>>l>>r>>k;
cout<<b[query(rt[l-],rt[r],,m,k)]<<'\n';
} }

hdu4417 用vector会莫名MLE,以后就用数组来进行离散化吧。另外,unique(a+1,a+1+n)函数对a数组去重,然后返回去重数组的下一位下标

#include<bits/stdc++.h>
using namespace std;
#define maxn 100005
struct Node{int lc,rc,v;}t[maxn*];
int m,tot,a[maxn],b[maxn],n,rt[maxn]; int update(int last, int l, int r, int pos){
int now = ++ tot;
t[now] = t[last];
t[now].v ++;
if(l == r) return now;
int mid = (l + r) >> ;
if(pos <= mid) t[now].lc = update(t[last].lc, l, mid, pos);
else t[now].rc = update(t[last].rc, mid+, r, pos);
return now;
}
int query(int st, int ed, int L, int R, int l, int r){
if(L <= l && r <= R) return t[ed].v - t[st].v;
int mid = (l + r) >> ;
int res = ;
if(L <= mid) res += query(t[st].lc, t[ed].lc, L, R, l, mid);
if(R > mid) res += query(t[st].rc, t[ed].rc, L, R, mid+, r);
return res;
}
int build(int l,int r){
int now=++tot;
t[now].lc=t[now].rc=t[now].v=;
if(l==r)return now;
int mid=l+r>>;
t[now].lc=build(l,mid);
t[now].rc=build(mid+,r);
return now;
}
int main(){
int T, Case = ;
scanf("%d", &T);
int n, q;
while(T --) {
scanf("%d %d", &n, &q);
for(int i = ;i <= n;i ++) {
scanf("%d", &a[i]);
b[i] = a[i];
}
sort(b+, b++n);
int m = unique(b+, b++n) - (b+);
for(int i = ;i <= n;i ++) a[i] = lower_bound(b+, b++m, a[i]) - b;
tot = ;
rt[] = build(, m);
for(int i = ;i <= n;i ++) rt[i] = update(rt[i-], , m, a[i]);
printf("Case %d:\n", ++Case);
while(q --) {
int l, r, H;
scanf("%d %d %d", &l, &r, &H);
l ++, r ++;
int pos = upper_bound(b+, b++m, H) - b;
pos --;
if(!pos) {
puts("");
continue;
} else {
printf("%d\n", query( rt[l-], rt[r], , pos, , m ) );
}
}
}
return ;
}

主席树入门——询问区间第k大pos2104,询问区间<=k的元素个数hdu4417的更多相关文章

  1. 主席树入门(区间第k大)

    主席树入门 时隔5个月,我又来填主席树的坑了,现在才发现学算法真的要懂了之后,再自己调试,慢慢写出来,如果不懂,就只会按照代码敲,是不会有任何提升的,都不如不照着敲. 所以搞算法一定要弄清原理,和代码 ...

  2. poj2104 k-th number 主席树入门讲解

    poj2104 k-th number 主席树入门讲解 定义:主席树是一种可持久化的线段树 又叫函数式线段树   刚开始学是不是觉得很蒙逼啊 其实我也是 主席树说简单了 就是 保留你每一步操作完成之后 ...

  3. 主席树初步学习笔记(可持久化数组?静态区间第k大?)

    我接触 OI也快1年了,然而只写了3篇博客...(而且还是从DP跳到了主席树),不知道我这个机房吊车尾什么时候才能摸到大佬们的脚后跟orz... 前言:主席树这个东西,可以说是一种非常畸形的数据结构( ...

  4. poj 2104 K-th Number (划分树入门 或者 主席树入门)

    题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...

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

    K-th Number Time Limit: 20000MS   Memory Limit: 65536K Total Submissions: 50247   Accepted: 17101 Ca ...

  6. 集训队8月1日(拓扑排序+DFS+主席树入门)

    上午看书总结 今天上午我看了拓扑排序,DFS+剪枝,相当于回顾了一下,写了三个比较好的例题.算法竞赛指南93~109页. 1.状态压缩+拓扑排序 https://www.cnblogs.com/246 ...

  7. SPOJ3267--D-query (主席树入门练习)

    题意:查找区间内不同数字的个数. 两种做法,一种是 树状数组离线,另一种就是主席树. 树状数组离线操作的链接 http://www.cnblogs.com/oneshot/p/4110415.html ...

  8. 静态区间第k大(主席树)

    POJ 2104为例(主席树入门题) 思想: 可持久化线段树,也叫作函数式线段树,也叫主席树(高大上). 可持久化数据结构(Persistent data structure):利用函数式编程的思想使 ...

  9. POJ 2104 HDU 2665 主席树 解决区间第K大

    两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...

随机推荐

  1. AI佳作解读系列(二)——目标检测AI算法集杂谈:R-CNN,faster R-CNN,yolo,SSD,yoloV2,yoloV3

    1 引言 深度学习目前已经应用到了各个领域,应用场景大体分为三类:物体识别,目标检测,自然语言处理.本文着重与分析目标检测领域的深度学习方法,对其中的经典模型框架进行深入分析. 目标检测可以理解为是物 ...

  2. elasticsearch6.6及其插件安装记录(较详细)

    借鉴网上资料并实施验证结果 elasticsearch6.6安装 安装包下载路径 https://www.elastic.co/downloads/elasticsearch 本文使用安装包 elas ...

  3. C#中字符串的字面值(转义序列)

    在程序开发中,经常会碰到在字符串中字面值中使用转义序列,下面表格收集了下转义序列的完整列表,以便大家查看引用: 转义序列列表 转义序列 产生的字符 字符的Unicode值 \' 单引号 0x0027 ...

  4. Vue.js指令实例

    v-if  v-else  v-show v-if 根据表达式的值的真假条件渲染元素. v-else 不需要表达式.前一兄弟元素必须有 v-if 或 v-else-if v-show 根据表达式之真假 ...

  5. Python——接口类、抽象类

    建立一个接口类.抽象类的规范 from abc import abstractmethod,ABCMeta class Payment(metaclass=ABCMeta): # 元类 默认的元类 t ...

  6. Linux查看文件以及磁盘空间大小管理(转)

    (1)查看文件大小  查看当前文件夹下所有文件大小(包括子文件夹)    du -sh   # du -h15M     ./package16K     ./.fontconfig4.0K    . ...

  7. [BZOJ 4818] [SDOI 2017] 序列计数

    Description Alice想要得到一个长度为 \(n\) 的序列,序列中的数都是不超过 \(m\) 的正整数,而且这 \(n\) 个数的和是 \(p\) 的倍数. Alice还希望,这 \(n ...

  8. 守护进程(Daemon)

    守护进程的概念 守护进程(Daemon)一般是为了保护我们的程序/服务的正常运行,当程序被关闭.异常退出等时再次启动程序/恢复服务. 例如 http 服务的守护进程叫 httpd,mysql 服务的守 ...

  9. React Native——react-navigation的使用

    在 React Native 中,官方已经推荐使用 react-navigation 来实现各个界面的跳转和不同板块的切换. react-navigation 主要包括三个组件: StackNavig ...

  10. centos6/7安装java和maven

    下载安装包并解压到相关目录即可 编辑环境变量vim /etc/profile.d/maven.sh export JAVA_HOME=/app/soft/java-1.8.0_181 export J ...