【Codeforces 1042D】Petya and Array

【链接】 我是链接,点我呀:)
【题意】


题意

【题解】


把a[i]处理成前缀和
离散化.
枚举i从1..n假设a[i]是区间和的a[r]
显然我们需要找到a[r]-a[l]<t的l的个数
即a[r]<t+a[l]
那么我们处理完每个i之后,每次把a[i]+t加入到树状数组中去。
每次以进入i的时候,找到比a[r]大的a[l]+t的个数就好(用树状数组求和即可)

【代码】

#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int N = 2e5;

int n;
ll t;
ll a[N+10];
ll b[N*2+10];
map<ll,int> dic,dic1;

int lowbit(int x){
    return x&(-x);
}

ll get_sum(int x){
    ll ans = 0;
    while (x>0){
        ans = ans + b[x];
        x = x-lowbit(x);
    }
    return ans;
}

void add(int x){
    while (x<=2*N+2){
        b[x]= b[x]+1;
        x = x + lowbit(x);
    }
}

int main(){
    ios::sync_with_stdio(0),cin.tie(0);
    cin >> n >> t;
    dic[0] = 1;
    dic[0+t] = 1;
    for (int i = 1;i <= n;i++) {
        cin >> a[i];
        a[i]+=a[i-1];
        dic[a[i]] = 1;
        dic[a[i]+t] = 1;
    }
    int cnt = 0;
    for (auto temp:dic){
        cnt++;
        dic1[temp.first] = cnt;
    }
    add(dic1[0+t]);
    //cout<<dic1[0+t]<<endl;
    ll fans = 0;
    for (int i = 1;i <= n;i++){
        ll temp1 = get_sum(cnt)-get_sum(dic1[a[i]]);
        fans = fans + temp1;
        add(dic1[a[i]+t]);
    }
    cout<<fans<<endl;
    return 0;
}

转载于:https://www.cnblogs.com/AWCXV/p/10684252.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值