题目链接

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. 将文件夹中的图像路径自动生成txt文件(便于opencv遍历处理图像)

    代码: #include<iostream> #include<vector> #include<io.h> #include<fstream> usi ...

  2. Git图形化界面客户端

    Git图形化界面客户端大汇总 文,还在不断更新,网上搜到的同名文章都是未经同意就从这里复制过去的) 一.TortoiseGit - The coolest Interface to Git Versi ...

  3. Ubuntu16.04中pip无法更新升级,采用源码方式安装

    1.从pip官网下载最新版 https://pypi.org/project/pip/#files 2.ubuntu中创建文件位置,我的放在一下路径,之后进行解压 3.解压后进入pip的文件夹,在执行 ...

  4. DRBD搭建

    基于块设备在不同的高可用服务器之间同步和镜像数据的软件,块设备可以是磁盘分区,LVM逻辑卷或整块磁盘,解决磁盘单点故障 三种复制协议 (1)协议A:异步复制协议,本地写成功后立即返回,数据放在发送bu ...

  5. 列表:remove/del删除方法中的逻辑“误区”

    结果: list_1=["A","B","C","D","E","F",&quo ...

  6. linux scp传输文件命令

    scp  -r /opt/test root@192.168.2.105:/opt

  7. MVC 中Scripts.Render、Styles.Render

    在ASP.NET MVC项目中,可以在视图中利用Scripts.Render.Styles.Render统一加载js.css文件,需要利用BundleConfig类来Add 各种Bundle,例如:b ...

  8. 对JVM的简单了解

  9. [转]ANTS Performance Profiler和ANTS Memory Profiler 使用

    .NET性能调优之一:ANTS Performance Profiler的使用   .NET性能调优系列文章 系列文章索引 .NET性能调优之一:ANTS Performance Profiler的使 ...

  10. calibre的注册表残留删除

    卸载calibre后,注册表仍有残留 for /f %a in ('reg query HKEY_CLASSES_ROOT /f calibre /k') do reg delete %a /f