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.
蔡徐坤,是一个篮球队的队长,正在指导对n个队员的训练。他让所有成员占成一行,并且从左往右从1开始标号。
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.
第i个人的能力值是wi,如果他右边有个人的能力值不小于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 .
我们定义第i个人的愤怒值是他与那些令他愤怒的人之间的某个人所间隔的最长距离,如果没人使得他不高兴,就输出-1
Please calculate the anger of every team member .
请计算出每个人的愤怒值。
本题可以用线段树维护每个区间的最大值,然后在查询某个值对应的答案时默认从右子树开始查找答案,如果存在的话(贪心),查询会返回相应的位置索引值。
比如样例3 4 5 6 2 10,如果要查询3的对应答案,从根节点开始(根节点保存了全区间最大值:10),发现右子树([4,6]区间)的最大值不小于3,于是直接向右查询,又发现[5,6]区间的最大值不小于3,继续向右查询,直至找到答案。如果右子树有合理答案,即使左子树存在这样的答案也无需去查询,因为题干中要求的是最长的距离,所以总是可以从右往左来找。
//#include
#include
#include
#include
#include
#include