题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6621

K-th Closest Distance

Time Limit: 20000/15000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)
Total Submission(s): 2426    Accepted Submission(s): 871

Problem Description
You have an array: a1, a2, , an and you must answer for some queries.
For each query, you are given an interval [L, R] and two numbers p and K. Your goal is to find the Kth closest distance between p and aL, aL+1, ..., aR.
The distance between p and ai is equal to |p - ai|.
For example:
A = {31, 2, 5, 45, 4 } and L = 2, R = 5, p = 3, K = 2.
|p - a2| = 1, |p - a3| = 2, |p - a4| = 42, |p - a5| = 1.
Sorted distance is {1, 1, 2, 42}. Thus, the 2nd closest distance is 1.
Input
The first line of the input contains an integer T (1 <= T <= 3) denoting the number of test cases.
For each test case:
冘The first line contains two integers n and m (1 <= n, m <= 10^5) denoting the size of array and number of queries.
The second line contains n space-separated integers a1, a2, ..., an (1 <= ai <= 10^6). Each value of array is unique.
Each of the next m lines contains four integers L', R', p' and K'.
From these 4 numbers, you must get a real query L, R, p, K like this: 
L = L' xor X, R = R' xor X, p = p' xor X, K = K' xor X, where X is just previous answer and at the beginning, X = 0.
(1 <= L < R <= n, 1 <= p <= 10^6, 1 <= K <= 169, R - L + 1 >= K).
Output
For each query print a single line containing the Kth closest distance between p and aL, aL+1, ..., aR.
Sample Input
1 5 2 31 2 5 45 4 1 5 5 1 2 5 3 2
Sample Output
0 1
Source
2019 Multi-University Training Contest 4
题目大意:T组样例,N个数 M次询问,L,R,P,K  问你L,R区间内 距离P第K近的距离是多少
思路:比赛的时候没啥思路,没写,看题解发现,主席树可以求区间内<=K的数有多少个  但是我们要求的是与某个值相减绝对值第K小啊,这有什么关系吗?
假设我们知道答案是几,那么此时求出来L,R区间内的数与P相减第K小是不是就是我们这个答案呢? 显然是的,但是问题是我们不知道答案啊
这就是二分的思想了,二分答案,查询范围在P-ans,P+ans之间的数有多少个,如果个数大于等于K的话,证明我们的ans还可以减小,一直这样下去
就会得到正确的答案了
看代码:
#include<iostream>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<math.h>
using namespace std;
typedef long long LL;
const int maxn=1e5+5;
int N,M;
int sz,cnt=0,sum=0;
int root[maxn],a[maxn],v[maxn];
int L,R,P,K,X=0;
struct Node
{int l,r,sum;Node(){l=r=sum=0;}
}T[maxn<<6];
void Init()
{cnt=0;root[0]=0;T[cnt].l=T[cnt].r=T[cnt].sum=0;X=0;
}
void Read(int &n)//只能读入整数
{n=0;int f=1;//用于记录正负char ch=getchar();while(ch<'0'||ch>'9')//
    {if(ch=='-') f=-1;ch=getchar();}while(ch>='0'&&ch<='9')//下面两种用法都可以
    {//n=(n<<1)+(n<<3)+(ch-'0');//
        n=(n<<1)+(n<<3)+(ch^48);//相当于  x*10+ch-'0';ch=getchar();}n=n*f;//切记乘以f  (记录的是+ -)return ;
}
void Update(int l,int r,int &x,int y,int pos)
{T[++cnt]=T[y];T[cnt].sum++;x=cnt;if(l==r) return ;int mid=(l+r)>>1;if(pos<=mid) Update(l,mid,T[x].l,T[y].l,pos);else Update(mid+1,r,T[x].r,T[y].r,pos);
}
int getid(int x)
{return lower_bound(v+1,v+1+sz,x)-(v);
}
void Query(int l,int r,int x,int y,int h)
{
//    cout<<"l:"<<l<<" r:"<<r<<" sum:"<<sum<<endl;if(l==r){if(h>=v[l]) sum+=T[x].sum-T[y].sum;return ;}int mid=(l+r)>>1;if(h<=v[mid]) Query(l,mid,T[x].l,T[y].l,h);else{sum+=T[T[x].l].sum-T[T[y].l].sum;Query(mid+1,r,T[x].r,T[y].r,h);}
}
bool judge(int x)
{sum=0;Query(1,sz,root[R],root[L-1],P+x);int sum1=sum;sum=0;Query(1,sz,root[R],root[L-1],P-x-1);int sum2=sum;return sum1-sum2>=K;
}
int main()
{int T;
//    scanf("%d",&T);
Read(T);int ca=1;while(T--){Init();
//        scanf("%d%d",&N,&M);
Read(N);Read(M);for(int i=1;i<=N;i++){scanf("%d",&a[i]);v[i]=a[i];}sort(v+1,v+1+N);sz=unique(v+1,v+1+N)-(v+1);
//        for(int i=1;i<=sz;i++) cout<<v[i]<<" ";cout<<endl;
//        for(int i=1;i<=N;i++) cout<<getid(a[i])<<" ";cout<<endl;for(int i=1;i<=N;i++) Update(1,sz,root[i],root[i-1],getid(a[i]));while(M--){sum=0;
//            scanf("%d%d%d%d",&L,&R,&P,&K);
Read(L);Read(R);Read(P);Read(K);L^=X;R^=X;P^=X;K^=X;int l=0,r=1e6,ans;while(l<=r)//二分答案
            {int mid=(l+r)>>1;if(judge(mid)){ans=mid;r=mid-1;}else l=mid+1;}printf("%d\n",ans);X=ans;}}return 0;
}

