Codeforces Global Round 2-D. Frets On Fire-2점
【Description】
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 (1018+1) frets numerated from 0 to 1018. The fleas use the array s1,
s2,…,sn to describe the ukulele's tuning, that is, the pitch of the j-th fret
on the i-th string is the integer si+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 (1018+1) columns, where the
cell in the i-th row and j-th column (0≤j≤1018) contains the integer si+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 lk-th to the rk-th columns, inclusive.
【Input】
The first line contains an integer n (1≤n≤100000) — the number of strings.
The second line contains n integers s1,s2,…,sn (0≤si≤1018) — the tuning of the
ukulele.
The third line contains an integer q (1≤q≤100000) — the number of questions.
The k-th among the following q lines contains two integers lk,rk (0≤lk≤rk≤1018)
— a question from the fleas.
【Output】
Output one number for each question, separated by spaces — the number of
different pitches.
【Examples】
Sample Input
6
3 1 4 1 5 9
3
7 7
0 2
8 17
Sample Output
5 10 18
Sample Input
2
1 500000000000000000
2
1000000000000000000 1000000000000000000
0 1000000000000000000
Sample Output
2 1500000000000000000
【Problem Description】
n , i i a[i], i j , a[i]+j
q , n ,[l,r] 。
:
7~7: 8,10,11,12,16.
Fret 0 1 2 3 4 5 6 7 … s 1 : 3 4 5 6 7 8 9 10 … s 2 : 1 2 3 4 5 6 7 8 … s 3 : 4 5 6 7 8 9 10 11 … s 4 : 1 2 3 4 5 6 7 8 … s 5 : 5 6 7 8 9 10 11 12 … s 6 : 9 10 11 12 13 14 15 16 …\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} Frets1:s2:s3:s4:s5:s6:031415914252610253637113647481247585913586961014697107111571081181216…………………
【Solution】
n , :1 1 3 4 5 9
, [l,r] [0,r-l+1]
r :r=r-l+1
r :
:
1 , , , ,
1 , 2, 1 2 。
3 , 1, 3 。
……
……
i a[i+1]-a[i], n r-a[n]
[0,r] , ?
i min(r,a[i+1]-a[i]), : min(r,a[i+1]-a[i])
q , n , O(n*q)
: [1,n-1] 0 2 1 1 4, :0 1 1 2 4
[0,r], r t 。
[1,t] r ,[t+1,n-1] r
sum[1,t]+((n-1)-t)*r+ r, =sum[1,t]+(n-t)*r
t ,sum 。
【Code】
/*
* @Author: Simon
* @Date: 2019-04-06 22:35:16
* @Last Modified by: Simon
* @Last Modified time: 2019-04-06 22:40:29
*/
#include
using namespace std;
typedef int Int;
#define int long long
#define INF 0x3f3f3f3f
#define maxn 100005
int a[maxn],b[maxn],s[maxn];
Int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);
for(int i=2;i<=n;i++){
b[i-1]=a[i]-a[i-1]; //
}
sort(b+1,b+n); //
for(int i=1;i<n;i++) s[i]=s[i-1]+b[i]; //
int q;cin>>q;
while(q--){
int x,y;cin>>x>>y;y=y-x+1;
int t=upper_bound(b+1,b+n,y)-b-1; // y t
cout<<s[t]+(n-t)*y<<' ';
}
cout<<endl;
cin.get(),cin.get();
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
|NOIOJ|2분|04: 네트워크 관리자심판위원회는 인터넷 라인을 구매하기 위해 현지의 한 인터넷 솔루션 제공 업체에 연락하여 일정한 수량의 등장망 라인을 제공할 수 있도록 요구했다.심판위원회는 네트워크가 길어질수록 좋아져 선수들 사이의 거리가 가능한 한...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.