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【线段树查询】的更多相关文章

  1. 计蒜客 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 ...

  2. [单调队列]XKC's basketball team

    XKC's basketball team 题意:给定一个序列,从每一个数后面比它大至少 \(m\) 的数中求出与它之间最大的距离.如果没有则为 \(-1\). 题解:从后向前维护一个递增的队列,从后 ...

  3. ACM/ICPC 2018亚洲区预选赛北京赛站网络赛 D 80 Days (线段树查询最小值)

    题目4 : 80 Days 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 80 Days is an interesting game based on Jules Ve ...

  4. 数据结构1 线段树查询一个区间的O(log N) 复杂度的证明

    线段树属于二叉树, 其核心特征就是支持区间加法,这样就可以把任意待查询的区间$[L, R]$分解到线段树的节点上去,再把这些节点的信息合并起来从而得到区间$[L,R]$的信息. 下面证明在线段树上查询 ...

  5. PAT天梯赛练习题 L3-002. 堆栈(线段树查询第K大值或主席树)

    L3-002. 堆栈 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 大家都知道“堆栈”是一种“先进后出”的线性结构,基本操作有 ...

  6. TZOJ 4325 RMQ with Shifts(线段树查询最小,暴力更新)

    描述 In the traditional RMQ (Range Minimum Query) problem, we have a static array A. Then for each que ...

  7. codeforces 652C C. Foe Pairs(尺取法+线段树查询一个区间覆盖线段)

    题目链接: C. Foe Pairs time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. 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 ...

  9. Hotel 旅馆, 线段树查询,合并

    C. Hotel 旅馆 内存限制:256 MiB 时间限制:1000 ms 标准输入输出 题目类型:传统 评测方式:文本比较   题目描述 OIER最近的旅游计划,是到长春净月潭,享受那里的湖光山色, ...

随机推荐

  1. 037_自动添加防火墙规则,开启某些服务或端口(适用于 RHEL7)

    #!/bin/bash#设置变量定义需要添加到防火墙规则的服务和端口号#使用 firewall-cmd --get-services 可以查看 firewall 支持哪些服务 service=&quo ...

  2. 为一台全新的电脑构建JavaEE开发环境

    1.开发以外的常用软件 2.下载各种资源(x64,2017年8月)JDKhttp://www.oracle.com/technetwork/java/javase/downloads/jdk8-dow ...

  3. 百度翻译api初使用(很久没写python了,写几行玩玩)

    调用free api做做简易的翻译 这个是百度翻译api文档 http://api.fanyi.baidu.com/api/trans/product/apidoc 照着百度api给的文档向web服务 ...

  4. [python]函数默认参数顺序问题

    python 函数参数定义有四类: 1.必选参数:调用函数时候必须赋值的参数. a,须以正确的顺序传入函数b,调用时的数量必须和声明时的一样 def exa(x): return x #b作为参数进入 ...

  5. vue 使用 echart ,自定义样式案例

    1.vue 安装 echart 库 npm install echarts --save 2.vue代码 引入 let echarts = require("echarts/lib/echa ...

  6. JavaWeb_(Mybatis框架)动态sql_七

    系列博文: JavaWeb_(Mybatis框架)JDBC操作数据库和Mybatis框架操作数据库区别_一 传送门 JavaWeb_(Mybatis框架)使用Mybatis对表进行增.删.改.查操作_ ...

  7. msql数据库常用指令操作

    数据库指令 1.数据库指令 创建数据库:create database db_name; 删除数据库:drop database db_name; 显示数据库:show databases: 导出数据 ...

  8. Gi命令行操作

    一.本地库初始化 命令:git init 效果: 注意:.git 目录中存放的是本地库相关的子目录和文件,不要删除,也不要胡乱修改 二.设置签名 形式 用户名:user Email 地址:user@1 ...

  9. 如何确定哪个SMB客户端/会话在Server 2008R2 Windows文件服务器上打开了特定文件?

    参考: http://www.kbase101.com/question/54969.html NetworkOpenedFiles v1.25  https://www.nirsoft.net/ut ...

  10. Linux 文件存在程序找不到文件

    1. 编码格式 程序运行时的编码格式和传输到程序中参数的编码格式是否一致,可以在程序中打印日志进行验证: 2. 转义符 文件路径中存在转义符 3. 运行程序的用户身份 不同用户运行程序也可能导致编码格 ...