ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2019icpc 徐州网络赛 E.XKC's basketball team(线段树)

2019-09-08 18:40:19  阅读:394  来源: 互联网

标签:basketball int XKC tree mid person 2019icpc th him


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⋯n from left to right.
The ability of the i-th person is w_i, and if there is a guy whose ability is not less than w_i+m stands on his right, he will become angry. It means that the j-th person will make the i-th person angry if j>i and wj ≥ 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 .
Please calculate the anger of every team member .

Input
The first line contains two integers n and m(2≤n≤5∗10^5, 0≤m≤10^9) .
The following line contain nn integers w_1…w_n(0≤wi≤10^9) .

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
题意:给定n个数,与一个数m,求ai右边最后一个至少比ai大m的数与这个数之间有多少个数
传送门
solution:用线段树来存储每个区间的最大值,如此,便达到了二分的时间复杂度

#include <bits/stdc++.h>
using namespace std;

int a[1000005];
struct node{
	//意思是在[l, r]区间中最大的数是maxele
	int l, r, maxele;
}tree[4000010];

void build(int l, int r, int p)
{
	tree[p].l = l;
	tree[p].r = r;
	if (l == r){
		tree[p].maxele = a[l];
		return;
	}
	int mid = (l + r) >> 1;
	build(l, mid, p << 1);
	build(mid + 1, r,  p << 1 | 1);
	tree[p].maxele = max(tree[p << 1].maxele, tree[p << 1 | 1].maxele);
}

int query(int l, int r, int p){
	if (l == tree[p].l && r == tree[p].r)return tree[p].maxele;
	int mid = (tree[p].l + tree[p].r) >> 1;
	if (l > mid)return query(l, r, p << 1 | 1);
	else if (r <= mid)return query(l, r, p << 1);
	else return max(query(l, mid, p << 1), query(mid + 1, r, p << 1 | 1));
}

int main()
{
	int n, m;
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; ++i)scanf("%d", &a[i]);
	build(1, n, 1);
	for (int i = 1; i < n; ++i)
	{
		int s = m + a[i], l = i + 1, r = n;
		while (l < r){
			int mid = (l + r + 1) >> 1;
			if (query(mid, r, 1) >= s)l = mid;
			else if (query(l, mid, 1) >= s)r = mid - 1;
			else break;
		}
		if (a[l] >= s)printf("%d ", l - i - 1);
		else printf("-1 ");
	}
	printf("-1\n");
	return 0;
}

标签:basketball,int,XKC,tree,mid,person,2019icpc,th,him
来源: https://blog.csdn.net/weixin_44014982/article/details/100632661

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有