codeforce—Frets On Fire 11.30training

Problem - F - Codeforces
Miyako came to the flea kingdom with a ukulele. She became good friends with local flea residents and played beautiful music for them every day.

In return, the fleas made a bigger ukulele for her: it has \(n\) strings, and each string has \((10^{18} + 1)\) frets numerated from \(0\) to \(10^{18}\). The fleas use the array \(s_1, s_2, \ldots, s_n\) to describe the ukulele's tuning, that is, the pitch of the \(j\)-th fret on the \(i\)-th string is the integer \(s_i + j\).

Miyako is about to leave the kingdom, but the fleas hope that Miyako will answer some last questions for them.

Each question is in the form of: "How many different pitches are there, if we consider frets between \(l\) and \(r\) (inclusive) on all strings?"

Miyako is about to visit the cricket kingdom and has no time to answer all the questions. Please help her with this task!

Formally, you are given a matrix with \(n\) rows and \((10^{18}+1)\) columns, where the cell in the \(i\)-th row and \(j\)-th column (\(0 \le j \le 10^{18}\)) contains the integer \(s_i + j\). You are to answer \(q\) queries, in the \(k\)-th query you have to answer the number of distinct integers in the matrix from the \(l_k\)-th to the \(r_k\)-th columns, inclusive.

宫子带着一把四弦琴来到跳蚤王国。她和当地的跳蚤居民成了好朋友,每天为他们演奏美妙的音乐。

作为回报,跳蚤们为她制作了一把更大的尤克里里:这把尤克里里有 \(n\) 根琴弦,每根琴弦上有 \((10^{18} + 1)\) 个音格,音格从 \(0\)\(10^{18}\) 依次排列。跳蚤们用数组 \(s _ 1, s _ 2, \ldots, s _ n\) 来描述尤克里里的调音,也就是说, \(i\) / \(0\) 根琴弦上 \(j\) / \(0\) 个音格的音高是整数 \(s _ i + j\)

宫子即将离开王国,但跳蚤们希望宫子能回答他们最后的几个问题。

每个问题的形式是"如果我们把所有琴弦上 \(l\)\(r\) 之间的音高都算在内,那么一共有多少种不同的音高?(如果把所有琴弦上的 \(l\)\(r\) 之间的音位都算在内,那么一共有多少种不同的音高?

Miyako 即将访问蟋蟀王国,没有时间回答所有问题。请帮助她完成这项任务!

从形式上看,给你一个有 \(n\) 行和 \((10^{18}+1)\) 列的矩阵,其中 \(i\) 行和 \(j\) 列( \(0 \le j \le 10^{18}\) )中的单元格包含整数 \(s _ i + j\) 。你要回答 \(q\) 个查询,在 \(k\) 个查询中,你必须回答矩阵中从 \(l _ k\) \th列到 \(r _ k\) \th列(含)的不同整数的个数。

Input

The first line contains an integer \(n\) (\(1 \leq n \leq 100\,000\)) — the number of strings.

The second line contains \(n\) integers \(s_1, s_2, \ldots, s_n\) (\(0 \leq s_i \leq 10^{18}\)) — the tuning of the ukulele.

The third line contains an integer \(q\) (\(1 \leq q \leq 100\,000\)) — the number of questions.

The \(k\)-th among the following \(q\) lines contains two integers \(l_k\)\(r_k\) (\(0 \leq l_k \leq r_k \leq 10^{18}\)) — a question from the fleas.

输入

第一行包含一个整数 \(n\) ( \(1 \leq n \leq 100\,000\) ) - 字符串的个数。

第二行包含 \(n\) 个整数 \(s _ 1, s _ 2, \ldots, s _ n\) ( \(0 \leq s _ i \leq 10^{18}\) ) - 尤克里里的调音。

第三行包含一个整数 \(q\) ( \(1 \leq q \leq 100\,000\) )( \(1 \leq q \leq 100\,000\) ) - 问题数。

下面 \(q\) 行中的 \(k\) (th)行包含两个整数 \(l _ k\)\(r _ k\) 。( \(0 \leq l _ k \leq r _ k \leq 10^{18}\) ) - 来自跳蚤的问题。

Output

Output one number for each question, separated by spaces — the number of different pitches.

输出

