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

这道题目就是区间查找大于等于某个数的最靠右的位置。

线段树查询,大区间里找小区间。

两个版本的板子, 比赛的时候,l和r写反了,一直没过。。。

参考模板来源:

关于如何用线段树实现查找区间内第一个小于(大于)某一值x的方法

查询区间内第一个大于x的数

代码1:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=5e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 ll tree[maxn<<]; void pushup(int rt)
{
tree[rt]=max(tree[rt<<],tree[rt<<|]);
} void update(int p,ll val,int l,int r,int rt)
{
if(l==r){
tree[rt]=val;
return ;
} int m=(l+r)>>;
if(p<=m) update(p,val,lson);
else update(p,val,rson);
pushup(rt);
} /*
int get(ll val,int l,int r,int rt)
{
if(l==r){
return l;
} int m=(l+r)>>1;
if(tree[rt<<1|1]>=val) return get(val,rson);
if(tree[rt<<1] > val) return get(val,lson);
}
*/ /*
int query(int L,int R,ll val,int l,int r,int rt)
{
if(L>r||R<l){
return -1;
}
// if(l==r){
// if(tree[rt]>=val){
// return l;
// }
// else{
// return -1;
// }
// }
if(L<=l&&r<=R){
if(tree[rt]<val){
return -1;
}
else{
return get(val,l,r,rt);
}
} int m=(l+r)>>1;
int ret=query(L,R,val,rson);
if(ret!=-1){
return ret;
}
return query(L,R,val,lson);
}
*/ int query(int L,int R,ll val,int l,int r,int rt)
{
if(L>r||R<l){
return -;
}
if(l==r){
if(tree[rt]>=val){
return l;
}
else{
return -;
}
}
if(L<=l&&r<=R){
if(tree[rt]<val){
return -;
}
// else{
// return get(val,l,r,rt);
// }
} int m=(l+r)>>;
int ret=query(L,R,val,rson);
if(ret!=-){
return ret;
}
return query(L,R,val,lson);
} ll a[maxn];
int ans[maxn]; int main()
{
int n;
ll m;
scanf("%d%lld",&n,&m);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
update(i,a[i],,n,);
}
for(int i=;i<=n;i++){
int pos=query(i,n,a[i]+m,,n,);
// cout<<pos<<endl;
if(pos!=-) ans[i]=pos-i-;
else ans[i]=-;
}
for(int i=;i<n;i++){
printf("%d ",ans[i]);
}
printf("%d\n",ans[n]);
}

代码2:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<ctime>
#include<deque>
#include<iomanip>
#include<list>
#include<map>
#include<queue>
#include<set>
#include<stack>
#include<vector>
using namespace std;
typedef long long ll; const double PI=acos(-1.0);
const double eps=1e-;
const ll mod=1e9+;
const int inf=0x3f3f3f3f;
const int maxn=5e5+;
const int maxm=+;
#define ios ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 ll tree[maxn<<]; void pushup(int rt)
{
tree[rt]=max(tree[rt<<],tree[rt<<|]);
} void update(int p,ll val,int l,int r,int rt)
{
if(l==r){
tree[rt]=val;
return ;
} int m=(l+r)>>;
if(p<=m) update(p,val,lson);
else update(p,val,rson);
pushup(rt);
} int get(ll val,int l,int r,int rt)
{
if(l==r){
return l;
} int m=(l+r)>>;
if(tree[rt<<|]>=val) return get(val,rson);
if(tree[rt<<] > val) return get(val,lson);
} int query(int L,int R,ll val,int l,int r,int rt)
{
if(L>r||R<l){
return -;
}
// if(l==r){
// if(tree[rt]>=val){
// return l;
// }
// else{
// return -1;
// }
// }
if(L<=l&&r<=R){
if(tree[rt]<val){
return -;
}
else{
return get(val,l,r,rt);
}
} int m=(l+r)>>;
int ret=query(L,R,val,rson);
if(ret!=-){
return ret;
}
return query(L,R,val,lson);
} ll a[maxn];
int ans[maxn]; int main()
{
int n;
ll m;
scanf("%d%lld",&n,&m);
for(int i=;i<=n;i++){
scanf("%lld",&a[i]);
update(i,a[i],,n,);
}
for(int i=;i<=n;i++){
int pos=query(i,n,a[i]+m,,n,);
// cout<<pos<<endl;
if(pos!=-) ans[i]=pos-i-;
else ans[i]=-;
}
for(int i=;i<n;i++){
printf("%d ",ans[i]);
}
printf("%d\n",ans[n]);
}

