计蒜客 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 ...
随机推荐
- 第四周(1):数据分布-Python实战
数据准备 数据集地址:http://jse.amstat.org/datasets/normtemp.dat.txt 数据集描述:总共只有三列:体温.性别.心率 数据集详细描述:Journal of ...
- prometheus搜索指标显示No datapoints found.
在指标能够在下拉框可以选择到的情况下,还有No datapoints found. 则考虑是时区的问题,详见官方issue https://github.com/prometheus/promethe ...
- PHP-FPM的相关知识的深度解释
一.需要搞清楚几个名词概念 1. CGI(Common Gateway Interface,CGI)通用网关接口, 是Web 服务器运行时外部程序的规范,按CGI 编写的程序可以扩展 ...
- "startIWDP": true
{ "platformName": "iOS", "platformVersion": "11.0", "au ...
- 突破Java面试-Redis集群模式的原理
1 面试题 Redis集群模式的工作原理说一下?在集群模式下,key是如何寻址的?寻址都有哪些算法?了解一致性hash吗? 2 考点分析 Redis不断在发展-Redis cluster集群模式,可以 ...
- iOS - 屏幕刷新 ADisplayLink
什么是CADisplayLink CADisplayLink是一个能让我们以和屏幕刷新率相同的频率将内容画到屏幕上的定时器.我们在应用中创建一个新的 CADisplayLink 对象,把它添加到一个r ...
- 来自GitHub的优秀开源项目系列
开发必看: 如何设计大型系统? 架构师技术图谱. 互联网Java工程师进阶扫盲 Java学习指南 Java工程师成神之路 有趣开源项目: 中华古诗词数据库 表情包博物馆
- vue组件6 使用vue添加样式
class绑定,内联样式 数组语法 :class="[stylename]" js:data{stylename:classname} 对象语法:class={stylena ...
- 13、vue如何解决跨域问题
开发环境:配置config文件夹中index.js文件: proxyTable: { '/api': { target: 'http://10.10.1.242:8245',//后端地址 // sec ...
- 智能制造进入下半场?APS如何进行优化
按照现在算法和计算机处理能力的发展,现在资源优化的方向已经逐渐摒弃,而是在更系统的“有限产能计划的”框架内一并解决产能和物料的问题. 我们所看到的新近涌现出来的很多APS系统.但碍于算法的复杂程度,在 ...