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 ...
随机推荐
- VUE开发SPA之微信授权登录
SPA单页应用中微信授权登录的一点思路 单页应用应该如何解决微信授权登录的尴尬跳转?后退无法返回?主要遇到的问题就是 先进入单页应用,一边渲染页面一边判断用户有没有登录,当判断到没有登录时异步数据请求 ...
- 同时开始了SQL。。。
SQL LIMIT OFFSET 和 LIMIT code1: SELECT id, name, score FROM table ORDER BY score DESC LIMIT OFFSET 4 ...
- Ubuntu16.04交叉工具链安装
前言: 开发环境是64位的ubuntu16.04,交叉工具链是通过sudo apt-get install ....安装的,移植uboot2014.10,但是很奇怪,按照网上的介绍在start.s里面 ...
- immutable.js使用总结
1. immutable相当于 JSON.parse 和 JSON.stringify: 2.引入redux中,除了 在最外层 reducer中 import { combineReducers } ...
- java_basic_基础
变量 类型 运算符 条件 循环 调试 字符串 数组 从键盘输入数据 switch的用法 从变量接收值 从键盘接收值 输出到一个文件 基本类型的赋值与引用类型的赋值是不一样的 是将引用类型的地址 每一个 ...
- 第一章 HTML+CSS(上)
HTML 网页的组成 HTML简介 HTML的语法 HTML的常用标签 HTML中的表格和表单 CSS的简单应用 我们这里使用WebStorm开发工具 配置浏览器 常用插件: CodeGlance 代 ...
- sql 50题
/* Navicat MySQL Data Transfer Source Server : localhost Source Server Version : 50717 Source Host : ...
- zabbix与tomcat(六)
一.zabbix监控远程tomcat的流程 Zabbix-server 找 zabbix Java Gateway获取Java数据 zabbix Java Gateway 找Java程序(zabb ...
- linux系统常用的基本命令分类
linux系统常用的基本命令分类: 文件命令:vim vimdiff diff mkdir touch rm mv cp ln cd ls more less head tail cat grep e ...
- [TFS教程]TFS: Get Command
Get Command Visual Studio 2012 Gets (downloads) either the latest version or a specified version o ...