计蒜客 41387.XKC's basketball team-线段树(区间查找大于等于x的最靠右的位置) (The Preliminary Contest for ICPC Asia Xuzhou 2019 E.) 2019年徐州网络赛
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的方法
代码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年徐州网络赛的更多相关文章
- 计蒜客 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 ...
- 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 ...
- 计蒜客 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 ...
- 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); ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team (线段树)
题目链接:https://nanti.jisuanke.com/t/41387 题目大意:对于给定序列,求出对于每个位置求出比该数大于m的最靠右的位置. 思路:首先对序列进行离散化,然后对于每个数的下 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E. XKC's basketball team
题目链接:https://nanti.jisuanke.com/t/41387 思路:我们需要从后往前维护一个递增的序列. 因为:我们要的是wi + m <= wj,j要取最大,即离i最远的那个 ...
- 计蒜客 30996 - Lpl and Energy-saving Lamps - [线段树][2018ICPC南京网络预赛G题]
题目链接:https://nanti.jisuanke.com/t/30996 During tea-drinking, princess, amongst other things, asked w ...
- 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 ...
- The Preliminary Contest for ICPC Asia Xuzhou 2019 E XKC's basketball team(排序+二分)
这题其实就是瞎搞,稍微想一想改一改就能过. 排序按值的大小排序,之后从后向前更新node节点的loc值,如果后一个节点的loc大于(不会等于)前一个节点的loc,就把前一个节点的loc值设置为后面的l ...
随机推荐
- EF Core 迁移整理
创建迁移 PowerShell Add-Migration InitialCreate 多数据源 Add-Migration InitialCreate -Context MyDbContext -O ...
- Smack Extensions用户手册
Smack Extensions用户手册 XMPP协议包括基本协议和许多可选扩展,通常记录为“XEP”.Smack为核心XMPP协议提供了org.jivesoftware.smack包,为许多协议扩展 ...
- iOS测试中发现一个textview控制,使用clear()无法清除文字
iOS测试中发现一个textview控制,使用clear()无法清除
- 【开发笔记】-Ubuntu环境命令初始化
更新apt-get命令 apt-get update 安装yum命令 首先检测是否安装 build-essential 包 sudo apt-get install build-essential 安 ...
- Jackson动态JSON处理
https://www.baeldung.com/jackson-mapping-dynamic-object https://www.baeldung.com/jackson-deserializa ...
- web和网络基础
TCP/IP 协议族按层次分别分为以下 4 层: 应用层. 传输层. 网络层和数据链路层 把 TCP/IP 层次化是有好处的. 比如, 如果互联网只由一个协议统筹, 某个地方需要改变设计时, 就必须把 ...
- Typora优化-适合不懂CSS代码的小白
转载请注明出处:https://www.cnblogs.com/nreg/p/11116176.html 先来一张优化前与优化后的对比图: 优化前: 优化后: 1.通过 文件-偏好设置 打开主题文件 ...
- memcached的安装和学习
linux下安装memcached 第一首先要安装 libvevent ,用 rpm -qa | preg libevent ,用 which libevent 查看安装路径 以下图解: ...
- Kubernetes YAML 文件全字段详解
Kubernetes YAML 文件全字段详解 Deployment yaml 其中主要参数都在podTemplate 中,DaemonSet StatefulSet 中的pod部分一样. apiVe ...
- php迭代器模式(iterator pattern)
... <?php /* The iterator pattern is used to traverse a container and access its elements. In oth ...