HDU - 6621 K-th Closest Distance 主席树+二分答案
K-th Closest Distance
主席树第二波~
题意
给你\(n\)个数\(m\)个询问,问\(i\in [l,r]\)计算每一个\(|a_{i}-p|\)求出第\(k\)小
题目要求强制在线\(l = l \oplus ans、r = r \oplus ans、p = p \oplus ans、k = k \oplus ans\)(ans为上次询问的答案)
思路
- 二分答案\(ans\),找区间\(i\in[l,r], a_{i} \in [p-ans, p+ans]\)里的数量\(>= k\)
\(|a_{i} - p | = ans\)
\(a_{i} - p = ans、p - a_{i} = ans\)
\(a_{i} = ans + p、a_{i} = p - ans\) - 用主席树维护一下就\(ok\)了
AC代码
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <cfloat>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + 10;
const int maxm = 1e5 + 10;
const ll mod = 1e9 + 7;
const ll INF = 1e18 + 100;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-8;
using namespace std;
int n, m;
int cas, tol, T;
struct Node{
int l, r, cnt;
}node[maxn*40];
int w[maxn], a[maxn];
vector<int> v;
void init(){
v.clear();
tol = 0;
mes(w, 0);
}
void build(int l, int r, int rt){
rt = ++tol;
node[rt].cnt = 0;
if(l == r)
return;
int mid = l+r>>1;
build(l, mid, node[rt].l);
build(mid+1, r, node[rt].r);
}
void update(int l, int r, int &x, int y, int pos){
x = ++tol;
node[x] = node[y];
node[x].cnt++;
if(l == r)
return;
int mid = l+r>>1;
if(pos <= mid)
update(l, mid, node[x].l, node[y].l, pos);
else
update(mid+1, r, node[x].r, node[y].r, pos);
}
int query(int L, int R, int l, int r, int x, int y){
if(L <= l && r <= R)
return node[y].cnt - node[x].cnt;
int mid = l+r>>1, ans = 0;
if(L <= mid)
ans += query(L, R, l, mid, node[x].l, node[y].l);
if(R > mid)
ans += query(L, R, mid+1, r, node[x].r, node[y].r);
return ans;
}
int getid(int x){
return lower_bound(v.begin(), v.end(), x) - v.begin()+1;
}
int getid_(int x){
return upper_bound(v.begin(), v.end(), x) - v.begin()+1;
}
int main() {
scanf("%d", &T);
while(T--){
scanf("%d%d", &n, &m);
init();
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
v.push_back(a[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
int len = v.size()+1;
build(1, len, 1);
for(int i = 1; i <= n; i++)
update(1, n, w[i], w[i-1], getid(a[i]));
int ans = 0;
while(m--){
int L, R, p, k;
scanf("%d%d%d%d", &L, &R, &p, &k);
L ^= ans; R ^= ans; p^= ans;k ^= ans; //要记得^
int l = 0, r = 1e6;
while(l <= r){
int mid = (l+r)>>1;
int sum = query(getid(p-mid), getid_(p+mid)-1, 1, n, w[L-1], w[R]); //第一个getid和第二个不一样
if(sum >= k){
ans = mid;
r = mid-1;
}
else
l = mid+1;
}
printf("%d\n", ans);
}
}
return 0;
}
HDU - 6621 K-th Closest Distance 主席树+二分答案的更多相关文章
- HDU 6521 K-th Closest Distance (主席树+二分)
题意: 给你一个数组,q次询问,每次问你[l,r]范围内与p距离第k大的元素的与p的距离,强制在线 思路: 主席树提取出[l,r]内的权值线段树,然后二分与p的距离mid ask该权值线段树里[p-m ...
- 2019HDU多校第四场 K-th Closest Distance ——主席树&&二分
题意 给定 $n$ 个数,接下来有 $q$ 次询问,每个询问的 $l, r, p, k$ 要异或上一次的答案,才是真正的值(也就是强制在线).每次询问,输出 $[l, r]$ 内第 $k$ 小的 $| ...
- BZOJ 1926: [Sdoi2010]粟粟的书架(主席树,二分答案)
BZOJ 1926: [Sdoi2010]粟粟的书架(主席树,二分答案) 题意 : 给你一个长为\(R\)宽为\(C\)的矩阵,第\(i\)行\(j\)列的数为\(P_{i,j}\). 有\(m\)次 ...
- BZOJ5343[Ctsc2018]混合果汁——主席树+二分答案
题目链接: CTSC2018混合果汁 显然如果美味度高的合法那么美味度低的一定合法,因为美味度低的可选方案包含美味度高的可选方案. 那么我们二分一个美味度作为答案然后考虑如何验证? 选择时显然要贪心的 ...
- 【bzoj2653】【middle】【主席树+二分答案】
Description 一个长度为 n 的序列 a ,设其排过序之后为 b ,其中位数定义为 b[n/2] ,其中 a,b 从 0 开始标号 , 除法取下整. 给你一个长度为 n 的序列 s .回答 ...
- P4094 [HEOI2016/TJOI2016]字符串 后缀数组+主席树+二分答案
$ \color{#0066ff}{ 题目描述 }$ 佳媛姐姐过生日的时候,她的小伙伴从某东上买了一个生日礼物.生日礼物放在一个神奇的箱子中.箱子外边写了一个长为n的字符串s,和m个问题.佳媛姐姐必须 ...
- BZOJ 4556 [Tjoi2016&Heoi2016]字符串 ——后缀数组 ST表 主席树 二分答案
Solution 1: 后缀数组暴力大法好 #include <map> #include <cmath> #include <queue> #include &l ...
- BZOJ 2653: middle(主席树+二分答案)
传送门 解题思路 首先可以想到一种暴力做法,就是询问时二分,然后大于等于这个值的设为1,否则设为-1,然后就和GSS1那样统计答案.但是发现这样时间空间复杂度都很爆炸,所以考虑预处理,可以用主席树来做 ...
- POJ 6621: K-th Closest Distance(主席树 + 二分)
K-th Closest Distance Time Limit: 20000/15000 MS (Java/Others) Memory Limit: 524288/524288 K (Jav ...
随机推荐
- Linux学习篇(二)-软件包管理器、Yum 软件仓库
红帽软件包管理器 在红帽软件包管理器(rpm)公布之前,Linux 系统软件的安装只能采取"源码包"的方式安装,需要自行编译源码并解决许多依赖关系,所以软件的安装.升级.卸载的难度 ...
- zookeeper分布式锁用法
package com.example.demo3.zk; import lombok.extern.slf4j.Slf4j; import org.apache.storm.shade.org.ap ...
- git 时 出现 Permission denied (publickey).
https://blog.csdn.net/awp0011/article/details/73368481 第一次使用github.com在本地 执行 git clone git@github.co ...
- poj2010 Moo University - Financial Aid 优先队列
Description Bessie noted that although humans have many universities they can attend, cows have none ...
- MySQL5.7的搭建以及SSL证书
Centos7 安装MySQL 5.7 (通用二进制包) 1.1 下载软件包 https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.17-l ...
- C++ STL map容器值为指针时怎么释放内存
最近在使用STL中map时,遇到了一个问题,就是当map中值为指针对象时怎么释放内存? // 站点与TCP连接映射表 (key为ip_port_stationCode, value为 clientSo ...
- PHP_CodeIgniter Github实现个人空间
github支持github Pages 可以实现自己的个人空间 XXX.github.io/project 1 注册自己的github账户 2 需要设置自己的user_name, user_name ...
- 【学习总结】Python-3-Python数字类型转换
菜鸟教程-Python3-Python数字 Python3支持三种数值类型:整型int,浮点型float,复数complex 格式:将数字类型作为函数名即可,然后传入要转换的参数. int(x) 将x ...
- oooooooooooooooo
安装后打开mysqld配置项加入skip-grant-tables可以无密码登录,登录进去后修改密码修改成功删除skip-grant-tables mysql> select user, plu ...
- MySQL--10 日志简介
目录 一.MySQL日志简介 二.错误日志 三.一般查询日志 四.二进制日志 五.慢查询日志 一.MySQL日志简介 二.错误日志 作用: 记录mysql数据库的一般状态信息及报错信息,是我们对于数据 ...