codeforces 1042d//Petya and Array// Codeforces Round #510 (Div. 2)

题意:给出一个数组,求其中和小于t的区间数。

先计算前缀和数组sum[i]。对当前的sum[i],查询树状数组中有几个比(sum[i]-t)大的数,那么用sum[i]减它就是一个合法区间。再将当前的sum[i]加入树状数组。

//#pragma comment(linker,"/STACK:1024000000,1024000000") 
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
#include <bitset>
#include <iomanip>
using namespace std;
const int SZ=2000010,INF=0x7FFFFFFF;
typedef long long lon;
lon n,t;
lon arr[SZ],sum[SZ],tot[SZ];
vector<lon> ls;

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

void add(lon x)
{
    for(;x<=n+1;x+=lowbit(x))
    {
        ++tot[x];
    }
}

lon getsum(lon x)
{
    lon res=0;
    for(;x;x-=lowbit(x))
    {
        res+=tot[x];
    }
    return res;
}

void init()
{
    cin>>n>>t;
    for(lon i=1;i<=n;++i)cin>>arr[i],sum[i]=sum[i-1]+arr[i];
    ls.push_back(0);
    for(lon i=1;i<=n;++i)
    {
        ls.push_back(sum[i]);
    }
    sort(ls.begin(),ls.end());
}



lon work()
{
    lon res=0;
    lon pos=lower_bound(ls.begin(),ls.end(),0)-ls.begin()+1;
    add(pos);
    //cout<<" "<<pos<<endl;
    for(lon i=1;i<=n;++i)
    {
        pos=upper_bound(ls.begin(),ls.end(),sum[i]-t)-ls.begin();
        lon tmp=i-getsum(pos);
        //cout<<tmp<<" "<<pos<<endl;
        res+=tmp;
        lon cur=lower_bound(ls.begin(),ls.end(),sum[i])-ls.begin()+1;
        add(cur);
        //if(sum[i]<t)++res;
    }
    return res;
}

int main()
{
    std::ios::sync_with_stdio(0);
    //freopen("d:\\1.txt","r",stdin); 
    //for(;scanf("%d",&n)!=EOF;)
    {
        init();
        cout<<work()<<endl;
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/gaudar/p/9722084.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值