# hihocoder#1384 Genius ACM（倍增+二分）

Advanced CPU Manufacturer (ACM) is one of the best CPU manufacturer in the world. Every day, they manufacture n CPU chips and sell them all over the world.

As you may know, each batch of CPU chips must pass a quality test by the QC department before they can be sold. The testing procedure is as follows:

1) Randomly pick m pairs of CPU chips from the batch of chips (If there are less than 2m CPU chips in the batch of chips, pick as many pairs as possible.)

2) For each pair, measure the Relative Performance Difference (RPD) between the two CPU chips. Let Di be the RPD of the i-th pair

3) Calculate the Sqared Performance Difference (SPD) of the batch according to the following formula:

SPD=∑Di2

If there are only 1 CPU in a batch, then the SPD of that batch is 0.

4) The batch of chips pass the test if and only if SPD≤k, where k is a preseted constant

Usually they send all the n CPU chips as a single batch to the QC department every day. As one of the best CPU manufacturer in the world, ACM never fail the test. However, with the continuous improvement of CPU performance, they find that they are at risk!

Of course they don’t want to take any risks. So they make a decision to divide the n chips into several batches to ensure all of them pass the test. What’s more, each batch should be a continuous subsequence of their productions, otherwise the QC department will notice that they are cheating. Quality tests need time and money, so they want to minimize the number of batches.

Given the absolute performance of the n chips P1 … Pn mesured by ACM in order of manufacture, your task is to determine the minimum number of batches to ensure that all chips pass the test. The RPD of two CPU chips equals to the difference of their absolute performance.

The first line contains a single integer T, indicating the number of test cases.

In each test case, the first line contains three integers n, m, k. The second line contains n integers, P1 … Pn.

T≤12
1≤n,m≤5×105
0≤k≤1018
0≤Pi≤220

For each test case, print the answer in a single line.

2
5 1 49
8 2 1 7 9
5 1 64
8 2 1 7 9

2
1

# BZOJ 4864: [BeiJing 2017 Wc]神秘物质（splay）

Description

21ZZ 年，冬。

merge x e 当前第 x 个原子和第 x+1 个原子合并，得到能量为 e 的新原子；
insert x e 在当前第 x 个原子和第 x+1 个原子之间插入一个能量为 e 的新原子。

max x y 当前第 x 到第 y 个原子之间的任意子区间中区间极差的最大值；
min x y 当前第 x 到第 y 个原子之间的任意子区间中区间极差的最小值。

Input

N<=100,000,M<=100,000 1 ≤ e, Ei ≤ 109。 设 N’ 为当前时刻原子数目。 对于 merge 类事件， 1 ≤ x ≤ N’-1； 对于 insert 类事件， 1 ≤ x ≤ N’； 对于 max 和 min 类事件， 1 ≤ x < y ≤ N’。 任何时刻，保证 N’ ≥ 2。 Output 输出若干行， 按顺序依次表示每次 max 和 min 类事件的测量结果。 Sample Input 4 3 5 8 10 2 max 1 3 min 1 3 max 2 4 Sample Output 5 2 8 一个细节很多的题目。。。调了半天。。 因为随着一个区间的长度增大，其最大值减最小值的差值也会越来越大。所以对于一个区间来说，其极差最大的子区间就是整个区间，其极差最小的区间必然是相邻的两个数。 splay维护区间最大值和最小值，以及相邻两个数的差值即可。在插入和删除的时候修改相邻的两个数的差值（注意这里一定要把两个数都旋转到当前节点的左右两侧）。

Description

Input

Output

S的最小值
Sample Input

2

4059 -1782

Sample Output

99