51nod p1175 区间中第K大的数
第1行:1个数N,表示序列的长度。(2 <= N <= 50000)
第2 - N + 1行:每行1个数,对应序列中的元素。(0 <= S[i] <= 10^9)
第N + 2行:1个数Q,表示查询的数量。(2 <= Q <= 50000)
第N + 3 - N + Q + 2行:每行3个数,对应查询的起始编号i和结束编号j,以及k。(0 <= i <= j <= N - 1,1 <= k <= j - i + 1)
共Q行,对应每一个查询区间中第K大的数。
5
1
7
6
3
1
3
0 1 1
1 3 2
3 4 2
7
6
1 区间第k大,主席树写一发,使用java写的,顺便复习
吐槽---java真是慢,一定要离散化,c++随便写都过
而且没有sort,没有map,只有hashmap,真逊啊。。。。
package p1175; import java.io.*;
import java.util.*; import javax.management.Query; public class Main
{
public static class Node
{
int sum;
Node lc, rc;
public Node(Node t)
{
if(t == null)
{
lc = rc = null;
sum = 0;
}
else
{
lc = t.lc;
rc = t.rc;
sum = t.sum;
}
}
} static final int N = 50010; public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
int n = getInt(reader);
int[] arr = new int[n], store = new int[n];
for(int i = 0; i < n; i++) store[i] = arr[i] = getInt(reader);
mergeSort(store, 0, n - 1);
int len = 1;
for(int i = 1; i < n; i++)
if(store[i] != store[i - 1]) store[len++] = store[i];
HashMap<Integer, Integer> myMap = new HashMap<Integer, Integer>();
for(int i = 0; i < len; i++)
myMap.put(store[i], i);
for(int i = 0; i < n; i++)
arr[i] = myMap.get(arr[i]); Node[] root = new Node[n + 1];
Node last = null;
for(int i = 0; i < n; i++)
{
root[i] = new Node(last);
buildTree(root[i], arr[i], 0, len);
last = root[i];
} for(int m = getInt(reader); m > 0; m--)
{
int l = getInt(reader), r = getInt(reader), k = getInt(reader);
int ans = queryKth(l > 0 ? root[l - 1] : null, root[r], k, 0, len);
writer.write(store[ans] + "\r\n");
writer.flush();
}
} public static int querySum(Node t)
{
if(t == null) return 0;
return t.sum;
} public static int queryKth(Node l, Node r, int kth, int left, int right)
{
if(left == right) return left;
int mid = (left + right) >> 1, ret = -1;
int lCnt = l == null ? 0 : querySum(l.rc), rCnt = r == null ? 0 : querySum(r.rc);
if(rCnt - lCnt >= kth) ret = queryKth(l == null ? null : l.rc, r == null ? null : r.rc, kth, mid + 1, right);
else ret = queryKth(l == null ? null : l.lc, r == null ? null : r.lc, kth - (rCnt - lCnt), left, mid);
return ret;
} public static void buildTree(Node x, int val, int left, int right)
{
if(left < right)
{
int mid = (left + right) >> 1;
if(val <= mid)
{
x.lc = new Node(x.lc);
buildTree(x.lc, val, left, mid);
}
else
{
x.rc = new Node(x.rc);
buildTree(x.rc, val, mid + 1, right);
}
}
x.sum++;
} static int[] tmp = new int[N];
public static void mergeSort(int[] arr, int l, int r)
{
if(l >= r) return;
int mid = (l + r) >> 1;
mergeSort(arr, l, mid);
mergeSort(arr, mid + 1, r);
int itL = l, itR = mid + 1, now = l;
while(itL <= mid && itR <= r)
{
if(arr[itL] < arr[itR]) tmp[now++] = arr[itL++];
else tmp[now++] = arr[itR++];
}
while(itL <= mid) tmp[now++] = arr[itL++];
while(itR <= r) tmp[now++] = arr[itR++];
for(int i = l; i <= r; i++) arr[i] = tmp[i];
} public static int getInt(BufferedReader reader) throws IOException
{
char ch = ' ';
for( ; !(ch >= '0' && ch <= '9'); ch = (char) reader.read()) ;
int ret = 0;
for( ; ch >= '0' && ch <= '9'; ch = (char) reader.read())
ret = ret * 10 + ch - '0';
return ret;
} }
51nod p1175 区间中第K大的数的更多相关文章
- 51Nod 1175 区间中第K大的数 (可持久化线段树+离散)
1175 区间中第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 一个长度为N的整数序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有 ...
- 51nod 区间中第K大的数
区间中第K大的数 基准时间限制:1 秒 空间限制:131072 KB 一个长度为N的整数序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,第K大的数是多少. 例如: 1 7 6 ...
- 51nod1175 区间中第K大的数
裸的主席树. #include<cstdio> #include<cstring> #include<cctype> #include<algorithm&g ...
- 51NOD 1105 第K大的数
数组A和数组B,里面都有n个整数. 数组C共有n^2个整数,分别是: A[0] * B[0],A[0] * B[1] ...... A[0] * B[n-1] A[1] * B[0],A[1] * B ...
- AC日记——第K大的数 51nod 1105
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...
- 51nod 1105 第K大的数 【双重二分/二分套二分/两数组任意乘积后第K大数】
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...
- 51nod 1105:第K大的数
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...
- [51NOD1105]第k大的数(二分答案)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1105 先排序,二分上下界分别是最小的两个数和最大的两个数的乘积 ...
- POJ 2985 The k-th Largest Group(树状数组 并查集/查找第k大的数)
传送门 The k-th Largest Group Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 8690 Acce ...
随机推荐
- python基础——定制类
python基础——定制类 看到类似__slots__这种形如__xxx__的变量或者函数名就要注意,这些在Python中是有特殊用途的. __slots__我们已经知道怎么用了,__len__()方 ...
- Tomcat在Linux上的安装与配置
以下使用的Linux版本为: Redhat Enterprise Linux 6.5 x86_64,Tomcat版本为tomcat-7.0.54. 1.下载JDK与Tomcat. jdk下载地址 ...
- BI 项目管理之角色和职责
DW/BI 系统在生命周期中需要许多不同的角色和技能,它们来自业务和技术领域.本文将介绍创建DW/BI 系统所涉及的主要角色.角色和人之间很少是一对一关系.与我们合作的团队小到只有一人,大 ...
- POJ1699 HDU 1560 Best Sequence(AC自动机 最短路)
曾写过迭代加深搜索的方法,现在使用在AC自动上跑最短路的方法 dp[i][j]表示状态为到节点i,模式串是否包含的状态为j的最短串的长度,则状态转移方程为: dp[nx][ny] = min(dp[x ...
- CocoaPods安装和使用教程(转摘)
Code4App 原创文章.转载请注明出处:http://code4app.com/article/cocoapods-install-usage 目录 CocoaPods是什么? 如何下载和安装Co ...
- 硬盘格式是MBR、GPT
装win7 64位要求硬盘格式是MBR 现在预装 Win8 的电脑大多是采用新版 UEFI 固件 + GPT 格式磁盘 GPT模式是针对整个硬盘的初始化而言,因此不存在某一个分区是GPT模式的说法.转 ...
- ubuntu初始化root帐号密码
Ubuntu Kylin 14.04的安装过程中并没有提供设置root密码的过程,取而代之的是自定义的帐号. 如果我们需要使用到root帐号或者root权限,则需要重新设置root帐号的密码. 设置方 ...
- Git 基础操作
[TOC] 在Linux上安装Git $ git --version #查看git的版本号 $ sudo apt-get install git # 安装git 创建版本库 $ git init # ...
- loj 1357(树形dp)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1357 #define _CRT_SECURE_NO_WARNINGS #include ...
- Linux---从start_kernel到init进程启动
“平安的祝福 + 原创作品转载请注明出处 + <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” ini ...