51nod 区间中第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大;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p;p=p*p;q>>=;}return f;}
int n,m,k,t,sz;
int a[maxn],b[maxn],ls[maxn*],rs[maxn*],s[maxn*],root[maxn];
void insert(int l,int r,int x,int &y,int v)
{
y=++sz;
s[y]=s[x]+;
if(l==r)return;
ls[y]=ls[x],rs[y]=rs[x];
int mid=l+r>>;
if(v<=mid)insert(l,mid,ls[x],ls[y],v);
else insert(mid+,r,rs[x],rs[y],v);
}
int query(int l,int r,int x,int y,int k)
{
if(l==r)return l;
int mid=l+r>>;
if(s[rs[y]]-s[rs[x]]>=k)return query(mid+,r,rs[x],rs[y],k);
else return query(l,mid,ls[x],ls[y],k-(s[rs[y]]-s[rs[x]]));
}
int main()
{
int i,j;
scanf("%d",&n);
rep(i,,n)scanf("%d",&a[i]),b[i]=a[i];
sort(b+,b+n+);
int num=unique(b+,b+n+)-b-;
rep(i,,n)a[i]=lower_bound(b+,b+num+,a[i])-b;
rep(i,,n)insert(,num,root[i-],root[i],a[i]);
scanf("%d",&m);
rep(i,,m)
{
int c,d,e;
scanf("%d%d%d",&c,&d,&e);
c++,d++;
printf("%d\n",b[query(,num,root[c-],root[d],e)]);
}
//system("Pause");
return ;
}
51nod 区间中第K大的数的更多相关文章
- 51nod p1175 区间中第K大的数
1175 区间中第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 一个长度为N的整数序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有 ...
- 51Nod 1175 区间中第K大的数 (可持久化线段树+离散)
1175 区间中第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 160 难度:6级算法题 一个长度为N的整数序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有 ...
- 51nod1175 区间中第K大的数
裸的主席树. #include<cstdio> #include<cstring> #include<cctype> #include<algorithm&g ...
- 51nod 1105:第K大的数
1105 第K大的数 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 收藏 关注 数组A和数组B,里面都有n个整数.数组C共有n^2个整数,分别是A[0] * ...
- 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] * ...
- [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 ...
随机推荐
- JS获取网页中HTML元素的几种方法分析
getElementById getElementsByName getElementsByTagName 大概介绍 getElementById ,getElementsByName ,getEle ...
- LeetCode OJ 101. Symmetric Tree
Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...
- java和.net的类比
原文地址在http://www.seguetech.com/blog/2013/06/03/dotnet-vs-java-how-to-pick
- 给php代码添加规范的注释phpDocumentor
给php代码添加规范的注释更多参考 http://phpdoc.org/docs/latest/index.html在phpdocumentor中,注释分为文档性注释和非文档性注释.所谓文档性注释,是 ...
- Entity Framework 学习高级篇1—改善EF代码的方法(上)
本节,我们将介绍一些改善EF代码的相关方法,如NoTracking,GetObjectByKey, Include等. l MergeOption.NoTracking 当我们只需要读 ...
- 路由器wan口连接不上的问题
路由器:tp-link:系统:win8.1:网络类型:PPPoE 克隆“当前管理PC的MAC地址”后,无法连接,将网线直接连到电脑上可以连接,所以问题一定出在路由上,经过不断探索发现问题所在,通过命令 ...
- 关于ckeditor过滤掉html样式标签之我见
1.CKEDITOR编辑器属性可以通过修改/ckeditor/config.js文件来控制 //标签过滤默认是开启的,默认会过了<style>样式标签设置为true可关闭过滤config. ...
- AJAX 在手机上用时
Response.ContentType = "text/html; text/plain; charset=UTF-8";
- linux fork()函数
C语言编程创建函数fork() 执行解析 | 浏览:1842 | 更新:2013-04-22 15:12 | 标签:c语言 概述 最近在看进程间的通信,看到了fork()函数,虽然以前用过,这次经过思 ...
- 【SQL】SQL
SQL基础 本文参照:http://www.w3school.com.cn/sql/ SQL 结构化查询语言(Structured Query Language). 对于大小写不敏感. SQL 使用单 ...