1. 程式人生 > 其它 >B. Replace and Keep Sorted字首和

B. Replace and Keep Sorted字首和

技術標籤:字首和思維演算法

B. Replace and Keep Sorted字首和

Given a positive integer k, two arrays are called k-similar if:
they are strictly increasing;
they have the same length;
all their elements are positive integers between 1 and k (inclusive);
they differ in exactly one position.
You are given an integer k, a strictly increasing array a and q queries. For each query, you are given two integers li≤ri. Your task is to find how many arrays b exist, such that b is k-similar to array [ali,ali+1…,ari].

Input
The first line contains three integers n, q and k (1≤n,q≤105, n≤k≤109) — the length of array a, the number of queries and number k.

The second line contains n integers a1,a2,…,an (1≤ai≤k). This array is strictly increasing — a1<a2<…<an.

Each of the following q lines contains two integers li, ri (1≤li≤ri≤n).

Output
Print q lines. The i-th of them should contain the answer to the i-th query.
**解題思路:這題用樸素做法肯定會超時,所以我們可以用字首和來降低時間複雜度,優化到O(n),在區間訪問時,我們可以先求出 l+1~r-1 區間中可以直接用差分公式求出可以替換的格式,而邊界需要特殊判斷,對於左邊界l,它可以替換的數是由第l+1個數決定的,所以可以提換的個數為a[l+1]-2,減二表示去掉a[l+1]和a[l],而右邊界是由a[r-1]的大小所決定的,所以可以替換的個數為k-a[r-1]-1,減a[r-1]-1是由於陣列是嚴格遞增的當選擇第r個數與原陣列不同時,那麼前面全部相同,所以要選擇比a[r-1]大的數,減一表示去除a[r]這個數,而位於l+1~r-1中的第i個數是由第i+1和第i-1個數共同限制的,其公式為a[i+1]-a[i-1]-2,減二表示去除a[i]和a[i+1],那麼字首和公式為sum[i]=sum[i-1]+a[i+1]-a[i-1]-2,當l=r時需要特判一下,除去a[i],那麼還有k-1個數,當l,r相差為1時,那麼不需要用到字首和公式。

#include<bits/stdc++.h>
using namespace std;
#define sl(n) scanf("%lld",&n)
#define sl3(n,q,k) scanf("%lld %lld %lld",&n,&q,&k)
#define sl2(l,r)  scanf("%lld %lld",&l,&r)
#define pl(n) printf("%lld\n",n)
#define pE printf("\n");
#define pr printf
#define pb push_back
#define ll long long 
int main(){
	ios_base::sync_with_stdio(false);
	ll t,i,j,n,q,k,l,r;
	sl3(n,q,k);
	ll a[n+1],sum[n+1];
	sum[0]=0;
	a[0]=0; 
	for(i=1;i<=n;i++)sl(a[i]);
	for(i=1;i<=n;i++){
		if(i==1)sum[i]=a[i]-1;
		if(i==n)sum[i]=k-a[i-1]-1+sum[i-1];
		if(i>1&&i<n)sum[i]=a[i+1]-a[i-1]-2+sum[i-1];
	}
	for(i=1;i<=q;i++){
		sl2(l,r);
		if(l==r){cout<<k-1<<endl;continue;}
		if(r-l>1)cout<<sum[r-1]-sum[l]+k-a[r-1]-1+a[l+1]-2<<endl;
		else cout<<k-a[r-1]-1+a[r]-2<<endl;
	}
	return 0;
}