HDU2665 求区间第K大 主席树
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=2665
代码:
//#include<bits/stdc++.h>
#include<iostream>
#include<cmath>
#include<algorithm>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
#define lowbit(x) ((x)&(-x))
using namespace std;
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=1e5+;
typedef long long LL;
typedef unsigned long long ULL;
const LL mod=1e9+;
const ULL base=1e7+;
struct node{
int siz,lc,rc;
}a[maxn*];
int s[maxn];
struct nub{
int st,num;
bool operator<(const nub &u)const{
return num<u.num;
}
}u[maxn];
int tot=,rt[maxn];
void Build(int d,int l,int r){
a[d].siz=;
if(l==r){
return ;
}
int m=(l+r)>>;
a[d].lc=tot++;a[d].rc=tot++;
Build(a[d].lc,l,m);
Build(a[d].rc,m+,r);
}
void updata(int st,int d,int last,int l,int r){
a[d].siz=a[last].siz+;
if(l==r){
return ;
}
int mid=(l+r)>>;
if(st<=mid){
a[d].rc=a[last].rc;
a[d].lc=tot++;
updata(st,a[d].lc,a[last].lc,l,mid);
}
else if(st>mid){
a[d].lc=a[last].lc;
a[d].rc=tot++;
updata(st,a[d].rc,a[last].rc,mid+,r);
}
}
int query(int k,int last,int now,int l,int r){
if(l==r){
return l;
}
int mid=(l+r)>>;
if(k<=a[a[now].lc].siz-a[a[last].lc].siz){
return query(k,a[last].lc,a[now].lc,l,mid);
}
else{
return query(k-(a[a[now].lc].siz-a[a[last].lc].siz),a[last].rc,a[now].rc,mid+,r);
}
}
int b[maxn];
int main(){
int n,m,t;
scanf("%d",&t);
while(t--){
tot=;
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&u[i].num);
u[i].st=i;
}
sort(u+,u++n);
int cnt=;
s[cnt]=u[].num;
b[u[].st]=;
for(int i=;i<=n;i++){
if(u[i].num!=u[i-].num){
cnt++;
s[cnt]=u[i].num;
}
b[u[i].st]=cnt;
}
rt[]=tot++;
Build(rt[],,cnt);
for(int i=;i<=n;i++){
rt[i]=tot++;
updata(b[i],rt[i],rt[i-],,cnt);
}
int l,r,k;
for(int i=;i<=m;i++){
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",s[query(k,rt[l-],rt[r],,cnt)]);
}
}
system("pause");
return ;
}
HDU1007
http://acm.hdu.edu.cn/showproblem.php?pid=1007
方差优化:
#include<bits/stdc++.h>
#define fi first
#define se second
#define INF 0x3f3f3f3f
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pqueue priority_queue
#define NEW(a,b) memset(a,b,sizeof(a))
#define lowbit(x) ((x)&(-x))
const double pi=4.0*atan(1.0);
const double e=exp(1.0);
const int maxn=2e5+;
typedef long long LL;
typedef unsigned long long ULL;
const LL mod=1e9+;
const ULL base=1e7+;
using namespace std;
int kdd;
struct node{
int id,di;
double g[];
bool operator<(const node &u)const{
return g[kdd]<u.g[kdd];
}
}kdt[maxn<<],data[maxn];
bool flag[maxn<<];
double dis(node a,node b){
return (a.g[]-b.g[])*(a.g[]-b.g[])+(a.g[]-b.g[])*(a.g[]-b.g[]);
}
double fx(int l,int r,int j){
double as=0.0;
for(int i=l;i<=r;i++){
as+=data[i].g[j];
}
as/=(double)(r-l+);
double f=0.0;
for(int i=l;i<=r;i++){
f+=(data[i].g[j]-as)*(data[i].g[j]-as);
}
f/=(double)(r-l+);
return f;
}
void build(int l,int r,int rt){
if(l>r) return ;
if(fx(l,r,)>fx(l,r,)){
kdd=;
}
else{
kdd=;
}
flag[rt]=;
int mid=(l+r)>>;
nth_element(data+l,data+mid,data+r+);
kdt[rt]=data[mid];
kdt[rt].di=kdd;
flag[rt<<]=flag[rt<<|]=;
if(l<=mid-){
build(l,mid,rt<<);
}
if(mid+<=r){
build(mid+,r,rt<<|);
}
}
pair<double,node> ans;
double mi;
void query(int rt,node p){
int kd=kdt[rt].di;
pair<double,node> now={dis(p,kdt[rt]),kdt[rt]};
int x=rt<<;
int y=rt<<|;
if(p.g[kd]>=kdt[rt].g[kd]){
swap(x,y);
}
if(flag[x])
query(x,p);
bool ff=;
if(ans.fi==-1.0){
ff=;
if(p.id!=kdt[rt].id){
ans=now;
}
}
else{
if(p.id!=kdt[rt].id&&(now.fi<ans.fi||now.fi==ans.fi&&now.se.id<ans.se.id)){
ans=now;
}
if((kdt[rt].g[kd]-p.g[kd])*(kdt[rt].g[kd]-p.g[kd])<ans.fi){
ff=;
}
}
if(flag[y]&&ff){
query(y,p);
}
}
int main(){
int n;
while(~scanf("%d",&n)&&n!=){
for(int i=;i<=n;i++){
for(int j=;j<;j++){
scanf("%lf",&data[i].g[j]);
}
data[i].id=i;
}
build(,n,);
mi=1000000000.0;
for(int i=;i<=n;i++){
ans.fi=-1.0;
query(,data[i]);
mi=min(mi,sqrt(ans.fi));
}
mi/=2.0;
printf("%.2lf\n",mi);
}
}
HDU2665 求区间第K大 主席树的更多相关文章
- POJ-2104-K-th Number(区间第K大+主席树模板题)
Description You are working for Macrohard company in data structures department. After failing your ...
- 计蒜客 38229.Distance on the tree-1.树链剖分(边权)+可持久化线段树(区间小于等于k的数的个数)+离散化+离线处理 or 2.树上第k大(主席树)+二分+离散化+在线查询 (The Preliminary Contest for ICPC China Nanchang National Invitational 南昌邀请赛网络赛)
Distance on the tree DSM(Data Structure Master) once learned about tree when he was preparing for NO ...
- POJ2761---Feed the dogs (Treap求区间第k大)
题意 就是求区间第k大,区间 不互相包含. 尝试用treap解决一下 第k大的问题. #include <set> #include <map> #include <cm ...
- [hdu2665]Kth number(划分树求区间第k大)
解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...
- Count on a tree(SPOJ COT + 树上第k大 + 主席树 + LCA)
题目链接:https://www.spoj.com/problems/COT/en/ 题目: 题意: 给你一棵有n个节点的树,求节点u到节点v这条链上的第k大. 思路: 我们首先用dfs进行建题目给的 ...
- HDU 3473 Minimum Sum (划分树求区间第k大带求和)(转)
题意:在区间中找一个数,求出该区间每个数与这个数距离的总和,使其最小 找的数字是中位数(若是偶数个,则中间随便哪个都可)接着找到该区间比此数大的数的总和 区间中位数可以使用划分树,然后在其中记录:每层 ...
- POJ 2104 求序列里第K大 主席树裸题
给定一个n的序列,有m个询问 每次询问求l-r 里面第k大的数字是什么 只有询问,没有修改 可以用归并树和划分树(我都没学过..囧) 我是专门冲着弄主席树来的 对主席树的建树方式有点了解了,不过这题为 ...
- G - KiKi's K-Number(树状数组求区间第k大)
For the k-th number, we all should be very familiar with it. Of course,to kiki it is also simple. No ...
- 【POJ】2104 K-th Number(区间k大+主席树)
http://poj.org/problem?id=2104 裸题不说.主席树水过. #include <cstdio> #include <iostream> #includ ...
随机推荐
- hadoop 单机模式 伪分布式 完全分布式区别
1.单机(非分布式)模式 这种模式在一台单机上运行,没有分布式文件系统,而是直接读写本地操作系统的文件系统,一般仅用于本地MR程序的调试 2.伪分布式运行模式 这种模式也是在一台单机上运行,但用不同的 ...
- 使用Log4net 日志系统
官方文档 http://logging.apache.org/log4net/release/config-examples.html C# 项目中直接使用nuget,下载Apache的log4net ...
- java初始重点语法
第三章 if基本语法: if(条件){// 表达式 // 代码块 } eg: int a = 10; if(a > 1){ System.out.println("内容"); ...
- Getting Visual Studio version of a Solution file
VS 6.0 -> 6.0 VS 2002 -> 7.0 VS 2003 -> 8.0 VS 2005 -> 9.0 VS 2008 -> 10.0 VS 2010 -& ...
- pxe+Kickstart自动装机补充知识点
1.vmlinuzvmlinuz是可引导的.压缩的内核.“vm”代表“Virtual Memory”.Linux 支持虚拟内存,不像老的操作系统比如DOS有640KB内存的限制.Linux能够使用硬盘 ...
- maya中MFnMesh.h使用说明的翻译
由于最近要修改一个maya中的deformer脚本,于是开始系统学习openMaya的一些知识,当然少不了得把一堆头文件说明看一遍.首先把MFnMesh.h这个文件翻译一下吧,不废话,上译文: 首先M ...
- sql server 2016 附加 其它目录的数据库
如果数据库不在默认目录,那么需要将 mdf所在目录或者 mdf文件 添加 用户 [NT SERVICE\MSSQLSERVER]的创建权限,否则会提示没有权限, 具体详见: https://docs. ...
- bananapi+OLED做的一个打地鼠游戏,c语言编程
说明一下:BPI是对拍死的BPI的计数,对应最终的成绩RANK是难度 数值越低难度越高 每当打死10个BPI以后就会减一即难度高一级 默认初始化RANK等于15 DIE是存在的BPI数量,一旦数量大于 ...
- SpringSecurity-UsernamePasswordAuthenticationFilter的作用
UsernamePasswordAuthenticationFilter应该是我们最关注的Filter,因为它实现了我们最常用的基于用户名和密码的认证逻辑. 先看一下一个常用的form-login配置 ...
- Python——python读取xml实战,作业6(python programming)
cd_catalog.xml <?xml version="1.0" encoding="ISO-8859-1"?> <!-- Edited ...