每个问题输出一个数字,用空格分隔,即不同音调的数量。

Examples

21 50000000000000000021000000000000000000 10000000000000000000 1000000000000000000

Note

For the first example, the pitches on the \(6\) strings are as follows.

\[\begin{matrix} \textbf{Fret} & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} & \textbf{7} & \ldots \\ s_1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & \dots \\ s_2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & \dots \\ s_3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & \dots \\ s_4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & \dots \\ s_5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & \dots \\ s_6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & \dots \end{matrix} \]

There are \(5\) different pitches on fret \(7\) — \(8, 10, 11, 12, 16\).

There are \(10\) different pitches on frets \(0, 1, 2\) — \(1, 2, 3, 4, 5, 6, 7, 9, 10, 11\).

在第一个例子中, \(6\) 弦上的音高如下。

\[\begin{matrix} \textbf{Fret} & \textbf{0} & \textbf{1} & \textbf{2} & \textbf{3} & \textbf{4} & \textbf{5} & \textbf{6} & \textbf{7} & \ldots \\ s _ 1: & 3 & 4 & 5 & 6 & 7 & 8 & 9 & 10 & \dots \\ s _ 2: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & \dots \\ s _ 3: & 4 & 5 & 6 & 7 & 8 & 9 & 10 & 11 & \dots \\ s _ 4: & 1 & 2 & 3 & 4 & 5 & 6 & 7 & 8 & \dots \\ s _ 5: & 5 & 6 & 7 & 8 & 9 & 10 & 11 & 12 & \dots \\ s _ 6: & 9 & 10 & 11 & 12 & 13 & 14 & 15 & 16 & \dots \end{matrix} \]

音格 \(7\) - \(8, 10, 11, 12, 16\) 上有 \(5\) 个不同的音高。

\(0, 1, 2\) - \(1, 2, 3, 4, 5, 6, 7, 9, 10, 11\) 音格上有 \(10\) 个不同的音高。


题解

有一个 N*(10^18) 的矩阵,题目给出 N 和矩阵 N 行的首元素,后面的元素都是前一个元素+1;Q次查询,询问 [ L, R ] 列 有多少个不同的元素(所有行)。
这里值得注意的是: [ L, R ]内不同元素的数量与 [ 0, R-L+1 ] 内是相等的,因为是递增的,所以对于[ 0, R-L+1 ]内的所有元素加L后即[ L, R ]内的所有元素,所以他们所求数量上是相等的。经过转换后,我们发现如果直接遍历的话,1018次显然是超时的,但是对于列的元素数是小的,所以我们可以对单独一列进行处理。我们发现数据是按列递增的,我们知道有几列所以是可以通过首列去计算的。我们从首列开始观察,如果我们对这一列的元素从小到大进行排序,并去重,对于这一列每一行之间的数是可以被后面的列给填充的,所以我们只需要求有多少数之间是可以被完全填充,有多少是不能被完全填充,当首列相邻两行之间的差值小于等于R-L+1就是能被完全填充的,否则不能,所以我们用差分和前缀和就可以完成计算。

代码如下

#include<bits/stdc++.h>
using namespace std;
#define ll long long
ll s[100005];
ll cc[100005];
ll sum[100005];
int main() {
//	std::ios::sync_with_stdio(false);
	ll n;
	cin>>n;
	for(int i = 1; i <= n; i++) {
		cin>>s[i];
	}
	sort(s+1, s+n+1);
	for(int i = 1; i < n; i++) {
		cc[i] = s[i+1] - s[i];
	}
	cc[n] = 2e18;
	sort(cc+1, cc+n+1);
	sum[0] = 0;
	for(int i = 1; i <= n; i++) {
		sum[i] = sum[i-1] + cc[i];
	}
	int q;
	cin>>q;
	ll l, r, len;
	ll ans = 0;
	while(q--) {
		cin>>l>>r;
		len = r - l + 1;
		ll ans = lower_bound(cc+1, cc+n+1, len) - cc - 1;//注意这里用到了二分来快速查找第一个大于len的数
		printf("%lld ", sum[ans] + (ll)(n-ans)*len);
	}
	return 0;
}
posted @ 2023-12-05 02:48  LongDz  阅读(16)  评论(0)    收藏  举报