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. 关于C#对Xml数据解析

    首先进行简单说明Xml 与Html  和 XAML数据标签的差别. 1.Xml属于数据文本, 被设计为传输和存储数据,其焦点是数据的内容.它与json格式数据相似,可作为服务数据传输类型. 其中XML ...

  2. React 根据条件自动计算

    1.输入框 <Item {...formItemProps} label="留房日期"> {getFieldDecorator('date', { rules: [{ ...

  3. App.config 分开文件

    App.config 分开文件 分开文件里配置: <appSettings configSource="App_TestMachine.config"> App_Tes ...

  4. python基础05--深浅copy, set,bytes

    1.1 深浅 copy 1. =  赋值操作, lis1=[1,2,3]  list2 = list1  list1.append(4)  则list1,list2都变 赋值都指向同一个地址,改变一个 ...

  5. 突破Java面试-Redis集群模式的原理

    1 面试题 Redis集群模式的工作原理说一下?在集群模式下,key是如何寻址的?寻址都有哪些算法?了解一致性hash吗? 2 考点分析 Redis不断在发展-Redis cluster集群模式,可以 ...

  6. 12、微信小程序实现授权

    在index.wxml中: <!--index.wxml--> <view wx:if="{{isHide}}"> <view wx:if=" ...

  7. 记录下登录Xcode 时出现 please sign in with an app-specific....

    今天做到准备提交到APP store时,要用xcode提交,在xcode->open developer tool->application loader时,要登录自己的ID,我登录时输入 ...

  8. 转摘Python安装与环境变量的配置

    Python安装与环境变量的配置   python下载: Python安装包下载地址:http://www.python.org/ 根据实际的操作系统,安装合适的安装版本. Python安装: 本文以 ...

  9. springboot设置访问端口和项目路径

    找到,application.properties, 添加如下配置即可 server.port=8088server.servlet.context-path=/

  10. ML- 线性回归推导

    线性回归, 这部分算是我最为擅长的了, 真的不吹, 6年经验, 我高中时代就已经会推导了, 当然是最最小二乘法和统计学(假设检验, 参数分布等)的角度. 后来上了大学, 又是从最小二乘和统计学角度, ...