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最近的旅游计划,是到长春净月潭,享受那里的湖光山色, ...
随机推荐
- Python14__网络SOCKET
- 012_Linux驱动之_wait_event_interruptible
1. 首先这篇博客讲解得挺好的,推荐 wait_event_interruptible 使用方法 2 .函数原型: #define wait_event_interruptible(wq, condi ...
- Shell 06 awk
一.基本操作方法 ######################################### grep 按行进行查找 vim 编辑文档,交互式 ####################### ...
- [Luogu] 程序自动分析
题面:https://www.luogu.org/problemnew/show/P1955 题解:https://www.zybuluo.com/wsndy-xx/note/1143858
- go - GOPATH配置
查看配置信息 go env 然后找到对应的 GOPATH=>修改为你自己的目录 ..liunx 系统下,注意要给权限 go bulid 编译文件 go run 先编译后执行. -------- ...
- 数据结构实验之图论一:基于邻接矩阵的广度优先搜索遍历 (SDUT 2141)
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> ...
- vue中mixin的理解与用法
vue中提供了一种混合机制--mixins,用来更高效的实现组件内容的复用.最开始我一度认为这个和组件好像没啥区别..后来发现错了.下面我们来看看mixins和普通情况下引入组件有什么区别? 组件在引 ...
- Win10配置Java环境变量
很多同学在学习Java入门的时候被Java环境变量搞的一头雾水,今天这篇文章拓薪教育就来说一下如何在win10下配置环境变量; 下载jdk安装包: 首先我们需要下载jdk的安装包,这里提供jdk的安装 ...
- 用Ajax请求后台数据
我们先不讲ajax的原理,还是先以实战为主,看一下这个东西到底怎么用的? form表单: <%@ page language="java" contentType=" ...
- 通过xshell在本地win主机和远程linux主机传输文件
1.下载和安装xshell此处不再介绍 2.安装lrzsz的软件 yum install lrzsz 3.通过xshell上传文件 只需要在XShell的菜单中点击File – Transfer – ...