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. Mysql中的排序查询

    进阶3:排序查询 语法: select 查询列表 from 表 [where 筛选条件]order by 排序列表 [asc 升序 | desc降序] 例子 查询员工信息,要求工资从高到低 SELEC ...

  2. C# 调用Access数据库关于like模糊查询的写法

    在access查询视图中要使用"*"做模糊匹配,但是在程序中要用%来匹配.在access中:NEIBUBH like '*1234*'在程序中:NEIBUBH like '%123 ...

  3. APS.NET MVC + EF (04)---路由和数据传递

    4.1 视图引擎 ASP.NET MVC 提供两种视图引擎:ASPX(C#)和Razor(CSHTML),推荐使用Razor. 4.1.1 Razor的语法 在Razor视图中,所有的服务器端代码都是 ...

  4. maven项目整合SSM配置log4j, 实现控制台打印SQL语句

    在原有项目正常启动的情况下, 实现在控制台打印mapper包下SQL语句. 1.在pom.xml配置文件中添加两个依赖(缺一不可) <!--日志包--> <dependency> ...

  5. 【转载】华为荣耀V9的手机录屏功能如何开启

    手机录屏有时候对我们的帮助很大,例如可以录制相应的APP使用教程.微信小程序使用流量讲解视频等,针对于软件开发人员等来说,手机录屏功能针对功能演示视频非常的有帮助.在华为荣耀V9手机中,进行手机录屏有 ...

  6. Beego 学习笔记13:Api编写

    Api编写 1>     api常用的数据的格式有json和xml这两种. 2>     下面开始讲解不同的数据格式使用的方式 1->JSON 数据直接输出. 调用 ServeJSO ...

  7. js学习之面向对象

    一.创建对象的方法 1. {} 字面量创建 var person ={ name: "lisi", age: , say: function(){ alert(this.name) ...

  8. 13 ARM指令集与Thumb指令集

    指令格式 ARM基本格式 <opcode>{<cond>}{S}{.W|.N}<Rd>,<Rn>{,<operand2>} opecode: ...

  9. Linux 下 svn 多个项目多用户分配

    安装步骤如下: 1.yum install subversion 2.输入rpm -ql subversion查看安装位置,如下图: 输入 svn –help可以查看svn的使用方法 需求 开发服务器 ...

  10. 2.创建NHibernateHelper帮助类,生成sessionFactory

    接上一篇文章 使用FluentNHibemate 操作数据库,添加映射到数据库 http://www.cnblogs.com/fzxiaoyi/p/8443586.html 在Model文件下再创建个 ...