2022河南萌新联赛第(二)场:D题

题目描述

给定一个长度为 n 的整数数列 和两个整数  x,y 。

请你判断一共有多少个数对 (l,r) 同时满足以下条件:

  • 1≤l≤r≤n
  • al+al+1+…+ar−1+ar≤x+y×(r−l+1)

输入描述:

第一行输入三个整数 n,x,y

第二行输入 n 个整数 a1,a2,⋯ ,an。

数据范围:

对于60%的数据,1≤n≤2000,−109≤ai≤109,−109≤x,y≤109
对于100%的数据,1≤n≤200000,−109≤ai≤109,−1012≤x,y≤1012

输出描述:

输出一个整数,表示满足条件的数对的数量。

示例1

输入

5 3 3
4 6 5 3 7

输出

5

说明

满足要求的数对一共5组,分别是(1,1)、(2,2)、(3,3)、(4,4)、(3,4)。

思路:al+al+1+…+ar−1+ar≤x+y×(r−l+1),即al+al+1+…+ar−1+ar-y×(r−l+1)≤x,相当于是a[i]-y的前缀和的第r项减去第l-1项,那么这就好办了,把所有a[i]都减y,相加构造前缀数组,那么这题就相当于是小于等于a[i-1]+x且下标大于等于i的数的总和

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5 + 10;
ll a[N];
int main()
{
    ll n, x, y;
    scanf("%lld%lld%lld", &n, &x, &y);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld", &a[i]);
        a[i] -= y;
    }
    for (int i = 1; i <= n; i++)
    {
        a[i] += a[i - 1];
        // cout << a[i] << ' ';
    }
    // cout << endl;
    vector<ll> v;
    ll ans = 0;
    for (int i = n; i >= 1; i--)
    {
        v.insert(lower_bound(v.begin(), v.end(), a[i]), a[i]);
        ans += upper_bound(v.begin(), v.end(), a[i - 1] + x) - v.begin();
        // cout << upper_bound(v.begin(), v.end(), a[i] + x) - v.begin() << ' ';
        // for (auto t : v)
        // {
        //     cout << t << ' ';
        // }
        // cout << endl;
    }
    printf("%lld\n", ans);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值