转载于:https://www.cnblogs.com/caijiaming/p/11289099.html

K-th Closest Distance相关推荐

  1. K-th Closest Distance HDU - 6621(第k小绝对值+主席树+二分)

    You have an array: a1, a2, , an and you must answer for some queries. For each query, you are given ...

  2. 杭电多校第四场-H- K-th Closest Distance

    题目描述 You have an array: a1, a2, , an and you must answer for some queries. For each query, you are ...

  3. HDU - 6621 K-th Closest Distance——主席树+二分

    [题目描述] HDU - 6621 K-th Closest Distance [题目分析] 因为看到第kkk大的要求,刚开始的时候一直都在想怎么运用第kkk大来解决问题,但是后来看其他人的博客才发现 ...

  4. 2019 Multi-University Training Contest 4 - K-th Closest Distance

    主席树 + 二分答案 对于这种区间内的值域问题一般用主席树进行求解. 因为数据范围只有1e6,所以不用离散化,直接建树即可. 题目要求找到区间内离p第k近的数,可以想到,这个问题具有单调性(某个区间长 ...

  5. 斯坦福CS231n项目实战(一):k最近邻(kNN)分类算法

    我的网站:红色石头的机器学习之路 我的CSDN:红色石头的专栏 我的知乎:红色石头 我的微博:RedstoneWill的微博 我的GitHub:RedstoneWill的GitHub 我的微信公众号: ...

  6. knn 邻居数量k的选取_选择K个最近的邻居

    knn 邻居数量k的选取 Classification is more-or-less just a matter of figuring out to what available group so ...

  7. scikit-learn K近邻法类库使用小结

    1. scikit-learn 中KNN相关的类库概述 在scikit-learn 中,与近邻法这一大类相关的类库都在sklearn.neighbors包之中.KNN分类树的类是KNeighborsC ...

  8. [机器学习-Sklearn]K-means(K均值)学习与总结

    K-means总结 前言 一,k-means算法 二,k的选择(仅供参考) 1.肘部法则 2. 根据实际应用的目的选择K 三,代码讲解 四, K值的确定的代码 五, 相同数据下用K-means分成3个 ...

  9. 机器学习(三)k均值聚类

    k均值聚类 原文地址:http://blog.csdn.net/hjimce/article/details/45200985 作者:hjimce 高斯混合模型和k均值聚类是聚类算法中的两种比较常用而 ...

最新文章

  1. 【C++】lambda 表达式
  2. 软件测试——进程调度(短作业优先调度算法+先来先服务算法)测试
  3. 四中方式实现单例模式
  4. verilog设置24进制计数器_阅读笔记:《Verilog HDL入门》第3章 Verilog语言要素
  5. 图解Android 内存分析工具之Mat使用教程
  6. Python实现红黑树的删除操作
  7. mysql中清空数据库,并重置主键为1
  8. 运营商管道的精细化运营之路
  9. 数据结构与算法分析——Hash表
  10. 各种软件系统架构图解析
  11. 计算机应用能力考试湖南成绩查询,湖南计算机等级考试成绩查询入口
  12. win10系统连接不上服务器,win10系统电驴连接不上服务器的解决方法
  13. 使用DreamweaverMX2004的搜索替换功能提高工作效率。
  14. 中创软件哪个部分是外包_什么是外包| 第2部分
  15. Codeforces 1324D(红黑树+求指定区间中大于指定值的个数)
  16. Java通过javacsv实现读取csv文件数据
  17. cocos creator 牌面翻转
  18. 黑马程序员————高新技术————类加载器
  19. ElasticSearch6.5.4快速入门
  20. Base64与MD5(数据加密)与ValidateCode(验证码)

热门文章

  1. asp,net 读写cookie(个人笔记)
  2. 引用opencv异常
  3. Java异常处理之InvocationTargetException(反射异常)
  4. 【推荐实践】推荐技术在旅游电商中的应用及挑战.pdf(附下载链接)
  5. 【报告分享】见实私域流量白皮书:私域流量案例实操手册.pdf
  6. NoSQL技术入门简介
  7. ACM做题过程中的一些小技巧
  8. 【采访】腾讯社交广告高校算法大赛第二周周冠军——Groot 比赛经验及心得分享
  9. 知识蒸馏在推荐精排中的应用与实践
  10. java支付宝支付_Java 高并发环境下的性能优化,揭秘支付宝技术内幕