题意:给出一个数组,求其中和小于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. Hive学习之路 (五)DbVisualizer配置连接hive

    一.安装DbVisualizer 下载地址http://www.dbvis.com/ 也可以从网上下载破解版程序,此处使用的版本是DbVisualizer 9.1.1 具体的安装步骤可以百度,或是修改 ...

  2. 异常和TCP通讯

    第七章 异常处理 * 异常处理机制中的try-catch * 语法: * try{ * 代码片段 * }catch(XXXException e){ * 当try中的代码片段出现了XXXExcepti ...

  3. Hive-复制表

    非分区表复制 复制一张非分区表,使用CREATE TABLE IF NOT EXISTS AS SELECT * FROM tb_name;只复制表结构,CREATE TABLE IF NOT EXI ...

  4. 20145127 《Java程序设计》第四次实验报告

    在本周,我们进行了Andirod部分的学习,这一次实验是使用Andirod Studio来运行简单的Andirod小程序,并在自己的手机虚拟机上显示自己的学号,为了达到这一效果,我在Andirod S ...

  5. 零碎知识点 telnet操作IMAP查看邮件

    1.用SQL语句查找出字符型字段内的最大值 select max(cast(字段 as int)) from 表 注:因为数据库是按字符比较的,10,9.因为9比1大,而只要有一位数字大,10后面就不 ...

  6. Python3基础 __getattr__ 访问不存在的属性时,新增提示功能

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  7. Python3基础 json.loads 解析json格式的数据,得到一个字典

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  8. Go第八篇之包的使用

    Go 语言的源码复用建立在包(package)基础之上.Go 语言的入口 main() 函数所在的包(package)叫 main,main 包想要引用别的代码,必须同样以包的方式进行引用,本章内容将 ...

  9. Linux写时拷贝技术【转】

    本文转载自:http://www.cnblogs.com/biyeymyhjob/archive/2012/07/20/2601655.html COW技术初窥: 在Linux程序中,fork()会产 ...

  10. ubuntu下如何修改时区和时间

    1.修改时区 sudo tzselect (按提示选择即可) sudo cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 2. 修改时间 sudo ...