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

K-th Closest Distance

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


 

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

 

题意:给你一个长度为n的序列,有m个查询,每次查询给你l,r,p,k,让你求[l,r]区间中|p-a[i]|第k小的值。

思路:这种求区间第k小一想就会想到主席树。直接求不太好求,我们考虑二分枚举答案ans。如果答案ans符合要求,那么一定有[p-ans,p+ans]这个区间内的数的个数>=k.

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1e5+10;
const int maxm=1e6+10;
const int M=1e6;
int a[maxn],root[maxm],n,m,ans,cnt;
struct node{
	int l;
	int r;
	int num;
}tree[maxm*40];
void pushup(int cur)
{
	tree[cur].num=tree[tree[cur].l].num+tree[tree[cur].r].num;
}
void build(int &cur,int l,int r)
{
	cur=++cnt;
	tree[cur].num=0;
	if(l==r) return ;
	int m=(l+r)>>1;
	build(tree[cur].l,l,m);
	build(tree[cur].r,m+1,r);
}
void update(int &now,int last,int l,int r,int tar)
{
	now=++cnt;
	tree[now]=tree[last];
	tree[now].num++;
	if(l==r) return;
	int m=(l+r)>>1;
	if(tar<=m) update(tree[now].l,tree[last].l,l,m,tar);
	else update(tree[now].r,tree[last].r,m+1,r,tar);
}
int query(int x,int y,int L,int R,int l,int r)
{
	if(L<=l&&r<=R) return tree[y].num-tree[x].num;
	int m=(l+r)>>1,res=0;
	if(L<=m) res+=query(tree[x].l,tree[y].l,L,R,l,m);
 	if(R>m) res+=query(tree[x].r,tree[y].r,L,R,m+1,r);
 	return res;
}
int main()
{
	int t,l,r,p,k;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		cnt=ans=0;
		build(root[0],1,M);
		for(int i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			update(root[i],root[i-1],1,M,a[i]);
		}
		while(m--)
		{
			scanf("%d%d%d%d",&l,&r,&p,&k);
			l^=ans;r^=ans;p^=ans;k^=ans;
			int L=0,R=M;
			while(L<=R)
			{
				int mid=(L+R)>>1;
				int tmp=query(root[l-1],root[r],max(1,p-mid),min(M,p+mid),1,M);
				if(tmp>=k)
				{
					ans=mid;
					R=mid-1;
				}
				else L=mid+1;
			}
			printf("%d\n",ans);
		}
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个基于 Python 的 K-Means++ 类的示例代码: ```python import numpy as np class KMeansPP: def __init__(self, k): self.k = k def fit(self, X): n_samples, n_features = X.shape # Initialize centroids list and add the first random centroid centroids = [X[np.random.randint(n_samples)]] # Add remaining k-1 centroids for i in range(1, self.k): # Initialize distances list distances = [] for j in range(n_samples): # Compute distance between each sample and the nearest centroid min_dist = np.inf for centroid in centroids: dist = np.linalg.norm(X[j] - centroid) min_dist = min(min_dist, dist) distances.append(min_dist) # Add a new centroid randomly based on the distance weights distances_sum = sum(distances) distances = [dist / distances_sum for dist in distances] new_centroid = X[np.random.choice(n_samples, p=distances)] centroids.append(new_centroid) self.centroids = centroids # Perform K-Means clustering clusters = [[] for _ in range(self.k)] for sample in X: distances = [np.linalg.norm(sample - centroid) for centroid in self.centroids] closest_centroid_idx = np.argmin(distances) clusters[closest_centroid_idx].append(sample) self.clusters = clusters ``` 这个类的主要功能是进行 K-Means++ 聚类,其中 `k` 是簇的数量,`X` 是形状为 `(n_samples, n_features)` 的输入数据。在 `fit` 方法中,它首先从输入数据中选择一个随机初始中心点,然后根据每个样本到最近中心点的距离选择下一个中心点,并重复此过程直到选择 `k` 个中心点。然后,它使用这些中心点对输入数据进行 K-Means 聚类,并将每个样本分配到最近的中心点的簇中。 `centroids` 和 `clusters` 属性分别存储所选的中心点和生成的簇。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值