K-th Closest Distance

HDOJ-6621

  • 本题可以使用线段树解决,结点存本结点对应的所有元素,并按照从小打到排序
  • 最后使用二分法求解答案。因为题目中有绝对值,所以需要使用两次查找,再相减和k比较
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
int n,m;
int l,r,p,k;
int l1,r1,p1,k1;
int x=0;
int arr[100005];
vector<int> v[400020];//注意这里的空间开4*n
void build(int node,int l,int r){
v[node].clear();
for(int i=l;i<=r;i++){
v[node].push_back(arr[i]);
}
sort(v[node].begin(),v[node].end());
if(l==r){
return;
}
int mid=(l+r)>>1;
build(node<<1,l,mid);
build(node<<1|1,mid+1,r);
}
int query(int node,int l,int r,int p,int q,int u){
if(p<=l&&q>=r){
vector<int>::iterator it=upper_bound(v[node].begin(),v[node].end(),u);
int cnt=it-v[node].begin();
return cnt;
}
int ans=0;
int mid=(l+r)>>1;
if(p<=mid){
ans+=query(node<<1,l,mid,p,q,u);
}
if(q>mid){
ans+=query(node<<1|1,mid+1,r,p,q,u);
}
return ans;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
x=0;
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&arr[i]);
}
build(1,1,n);
int maxs=*max_element(arr+1,arr+1+n);//这里不能卸载right那里,否则超时
for(int i=0;i<m;i++){
scanf("%d%d%d%d",&l1,&r1,&p1,&k1);
l=l1^x;r=r1^x;p=p1^x;k=k1^x;
int left=0,right=maxs;//max_element用来求数组中的一个最大值
while(right>=left){
int mid=(right+left)>>1;
int temp=query(1,1,n,l,r,p+mid)-query(1,1,n,l,r,p-mid-1);
if(temp>=k){
right=mid-1;
}else{
left=mid+1;
}
}
printf("%d\n",left);
x=left;
//cout<<left<<endl;
}
}
//system("pause");
return 0;
}

HDOJ-6621(线段树+二分法)的更多相关文章

  1. SPOJ Meteors - 可持久化线段树 - 二分法

    Byteotian Interstellar Union (BIU) has recently discovered a new planet in a nearby galaxy. The plan ...

  2. HDOJ 4417 - Super Mario 线段树or树状数组离线处理..

    题意: 同上 题解: 抓着这题作死的搞~~是因为今天练习赛的一道题.SPOJ KQUERY.直到我用最后一种树状数组通过了HDOJ这题后..交SPOJ的才没超时..看排名...时间能排到11名了..有 ...

  3. hdoj 4325 Flowers 线段树+离散化

    hdoj 4325 Flowers 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4325 思路: 直接线段树,按照花的开放区间的大小建树,要注意虽然 ...

  4. 线段树(单点更新) HDOJ 2795 Billboard

    题目传送门 /* 主要利用线段树求区间最值,sum[]代表位置可用空间 每次找到最大值的位置 功能:查询最靠前能容纳广告的位置 */ #include <cstdio> #include ...

  5. 线段树(单点更新)/树状数组 HDOJ 1166 敌兵布阵

    题目传送门 /* 线段树基本功能:区间值的和,修改某个值 */ #include <cstdio> #include <cstring> #define lson l, m, ...

  6. 线段树+树状数组+贪心 HDOJ 5338 ZZX and Permutations

    题目传送门 /* 题意:不懂... 线段树+树状数组+贪心:贪心从第一位开始枚举,一个数可以是循环节的末尾或者在循环节中,循环节(循环节内部是后面的换到前面,最前面的换到最后面).线段树维护最大值,树 ...

  7. 并查集+树链剖分+线段树 HDOJ 5458 Stability(稳定性)

    题目链接 题意: 有n个点m条边的无向图,有环还有重边,a到b的稳定性的定义是有多少条边,单独删去会使a和b不连通.有两种操作: 1. 删去a到b的一条边 2. 询问a到b的稳定性 思路: 首先删边考 ...

  8. 树链剖分+线段树 HDOJ 4897 Little Devil I(小恶魔)

    题目链接 题意: 给定一棵树,每条边有黑白两种颜色,初始都是白色,现在有三种操作: 1 u v:u到v路径(最短)上的边都取成相反的颜色 2 u v:u到v路径上相邻的边都取成相反的颜色(相邻即仅有一 ...

  9. 线段树(多棵) HDOJ 4288 Coder

    题目传送门 题意:集合,add x, del x, 求和 分析:首先,暴力可以过这题.用上线段树能大大降低时间的消耗,具体就是类似开了5棵线段树,每个节点都有5个空间,表示该区间的id%5后的和,区间 ...

随机推荐

  1. Educational Codeforces Round 86 (Div. 2)

    比赛链接:https://codeforces.com/contest/1342 A - Road To Zero 题意 有两个非负整数 x, y 以及两种操作: 支付 a 点代价使其中一个数加一或减 ...

  2. C. New Year Book Reading

    New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n b ...

  3. SpringCloud @RefreshScope标注的Bean在生命周期中怎么样的?

    @RefreshScope 前言 该文章是由上一篇引申而来的,上一篇文章描述了Nacos是怎么触发配置刷新的,接着来写有关@RefreshScope的一些东西,都是由DEBUG而来 上一篇 文章概览 ...

  4. 缓冲区溢出实验 6 exit(0)

    实验环境.代码.及准备 https://www.cnblogs.com/lqerio/p/12870834.html vul6 Vul6和vul2类似,可以覆盖foo的ebp的一字节.而这里有一个ex ...

  5. mssql数据库提权(xp_cmdshell)

    1.关于 "xp_cmdshell" "存储过程":其实质就是一个"集合",那么是什么样的结合呢,就是存储在SqlServer中预先定义好的 ...

  6. Vue Login Form Component

    Vue Login Form Component Account Login <template> <div> <slot></slot> <el ...

  7. 钓鱼教程 All In One

    钓鱼教程 All In One youtube https://www.youtube.com/results?search_query=钓鱼教程&sp=CAM%3D 钓鱼证 https:// ...

  8. js 实现各种数据结构 APP

    js 实现各种数据结构 APP 常见数据结构: 数组,队列,栈,堆,链表,集合,字典,散列表,树, 图 Array, Queue, Link, Collection, Set,Map, HashMap ...

  9. Raspberry Pi & Raspberry Pi 4

    Raspberry Pi & Raspberry Pi 4 pdf https://www.raspberrypi.org/magpi/issues/beginners-guide-2nd-e ...

  10. PWA & TWA

    PWA & TWA https://www.bilibili.com/video/av68082979/ Service Worker workbox.js https://developer ...