题意:给出一个数组,求其中和小于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=,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+;x+=lowbit(x))
{
++tot[x];
}
} lon getsum(lon x)
{
lon res=;
for(;x;x-=lowbit(x))
{
res+=tot[x];
}
return res;
} void init()
{
cin>>n>>t;
for(lon i=;i<=n;++i)cin>>arr[i],sum[i]=sum[i-]+arr[i];
ls.push_back();
for(lon i=;i<=n;++i)
{
ls.push_back(sum[i]);
}
sort(ls.begin(),ls.end());
} lon work()
{
lon res=;
lon pos=lower_bound(ls.begin(),ls.end(),)-ls.begin()+;
add(pos);
//cout<<" "<<pos<<endl;
for(lon i=;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()+;
add(cur);
//if(sum[i]<t)++res;
}
return res;
} int main()
{
std::ios::sync_with_stdio();
//freopen("d:\\1.txt","r",stdin);
//for(;scanf("%d",&n)!=EOF;)
{
init();
cout<<work()<<endl;
}
return ;
}

codeforces 1042d//Petya and Array// Codeforces Round #510 (Div. 2)的更多相关文章

  1. codeforces 1042D - Petya and Array【树状数组+离散化】

    题目:戳这里 题意:有n个数,问有多少个区间满足[L,R]内的和小于t. 解题思路: [L,R]内的和小于t等价于sum[R]-sum[L-1]<t,将sum[L-1]左移,可以看出R与L的关系 ...

  2. Codeforces Round #510 (Div. 2)

    Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...

  3. Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)

    D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...

  4. Petya and Array CodeForces - 1042D (树状数组)

    D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  5. CF Round #510 (Div. 2)

    前言:没想到那么快就打了第二场,题目难度比CF Round #509 (Div. 2)这场要难些,不过我依旧菜,这场更是被\(D\)题卡了,最后\(C\)题都来不及敲了..最后才\(A\)了\(3\) ...

  6. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  7. codeforces 1042c// Array Product// Codeforces Round #510(Div. 2)

    题意:给出一个数组,2种操作:.1:x*y然后x消失,2:除掉x(2操作最多只能进行一次).问最大的结果的一种操作方式.逻辑题,看能不能想全面. 1先数好0,正,负的数量,zero,pos,neg.如 ...

  8. Codeforces Round #510 (Div. 2) C. Array Product

    题目 题意: 给你n个数,有两种操作,操作1是把第i个位置的数删去, 操作2 是把 a[ j ]= a[ i ]* a[ j ],把a[ i ]删去 .n-1个操作以后,只剩1个数,要使这个数最大 . ...

  9. Petya and Array CodeForces - 1042D

    很不错的一道题 给你一个长度为n的数组,问共有多少个区间满足区间之和小于给定的数t 这种题一般做法肯定是枚举,固定左端点枚举右端点,枚举的过程需要优化,否则就是n方 这道题我先求一个前缀和,然后逆着枚 ...

随机推荐

  1. python 多进程并发接口测试实例

    #encoding=utf-8 import requests import json import os import hashlib print "register------" ...

  2. IO(字节流)

    1. 字节流类以InputStream 和 OutputStream为顶层类,他们都是抽象类(abstract) 2. 最重要的两种方法是read()和write(),它们分别对数据的字节进行读写.两 ...

  3. Linux基础命令---mkisofs

    mkisofs mkisofs指令可以创建ISO9660/Joliet/HFS文件系统,现在使用指令genisoimage代替它.genisoImage是一个预掌握程序,用于生成iso 9660/jo ...

  4. go环境搭建—基于CentOS6.8

    1. 背景 在当前的中国网络环境下,我们无法访问Google的服务的,包括Golang.org.从第三方网站下载预编译的二进制Go发行版可能存在第三方源代码注入的风险,例如之前的XcodeGhost. ...

  5. java反射之-性能优化

    在最近的计划中,打算看看在不使用google protobuf的情况下,在原有的采用jackson作为json序列化工具的基础上,是否可以实现进一步的性能优化.主要是针对list的情况. 测试的时候选 ...

  6. Cortex-M3基础

    (一)寄存器 1 寄存器组      R0-R12: 通用寄存器 ------------------------------------------------------------------- ...

  7. max3232

    max3232采用专有低压差发送器输出级,利用双电荷泵在3.0V至5.5V电源供电时能够实现真正的RS-232性能,器件仅需四个0.1uF的外部小尺寸电荷泵电容.max3232确保在120kbps数据 ...

  8. VC++ 删除一个文件目录下的所有文件以及目录

    BOOL DoRemoveDirectory(CString chrDirName); BOOL ReleaseDirectory(CString chrDirName) { BOOL bRemove ...

  9. React 回忆录(一)为什么使用 React?

    Hi 各位,欢迎来到 React 回忆录!

  10. 蚂蚁感冒|2014年蓝桥杯B组题解析第八题-fishers

    蚂蚁感冒 长100厘米的细长直杆子上有n只蚂蚁.它们的头有的朝左,有的朝右. 每只蚂蚁都只能沿着杆子向前爬,速度是1厘米/秒. 当两只蚂蚁碰面时,它们会同时掉头往相反的方向爬行. 这些蚂蚁中,有1只蚂 ...