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 ...
随机推荐
- openssl查看apk的证书信息
查看apk的证书信息: openssl pkcs7 -inform DER -in CERT.RSA -noout -print_certs -text
- 洛谷1101:单词方阵(DFS)
题目描述 给一n×nn \times nn×n的字母方阵,内可能蕴含多个"yizhong"单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着888个方向的任一方向,同一单词摆放 ...
- 《从Lucene到Elasticsearch:全文检索实战》学习笔记三
今天我给大家讲讲倒排索引. 索引是构成搜索引擎的核心技术之一,它在日常生活中是非常常见的,比如我看一本书的时候,我首先会看书的目录,通过目录可以快速定位到具体章节的页码,加快对内容的查询速度. 文档通 ...
- Go Example--格式化字符串
package main import ( "fmt" "os" ) type point struct { x, y int } func main() { ...
- Google - Reconstruct To Chain
/* 4. 给你一串input,比如: A -> B B -> C X -> Y Z -> X . . . 然后让你设计一个data structure来存这些关系,最后读完了 ...
- tomcat 端口修改和内存配置
端口号修改参考:https://jingyan.baidu.com/article/adc815139b12def722bf7377.html Tomcat内存溢出(windows) java.lan ...
- Java高级特性 第4节 输入输出流
一.使用I/O操作文件 关键步骤: 使用File类操作文件或目录属性 使用FileInputStream类读文本文件 使用FileOutputStram类写文本文件 使用BufferedReader类 ...
- 邮件报警以及服务端能否ping通客户端的小例子(三)
就这个小小的东西,弄了一天,弄的头晕眼花,毕竟第一次弄这个,记录下来,若干年之后,回看这些笔记,不知是什么样的感想,哈哈.我学一个东西的时候喜欢系统的来,一点一点的来,做这个的时候想法很 ...
- Linux内核分析第七次作业
分析Linux内核创建一个新进程的过程 Linux中创建进程一共有三个函数: 1. fork,创建子进程 2. vfork,与fork类似,但是父子进程共享地址空间,而且子进程先于父进程运行. 3. ...
- [JAVA]JAVA多线程实现方法之——实现Runnable接口
public class MultiThread { public static void main(String[] args) { Thread t1 = new Thread(new Threa ...