[CodeForces]1042D
大意:求一个序列有几个子序列的和小于给定值,里面的数有正有负,序列长度≤200000。
列个式子,其实求的是sum[r]-sum[l-1]<T
sum[r]-T<sum[l-1]
所以我们可以枚举r,然后用树状数组找小于sum[r]-T的数的个数。记得要在树状数组里先加一个0,表示L取1,即从头开始。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
const int N=;
long long n,pre[N],a[N],ans,t[N],T,sum,tp[N];
void add(long long i) {for(;i<N;i+=i&-i) t[i]++;}
long long ask(long long i) {long long res=;for(;i;i-=i&-i) res+=t[i];return res;}
int main() {
scanf("%lld%lld",&n,&T);
for(int i=;i<=n;i++) scanf("%lld",&a[i]),pre[i]=pre[i-]+a[i],tp[i]=pre[i];
sort(tp+,tp++n);
int u=unique(tp+,tp++n)-tp-;
for(int i=;i<=n;i++) pre[i]=lower_bound(tp+,tp++u,pre[i])-tp;
add(pre[]);
for(int i=;i<=n;i++) {
int pos=lower_bound(tp+,tp++u,tp[pre[i]]-T)-tp;
if(tp[pos]!=tp[pre[i]]-T) pos--;
ans+=i-ask(pos);
add(pre[i]);
}
cout<<ans<<endl;
return ;
}
Petya and Array
[CodeForces]1042D的更多相关文章
- Petya and Array CodeForces - 1042D (树状数组)
D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 1042d//Petya and Array// Codeforces Round #510 (Div. 2)
题意:给出一个数组,求其中和小于t的区间数. 先计算前缀和数组sum[i].对当前的sum[i],查询树状数组中有几个比(sum[i]-t)大的数,那么用sum[i]减它就是一个合法区间.再将当前的s ...
- 【Codeforces 1042D】Petya and Array
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 把a[i]处理成前缀和 离散化. 枚举i从1..n假设a[i]是区间和的a[r] 显然我们需要找到a[r]-a[l]<t的l的个数 即a ...
- Petya and Array CodeForces - 1042D
很不错的一道题 给你一个长度为n的数组,问共有多少个区间满足区间之和小于给定的数t 这种题一般做法肯定是枚举,固定左端点枚举右端点,枚举的过程需要优化,否则就是n方 这道题我先求一个前缀和,然后逆着枚 ...
- codeforces 1042D - Petya and Array【树状数组+离散化】
题目:戳这里 题意:有n个数,问有多少个区间满足[L,R]内的和小于t. 解题思路: [L,R]内的和小于t等价于sum[R]-sum[L-1]<t,将sum[L-1]左移,可以看出R与L的关系 ...
- python爬虫学习(5) —— 扒一下codeforces题面
上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...
- 【Codeforces 738D】Sea Battle(贪心)
http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- 【Codeforces 738A】Interview with Oleg
http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...
随机推荐
- POJ题目1947 Rebuilding Roads(树形dp)
Rebuilding Roads Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 9957 Accepted: 4537 ...
- 分享微软官方Demo用的SharePoint 2010, Exchange 2010, Lync 2010虚拟机
微软官方有一套专门用于SharePoint 2010, Exchange 2010 Demo的虚拟机:SharePoint 2010: Information Worker Demonstration ...
- ios最新设置状态栏字体颜色总结
状态栏的字体为黑色:UIStatusBarStyleDefault 状态栏的字体为白色:UIStatusBarStyleLightContent 一.在info.plist中,将View contro ...
- VassistX 凝视 模板
避免头文件反复包括宏定义: #ifndef $FILE_BASE_UPPER$_H_ #define $FILE_BASE_UPPER$_H_ $selected$ #endif // $FILE_B ...
- cocos2d-x开发的《派对小游戏》-github源代码分享
这是博主非常久曾经写的一个cocos2d-x跨平台小游戏,我称它为<派对小游戏>,如今分享给大家.希望对大家有所帮助的话. 项目源代码地址:https://github.com/xieba ...
- 摄像头ov2685中关于sensor id 设置的相关的寄存器地址【转】
本文转载自:http://blog.csdn.net/morixinguan/article/details/51220992 OV2685 : CHIP_ID address : 0x300A ...
- ArraySegment
第一个构造函数 Initializes a new instance of the ArraySegment<T> structure that delimits all the elem ...
- C#实现对数据库的备份还原(完全)
C#实现对数据库的备份还原 ( 用SQL语句实现对数据库备份还原操作 备份SqlServer数据库: backup database 数据库名 to disk (备份文件存放路径+文件名).bak 还 ...
- Codeforces--630H--Benches(组合数)
H - Benches Crawling in process... Crawling failed Time Limit:500MS Memory Limit:65536KB 64b ...
- Angularjs+Bootstrap实现分页指令
本插件的开发目的主要给前端同学使用,本人是专注于后台开发的,对css样式不熟悉,但逼于前端要求做一个共公组件方便日常开发,所以这个插件在样式上可能不适合大部分人,喜欢的拿走吧,不喜欢的也请别喷. 一 ...