XKC's basketball team【线段树查询】
XKC , the captain of the basketball team , is directing a train of nn team members. He makes all members stand in a row , and numbers them 1 \cdots n1⋯n from left to right.
The ability of the ii-th person is w_iwi , and if there is a guy whose ability is not less than w_i+mwi+m stands on his right , he will become angry. It means that the jj-th person will make the ii-th person angry if j>ij>i and w_j \ge w_i+mwj≥wi+m.
We define the anger of the ii-th person as the number of people between him and the person , who makes him angry and the distance from him is the longest in those people. If there is no one who makes him angry , his anger is -1−1 .
Please calculate the anger of every team member .
Input
The first line contains two integers nn and m(2\leq n\leq 5*10^5, 0\leq m \leq 10^9)m(2≤n≤5∗105,0≤m≤109) .
The following line contain nn integers w_1..w_n(0\leq w_i \leq 10^9)w1..wn(0≤wi≤109) .
Output
A row of nn integers separated by spaces , representing the anger of every member .
样例输入复制
6 1
3 4 5 6 2 10
样例输出复制
4 3 2 1 0 -1 题目大意:
1.给出一个长度为n的数列,给出m的值。问在数列中从右到左第一个比 arr[i] + m 大的值所在位置与 arr[i] 的所在位置之间夹着多少个数。
解题思路:
1.用线段树来记录区间最大值,优先从右子树开始查询是否存在大于 arr[i] + m的值,返回下标即该值在原数列中的位置,即可求得答案。
2.重要的是查询的值必须是在 i 的右边,否则输出 -1
代码如下:
#include<stdio.h>
#include<algorithm>
using namespace std;
const int MAXN = 5e5 + ; int n, m;
int a[MAXN], ANS[MAXN]; struct Tree
{
int val, l, r;
}tree[ * MAXN]; void build(int k, int l, int r)
{
tree[k].l = l, tree[k].r = r;
if(l == r)
{
tree[k].val = a[l];
return ;
}
int mid = (l + r) / ;
build( * k, l, mid);
build( * k + , mid + , r);
tree[k].val = max(tree[ * k].val, tree[ * k + ].val);
} int query(int k, int goal)
{
if(tree[k].l == tree[k].r)
return tree[k].l;
if(tree[ * k + ].val >= goal)
return query( * k + , goal);
else if(tree[ * k].val >= goal)
return query( * k, goal);
else
return -;
} int main()
{
scanf("%d%d", &n, &m);
for(int i = ; i <= n; i ++)
scanf("%d", &a[i]);
build(, , n); //线段树建树
for(int i = ; i <= n; i ++)
{
int flag = ;
int ans = query(, a[i] + m); //由右边开始查询,返回右边第一个大于 a[i] + m值的位置
if(ans == - || ans <= i)
ANS[i] = -;
else if(ans > i)
ANS[i] = ans - i - ;
}
printf("%d", ANS[]);
for(int i = ; i <= n; i ++)
printf(" %d", ANS[i]);
printf("\n");
return ;
}
XKC's basketball team【线段树查询】的更多相关文章
- 计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛
		
XKC's basketball team XKC , the captain of the basketball team , is directing a train of nn team mem ...
 - [单调队列]XKC's basketball team
		
XKC's basketball team 题意:给定一个序列,从每一个数后面比它大至少 \(m\) 的数中求出与它之间最大的距离.如果没有则为 \(-1\). 题解:从后向前维护一个递增的队列,从后 ...
 - ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 80 Days (线段树查询最小值)
		
题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...
 - 数据结构1 线段树查询一个区间的O(log N) 复杂度的证明
		
线段树属于二叉树, 其核心特征就是支持区间加法,这样就可以把任意待查询的区间$[L, R]$分解到线段树的节点上去,再把这些节点的信息合并起来从而得到区间$[L,R]$的信息. 下面证明在线段树上查询 ...
 - PAT天梯赛练习题 L3-002. 堆栈(线段树查询第K大值或主席树)
		
L3-002. 堆栈 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家都知道“堆栈”是一种“先进后出”的线性结构,基本操作有 ...
 - TZOJ 4325 RMQ with Shifts(线段树查询最小,暴力更新)
		
描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each que ...
 - codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)
		
题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...
 - The Preliminary Contest for ICPC Asia Xuzhou 2019 徐州网络赛 XKC's basketball team
		
XKC , the captain of the basketball team , is directing a train of nn team members. He makes all mem ...
 - Hotel 旅馆,  线段树查询,合并
		
C. Hotel 旅馆 内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较 题目描述 OIER最近的旅游计划,是到长春净月潭,享受那里的湖光山色, ...
 
随机推荐
- ZooKeeper - 攘外安内的多面管理员
			
Zookeeper 作为分布式协调框架,在实际生产中有着不可替代的位置,它作为管理框架,大多数时间是不需要我们去维护的.也正是由于它是协调全局的管理者,所以它的一些内部原理我们还是要了解的. 今天我们 ...
 - python第三方库的更新和安装指定版本
			
安装指定版本: pip install openpyxl==2.3.4 更新到最新版本: pip install --upgrade openpyxl
 - poi操作excel之: 将NUMERIC转换成TEXT
			
一.NUMERIC TO TEXT(生成excel)代码生成一个excel文件: public static void generateExcel() throws Exception { XSSFW ...
 - loj#6285 数列分块入门 9   (  回 滚 )
			
题目 : 链接 :https://loj.ac/problem/6285 题意:给出一个长为 n的数列,以及 n个操作,操作涉及询问区间的最小众数. 思路:虽然这不是一道 回滚莫队题,就是 暴力分块 ...
 - AGC023C Painting Machines
			
题意 有一排\(n\)个格子,\(i\)操作会使\(i\)和\(i+1\)都变黑. 一个操作序列的得分为染黑所有格子时所用的步数 问所有排列的得分和. \(n\le 10^6\) 传送门 思路 有一个 ...
 - linux C file format analysis
			
c语言文件格式 source file file.c C source, ASCII text pretreatment 预处理文件 file.i C source, ASCII text assem ...
 - arcgis python  列出一个表所有字段
			
import arcpy inFeature = arcpy.GetParameterAsText(0) #原始数据 try: fieldList = arcpy.ListFields(inFeatu ...
 - Facebook币Libra学习-6.发行属于自己的代币Token案例(含源码)
			
在这个简短的概述中,我们描述了我们在eToro标记化资产背后实施技术的初步经验,即MoveIR语言中的(eToken),用于在Libra网络上进行部署. Libra协议是一个确定性状态机,它将数据存储 ...
 - Facebook币Libra学习-2.交易生命周期
			
交易生命周期 为了更加深入的理解Libra的交易生命周期,我们将跟随一个交易的全过程,从其被提交到Libra validator始,直至其被添加到区块链上止.我们将“放大”来看每个validator逻 ...
 - 【matlab】模拟变焦拼接代码备份
			
1.初版,边缘未处理. % % In----near % If----far % In=imread('D:\文件及下载相关\桌面\模拟变焦拼接\Matlab_code\nearframe\frame ...