计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛的更多相关文章

  1. 计蒜客 41391.query-二维偏序+树状数组(预处理出来满足情况的gcd) (The Preliminary Contest for ICPC Asia Xuzhou 2019 I.) 2019年徐州网络赛)

    query Given a permutation pp of length nn, you are asked to answer mm queries, each query can be rep ...

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

  3. 计蒜客 30996.Lpl and Energy-saving Lamps-线段树(区间满足条件最靠左的值) (ACM-ICPC 2018 南京赛区网络预赛 G)

    G. Lpl and Energy-saving Lamps 42.07% 1000ms 65536K   During tea-drinking, princess, amongst other t ...

  4. E.XKC's basketball team(The Preliminary Contest for ICPC Asia Xuzhou 2019)

    https://nanti.jisuanke.com/t/41387 解: 离散化+线段树. #define IOS ios_base::sync_with_stdio(0); cin.tie(0); ...

  5. The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team (线段树)

    题目链接:https://nanti.jisuanke.com/t/41387 题目大意:对于给定序列,求出对于每个位置求出比该数大于m的最靠右的位置. 思路:首先对序列进行离散化,然后对于每个数的下 ...

  6. The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team

    题目链接:https://nanti.jisuanke.com/t/41387 思路:我们需要从后往前维护一个递增的序列. 因为:我们要的是wi + m <= wj,j要取最大,即离i最远的那个 ...

  7. 计蒜客 30996 - Lpl and Energy-saving Lamps - [线段树][2018ICPC南京网络预赛G题]

    题目链接:https://nanti.jisuanke.com/t/30996 During tea-drinking, princess, amongst other things, asked w ...

  8. The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team [单调栈上二分]

    也许更好的阅读体验 \(\mathcal{Description}\) 给n个数,与一个数m,求\(a_i\)右边最后一个至少比\(a_i\)大\(m\)的数与这个数之间有多少个数 \(2\leq n ...

  9. The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team(排序+二分)

    这题其实就是瞎搞,稍微想一想改一改就能过. 排序按值的大小排序,之后从后向前更新node节点的loc值,如果后一个节点的loc大于(不会等于)前一个节点的loc,就把前一个节点的loc值设置为后面的l ...

随机推荐

  1. Netty高性能原理和框架架构解析

    1.引言 Netty 是一个广受欢迎的异步事件驱动的Java开源网络应用程序框架,用于快速开发可维护的高性能协议服务器和客户端. 本文基于 Netty 4.1 展开介绍相关理论模型,使用场景,基本组件 ...

  2. Git教程-安装与创建版本库

    Git是一个分布式版本控制系统,他通过命令行使用的工具,Github是提供Git仓库托管服务的网站 安装参考: https://www.liaoxuefeng.com/wiki/89604348802 ...

  3. HeRaNO's NOIP CSP Round Day 2 T1 building

    考试的时候居然睡着了... T1的60分做法很明显,3^n枚举每个状态并进行验证 (考试剩十分钟结束的时候我开始打,,不到五分钟就写完了? 暴力(60分) #include<bits/stdc+ ...

  4. Vue安装及项目介绍

    目录 创建Vue项目 环境安装 创建项目 pycharm打开Vue项目 项目目录介绍 入口文件(main.js) 路由配置(router.js ) 组件 前台路由的基本工作流程 目录结构 根组件(Ap ...

  5. JavaScript之变量(声明、解析、作用域)

    声明(创建) JavaScript 变量 在 JavaScript 中创建变量通常称为"声明"变量. 一.我们使用 var 关键词来声明变量: var carname; 变量声明之 ...

  6. 46、VUE + JS 面试宝典

    https://github.com/rohan-paul/Awesome-JavaScript-Interviewshttps://github.com/nieyafei/front-end-int ...

  7. 前端获取文件input框的美化操作

    前面我们说了一种利用input框和js的当时获取本地文件内容的情况-详细信息参考 2017年11月8日前端用js获取本地文件的内容 以上方式获取的按钮是系统默认的显示,有时候我们需要对按钮的外观进行美 ...

  8. android解析xml (pull)

    1. xml <persons> <person id="18"> <name>furong</name> <age>2 ...

  9. MongoDB分片,唯一索引与upsert

    前言 分片,唯一索引和upsert,表面上看似没有直接联系的几个东西,到底存在怎样的瓜葛呢? 分片 为了保持水平扩展的有效性,分片功能必须保证各个片之间没有直接关联,不需要与其他分片交互就可以独立做出 ...

  10. springboot+内置改为外置tomcat

    1.pom.xml springboot项目利用的是自己内置的tomcat,这边就是不依赖内置的tomcat,将其编译的作用域设置为provided <dependency> <gr ...