题意:给出一个数组,求其中和小于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 为什么sys.stdout.write 输出时后面总跟一个数字

    sys.stdout 是标准输出文件.write就是往这个文件写数据. 合起来就是打印数据到标准输出 因为-在交互模式下会输出函数返回值,而write会返回输出的字符数量.在命令行里不会显示

  2. 基于Axis1.4的webservice接口开发(接口调用)

    基于Axis1.4的webservice接口开发(接口调用) 一.webservice接口代码参考上一篇博客: http://www.cnblogs.com/zhukunqiang/p/7125668 ...

  3. Linux基础命令---mkisofs

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

  4. MySQL数据库----流程控制

    流程控制 1.条件语句 举例一 delimiter // CREATE PROCEDURE proc_if () BEGIN declare i int default 0; if i = 1 THE ...

  5. 解决“ 故障模块名称: clr.dll ”

    错误内容: 微软的错误说明:http://support.microsoft.com/kb/2640103/zh-cn 类似下面的错误: 错误应用程序名称:xxx.exe,版本: 1.0.0.0,时间 ...

  6. CentOS安装mysql并配置远程访问

    最近上班挺无聊,每天就是不停的重启重启重启,然后抓log.于是有事儿没事儿的看卡闲书,搞搞其他事情. 但是,公司笔记本装太多乱其八糟的东西也还是不太好. 于是,想到了我那个当VPN server的VP ...

  7. ELK之elasticsearch6.5

    官方网站:https://www.elastic.co/guide/en/elasticsearch/reference/current/rpm.html 这里采用rpm的方式安装: # wget h ...

  8. 20145225唐振遠《网络对抗》Exp5 MSF基础应用

    基础问题回答 用自己的话解释什么是exploit,payload,encode? exploit就相当于是载具,将真正要负责攻击的代码传送到靶机中,我觉得老师上课举的火箭和卫星的例子非常形象,火箭只是 ...

  9. 20135234mqy-——信息安全系统设计基础第十周学习总结

    ls1:显示当前目录下的文件. ls2:查看文件的详细信息(用户名,群组名,大小,创建时间,读写权限等). who:读取需要的信息到存储器,并用标准输出函数打印到屏幕上 filesize:计算文件的字 ...

  10. C语言结构体,点运算和箭头运算

    C语言有一种数据类型叫结构体,其定义格式为: struct 结构体名 { 结构体成员变量定义; }; 如: struct student { char name[20]; int age ; doub ...