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;
}



  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值