题目链接

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大 主席树的更多相关文章

  1. POJ-2104-K-th Number(区间第K大+主席树模板题)

    Description You are working for Macrohard company in data structures department. After failing your ...

  2. 计蒜客 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 ...

  3. POJ2761---Feed the dogs (Treap求区间第k大)

    题意 就是求区间第k大,区间 不互相包含. 尝试用treap解决一下 第k大的问题. #include <set> #include <map> #include <cm ...

  4. [hdu2665]Kth number(划分树求区间第k大)

    解题关键:划分树模板题. #include<cstdio> #include<cstring> #include<algorithm> #include<cs ...

  5. Count on a tree(SPOJ COT + 树上第k大 + 主席树 + LCA)

    题目链接:https://www.spoj.com/problems/COT/en/ 题目: 题意: 给你一棵有n个节点的树,求节点u到节点v这条链上的第k大. 思路: 我们首先用dfs进行建题目给的 ...

  6. HDU 3473 Minimum Sum (划分树求区间第k大带求和)(转)

    题意:在区间中找一个数,求出该区间每个数与这个数距离的总和,使其最小 找的数字是中位数(若是偶数个,则中间随便哪个都可)接着找到该区间比此数大的数的总和 区间中位数可以使用划分树,然后在其中记录:每层 ...

  7. POJ 2104 求序列里第K大 主席树裸题

    给定一个n的序列,有m个询问 每次询问求l-r 里面第k大的数字是什么 只有询问,没有修改 可以用归并树和划分树(我都没学过..囧) 我是专门冲着弄主席树来的 对主席树的建树方式有点了解了,不过这题为 ...

  8. 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 ...

  9. 【POJ】2104 K-th Number(区间k大+主席树)

    http://poj.org/problem?id=2104 裸题不说.主席树水过. #include <cstdio> #include <iostream> #includ ...

随机推荐

  1. linux基础之系统管理类命令

    系统管理类命令 1.reboot.halt.poweroff命令 基本介绍 reboot命令.halt命令.poweroff命令:都表示重启或者关闭系统 基本语法 reboot/halt/powero ...

  2. GP card规范学习笔记

    9.   APDU命令参考 9.1  总的编码规则 A.生命周期状态的编码 可执行的装载文件 b8 b7 b6 b5 b4 b3 b2 b1 含义 16进制命令  0 0 0 0 0 0 0 1 LO ...

  3. PythonStudy——函数的分类 Classification of functions

    # PEP8:python写代码的规范 def fn(n1, n2): """ 函数的文档注释 :param n1: 第一个数 :param n2: 第二个数 :retu ...

  4. range的新发现

    正向打印的时候 for i in range(2): print(i) 打印的结果 0 1 反向的时候 for i in range(2,-1,-1): print(i) 2 1 0 for i in ...

  5. HTTP客户端/服务端 POST/GET

    获取GET请求内容 实例 //引入模块var http=require('http');var urls=require('url');var util=require('util');//创建服务h ...

  6. hash 在 perl 中的用法(转载)

    Perl的数据结构中最有趣的一个特性是哈希(hash),它使得在数据片段之间建立键-值(key-value)关联成为可能.虽然这些哈希要远远比普通系统中以数字索引的数组用途更广,但是往往也会使初学者不 ...

  7. [UE4]ProgressBar,进度条

    准备好2张进度条图片 一.新建名为“testProgress”的UserWidget,添加一个名为“ProgressBar_0”的ProgressBar到默认容器Canvas Panel 二.进度条进 ...

  8. VRay(一)材质基础

    专业术语 漫反射(Diffuse reflection)高光反射(Specular reflection)折射(Refraction)菲涅耳反射(Fresnel reflection)次表面散射(Su ...

  9. 通过spark sql 将 hdfs上文件导入到mongodb

    功能:通过spark sql 将hdfs 中文件导入到mongdo 所需jar包有:mongo-spark-connector_2.11-2.1.2.jar.mongo-java-driver-3.8 ...

  10. java并发编程目录

    java并发编程目录 Java多线程基础:进程和线程之由来 JAVA多线程实现的四种方式 Java并发编程:线程间协作的两种方式:wait.notify.notifyAll和Condition Jav ...