HDU 2665(主席树,无修改第k小)
Kth number
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10682 Accepted Submission(s): 3268
For
each test case, the first line contain two integer n and m (n, m <=
100000), indicates the number of integers in the sequence and the number
of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
#define ll long long
#define LL unsigned long long
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
struct node{
int left,right;
int val;
}tree[N*];
int cnt;
int a[N],b[N];
int root[N];
int build(int l,int r){
int pos=++cnt;
tree[pos].val=;
if(l==r)return pos;
int mid=(l+r)>>;
tree[pos].left=build(l,mid);
tree[pos].right=build(mid+,r);
return pos;
}
void update(int pre,int &now,int x,int l,int r){
tree[++cnt]=tree[pre];
now=cnt;
tree[now].val++;
if(l==r)return;
int mid=(l+r)>>;
if(x<=mid){
update(tree[pre].left,tree[now].left,x,l,mid);
}
else{
update(tree[pre].right,tree[now].right,x,mid+,r);
} }
int query(int L,int R,int k,int l,int r){
if(l==r) return l;
int mid=(l+r)>>;
int sum=tree[tree[R].left].val-tree[tree[L].left].val;
if(k<=sum){
return query(tree[L].left,tree[R].left,k,l,mid);
}
else{
return query(tree[L].right,tree[R].right,k-sum,mid+,r);
} }
int main()
{
int t;
scanf("%d",&t);
while(t--){
memset(root,,sizeof(root));
memset(a,,sizeof(a));
memset(b,,sizeof(b));
int n,m;
scanf("%d%d",&n,&m);
cnt=;
for(int i=;i<=n;i++) {
scanf("%d",&a[i]);
b[i]=a[i];
}
sort(b+,b++n);
int tt = unique(b+,b++n)-b-;
root[0]=build(1,tt);
for(int i=;i<=n;i++) {
a[i]=lower_bound(b+,b++tt,a[i])-b;//二分找到a[i]的位置
update(root[i-],root[i],a[i],,tt);//root[i-1]表示上一个版本的线段树
}
for(int i=;i<=m;i++) {
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
int ans=query(root[l-],root[r],k,,tt);
printf("%d\n", b[ans]);
}
}
return ;
}
HDU 2665(主席树,无修改第k小)的更多相关文章
- POJ 2104 HDU 2665 主席树 解决区间第K大
两道题都是区间第K大询问,数据规模基本相同. 解决这种问题, 可以采用平方划分(块状表)复杂度也可以接受,但是实际表现比主席树差得多. 这里大致讲一下我对主席树的理解. 首先,如果对于某个区间[L,R ...
- Super Mario HDU - 4417 (主席树询问区间比k小的个数)
Mario is world-famous plumber. His “burly” figure and amazing jumping ability reminded in our memory ...
- HDU 5919 - Sequence II (2016CCPC长春) 主席树 (区间第K小+区间不同值个数)
HDU 5919 题意: 动态处理一个序列的区间问题,对于一个给定序列,每次输入区间的左端点和右端点,输出这个区间中:每个数字第一次出现的位子留下, 输出这些位子中最中间的那个,就是(len+1)/2 ...
- 主席树--动态区间第k小
主席树--动态区间第\(k\)小 模板题在这里洛谷2617. 先对几个问题做一个总结: 阅读本文需要有主席树的基础,也就是通过区间kth的模板题. 静态整体kth: sort一下找第k小,时间复杂度\ ...
- A - 低阶入门膜法 - K-th Number (主席树查询区间第k小)
题目链接:https://cn.vjudge.net/contest/284294#problem/A 题目大意:主席树查询区间第k小. 具体思路:主席树入门. AC代码: #include<i ...
- SPOJ 10628 Count on a tree(Tarjan离线LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- BZOJ.2588.Count on a tree(主席树 静态树上第k小)
题目链接 /* 序列上的主席树 某点是利用前一个点的根建树 同理 树上的主席树 某个节点可以利用其父节点(is unique)的根建树 排名可以利用树上前缀和求得: 对于(u,v),w=LCA(u,v ...
- SPOJ 10628 Count on a tree(Tarjan离线 | RMQ-ST在线求LCA+主席树求树上第K小)
COT - Count on a tree #tree You are given a tree with N nodes.The tree nodes are numbered from 1 to ...
- 主席树(区间第k小的数)
题目链接: https://www.luogu.org/problem/P3834 首先要离散化,然后主席树模板. 1 #include<cstdio> 2 #include<cst ...
随机推荐
- jQuery导航插件One-Page-Nav演示-显示命名锚记
jQuery导航插件One-Page-Nav演示-显示命名锚记 简介 使用 选项 示例 推荐 简介 电商网站的分类比较明确,比如1楼是手机通讯产品,2楼是家用电器,3楼是服装鞋包等等,旁边还会有一个固 ...
- JS——html基本结构
document.title——文档标题 document.head——文档头标签 document.body——文档的主体 document.documentElement 表示整个文档的html标 ...
- Linux Shell ssh登录脚本
Linux 登陆服务器敲命令太多,某时候确实不便,所以就用shell写了一个 我的blog地址: http://www.cnblogs.com/caoguo 一.说明 支持秘密和密钥两种格式 用户名 ...
- url取值乱码问题,url加中文导致页面不能加载问题 js unicode转码,以及解码
很多时候写H5或其他适配时,打不开url.很多原因是因为浏览器不支持中文url,从url拿 出来的中文值也会乱码,这时候就必须把中文转化成Unicode值,去进行页面传值 中文转Unicode fun ...
- JAVA如何获得数据库的字段及字段类型
Java获取数据库的表中各字段的字段名,代码如下: import java.sql.Connection;import java.sql.DriverManager;import java.sql.R ...
- 洛谷——P3906 Geodetic集合
P3906 Geodetic集合 题目描述 图G是一个无向连通图,没有自环,并且两点之间至多只有一条边.我们定义顶点v,u最短路径就是从v到u经过边最少的路径.所有包含在v-u的最短路径上的顶点被称为 ...
- Python变量的命名 单下划线和双下划线
python命名变量的区别 foo: 一种约定,Python内部的名字,用来区别其他用户自定义的命名,以防冲突,就是例如__init__(),__del__(),__call__()这些特殊方法 _f ...
- C#学习笔记_13_静态类&Sealed&运算符重载&抽象类
13_静态类&Sealed&运算符重载&抽象类 静态类 由static修饰的类就是静态类 特点: 静态类不能实例化对象 静态类中不允许写非静态的成员 静态类只能由一个父类Obj ...
- BZOJ 2038 2009国家集训队 小Z的袜子【模板·莫队】
[题解] 1,先说说莫队算法. 莫队算法是用来离线处理区间问题的算法.非常易于理解和使用,且运用十分广泛. 假设我们现在已知区间[L,R]的答案,如果我们能以较低的时间复杂度扩展得到区间$[L-1,R ...
- bupt summer training for 16 #2 ——计算几何
https://vjudge.net/contest/171368#overview A.一个签到题,用叉积来判断一个点在一条线的哪个方向 可以二分,数据范围允许暴力 #include <cst ...