题意:给出一个数组,求其中和小于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. 关于安装VS2010过程中的错误

    下午本来安装好了VS:但是后来由于自己更新太多功能:直接使得VS太卡打不开:卸载重装:但是卸载的时候在“开始”里面的帮助文档和一些目录在卸载项里面没有:而在“开始"菜单就有:所以我索性把整个 ...

  2. [转] Matlab编程规范(MATLAB Programming Style Guidelines)

    转自: Jerry Zitao Liu的博客 主要是参考了下面这篇文章,简洁总结在这里. MATLAB Programming Style Guidelines 简洁总结如下: 表示object的数量 ...

  3. 查看Tensorflow版本

    python -c 'import tensorflow as tf; print(tf.__version__)' # for Python 2 python3 -c 'import tensorf ...

  4. Hive 数据类型转换

    在Hive的日常使用中经常会遇到需要对字段进行数据类型转换的情况.Hive中的数据类型转换包括隐式转换(implicit conversions)和显式转换(explicitly conversion ...

  5. 使用 SSH 和 SFTP 协议

    通过 SSH 和 SFTP 协议,我们能够访问其他设备,有效而且安全的传输文件等等. 几年前,我决定配置另外一台电脑,以便我能在工作时访问它来传输我所需要的文件.要做到这一点,最基本的一步是要求你的网 ...

  6. 使用wireshark分析tcp/ip报文之报文头

    以太网报文的结构如下: 其中,以太网的帧头: 14 Bytes:MAC目的地址48bit(6B),MAC源地址48bit(6B),Type域2B,一共14B. IP头部: TCP头部: http:// ...

  7. Total Difference String

    Total Difference Strings 给一个string列表,判断有多少个不同的string,返回个数相同的定义:字符串长度相等并从左到右,或从右往左是同样的字符 abc 和 cba 为视 ...

  8. 20145106java实验四

    实验名称:Java网络编程 实验内容: 1.掌握Socket程序的编写: 2.掌握密码技术的使用: 3.设计安全传输系统. 结对小伙伴 20145109竺文君 博客地址: 在本次实验中,是以我作为服务 ...

  9. 20145220韩旭飞《网络对抗》Exp7 网络欺诈技术防范

    20145220韩旭飞<网络对抗>Exp7 网络欺诈技术防范 应用SET工具建立冒名网站 要让冒名网站在别的主机上也能看到,需要开启本机的Apache服务,并且要将Apache服务的默认端 ...

  10. 20145225《网络对抗》Exp8 Web基础

    参考博客:5215~ 这次试验,没搞懂,只做了一部分,求老师酌情给分啊 啊啊 基础问题回答 什么是表单? 表单是一个包含表单元素的区域,表单元素是允许用户在表单中(比如:文本域.下拉列表.单选框.复选 ...