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=,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)的更多相关文章
- codeforces 1042D - Petya and Array【树状数组+离散化】
题目:戳这里 题意:有n个数,问有多少个区间满足[L,R]内的和小于t. 解题思路: [L,R]内的和小于t等价于sum[R]-sum[L-1]<t,将sum[L-1]左移,可以看出R与L的关系 ...
- Codeforces Round #510 (Div. 2)
Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...
- Petya and Array CodeForces - 1042D (树状数组)
D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- CF Round #510 (Div. 2)
前言:没想到那么快就打了第二场,题目难度比CF Round #509 (Div. 2)这场要难些,不过我依旧菜,这场更是被\(D\)题卡了,最后\(C\)题都来不及敲了..最后才\(A\)了\(3\) ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
- codeforces 1042c// Array Product// Codeforces Round #510(Div. 2)
题意:给出一个数组,2种操作:.1:x*y然后x消失,2:除掉x(2操作最多只能进行一次).问最大的结果的一种操作方式.逻辑题,看能不能想全面. 1先数好0,正,负的数量,zero,pos,neg.如 ...
- Codeforces Round #510 (Div. 2) C. Array Product
题目 题意: 给你n个数,有两种操作,操作1是把第i个位置的数删去, 操作2 是把 a[ j ]= a[ i ]* a[ j ],把a[ i ]删去 .n-1个操作以后,只剩1个数,要使这个数最大 . ...
- Petya and Array CodeForces - 1042D
很不错的一道题 给你一个长度为n的数组,问共有多少个区间满足区间之和小于给定的数t 这种题一般做法肯定是枚举,固定左端点枚举右端点,枚举的过程需要优化,否则就是n方 这道题我先求一个前缀和,然后逆着枚 ...
随机推荐
- kafka监控工具之一--kafka-manager
部署环境 jdk7 kafka_2.10-0.9.0.1 xshell4 rhel-server-6.5 kafka-manager 是功能比较多的kafka管控工具. 安装方法一 安装方法二 步骤一 ...
- Python: 从字典中提取子集--字典推导
问题: 构造一个字典,它是另外一个字典的子集 answer: 最简单的方式是使用字典推导 eg1: 1. >>>prices = {'ACME': 45.23, 'AAPL': 61 ...
- python repr方法和str方法
每个类都有默认的__repr__, __str__方法,用print 实例时调用类的str方法,直接输出类的实例,调用的是类的repr方法 在命令行界面,不用print命令打印而是直接写变量名,就是用 ...
- 处理内容有&特殊字符thinkphp返回xml无法解析的问题<![CDATA[xxx]]>
处理内容有&特殊字符thinkphp返回xml无法解析的问题<![CDATA[xxx]]> // xml 转义特殊字符 如&'" <![CDATA[&quo ...
- [转载]ViewState使用小结
ViewState是.Net中提出的状态保存的一种新途径,web程序保存状态的方式有这样几种:1.Application:保存在Application中的数据是全局有效的:Application里面存 ...
- Linux中Postfix虚拟用户及虚拟域(六)
Postfix基于虚拟用户虚拟域的邮件架构 上图是一个几乎完整的邮件系统架构图,这里基于Mysql数据库进行用户认证,不管是Postfix.Dovecot.webmail都需要去Mysql数据库中进行 ...
- MySQL数据库----视图
视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的临时 ...
- scrapy运行方式
1,在cmd 命令行下执行 scrapy crawl demo (爬虫主逻辑的 name= 'demo '的名字) 2, 也可以在spider目录下添加一个py文件,加入以下代码 from scra ...
- P4878 [USACO05DEC]layout布局
P4878 [USACO05DEC]layout布局 差分约束 最短路径最长路,最长路径最短路 本题求的是最长路径,所以跑最短路 根据题意连边,然后spfa即可 注意要判断图的连通性,所以新建一个虚拟 ...
- linux查看是否能访问外网及拥有的公网IP
linux查看是否能访问外网及拥有的公网IP linux查看是否能访问外网及拥有的公网IP: 1,测访问外网能力:curl -l http://www.baidu.com 2,测访问外网能力:wget ...