#701 Div 2 B

B. Replace and Keep Sorted
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

Examples
inputCopy
4 2 5
1 2 4 5
2 3
3 4
outputCopy
4
3
inputCopy
6 5 10
2 4 6 7 8 9
1 4
1 2
3 5
1 6
5 5
outputCopy
8
9
7
6
9
Note
In the first example:

In the first query there are 4 arrays that are 5-similar to [2,4]: [1,4],[3,4],[2,3],[2,5].

In the second query there are 3 arrays that are 5-similar to [4,5]: [1,5],[2,5],[3,5].

思路:

找有个相机几个相同长度的递增序列
只能 有 一个 位置不同 其余位置 和 原数列 相同

所以 对于新数列 只是 在 原数列 更改 一个数 保证单调递增

对于每一个位置他都可以 向左 向右 不超过 相邻 的都 ok

即 对于每一个 位置 有 这样选择 的选择 (a[i+1]-a[i]-1)+(a[i]-a[i-1]-1) (除了端点 另外讨论)

给定两个端点 left right

a[left+1]-a[left]-1 + a[left+2]+a[left+1]-1 (这是第二个点的选择)(除了端点是第一个)

=a[left+2]-a[left]-2

a[letf+2]-a[left+1]-1 +a[left+3]-a[left+2]-1 (第三个点)

=a[left+3]-a[left+1]-2

a[left+3]-a[left+2]-1 +a[left+4]-a[left+3]-1

=a[left+4]-a[left-2]-2

这时候发现规律

可以约掉 就好比高中的数列的一个做法 我也忘了(有点类似于 裂项相消 我也没裂

… …

… .
a[right-1]-a[right-2]-1 +a[right-2]-a[right-3]-1

=a[right-1]-a[right-3]-2

a[right-1]-a[right-2]-1 +a[right]-a[right-1]-1

=a[right]-a[right-2] -2

相加得到

a[right]+a[rifght-1]-a[left]-a[left+1]+2*(right-left-1)

然后加上 两端的选择

a[left]-1 , a[left+1]-a[left]-1 (这是左端点)
a[right]-a[right-1]-1 , k-a[right] (这是右端点)

然后和中间的点相加得到

k+a[right]-a[left]-2*(right-left-1)-3;

然后奉上小代码

#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
ll read(){ll res = 0, ch, flag = 0;if((ch = getchar()) == '-')flag = 1;else if(ch >= '0' && ch <= '9')res = ch - '0';while((ch = getchar()) >= '0' && ch <= '9' )res = res * 10 + ch - '0';return flag ? -res : res;}
const int maxn =1e6+199 ;
ll sum=0,maxa=-1;
ll n,m,k,w,ans=0,cnt=0;
int a[maxn];
ll mod=1e9+7;
int main()
{
    n=read();
    m=read();
    k=read();
    for(int i=1;i<=n;i++)
        a[i]=read();
    while(m--)
    {
        int left=read();
        int right=read();

        cout<<a[right]-a[left]+k-2*(right-left)-1<<endl;
    }

      return 0;
}

```

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牛郎恋刘娘,刘娘念牛郎

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值