[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 ...
随机推荐
- [Debug] Use Remote Sources to Debug a Web App on an Emulator, Simulator, or Physical Device
We can emulate different operating systems, browsers, and devices within a desktop operating system. ...
- Graphics简单汇总
1.主页面布局文件 activity_main.xml(仅仅有2个button按钮) <?xml version="1.0" encoding="utf-8&quo ...
- Navicat 提示Cannot create oci environment 解决方式
一直在使用Navicat,这是一个数据库client软件.能连接多种不同类型的数据库,给我们的日常的工作带来了不少的便捷.近期.我在电脑上安装了orcale,然后,Navicat就莫名其妙的不能连接o ...
- 用Radeon RAMDisk在Windows 10中创建关机或重新启动不消失的内存虚拟盘
之前用ImDisk创建的内存虚拟盘每次关机或重新启动后就会消失,想要开机自己主动创建内存虚拟盘尽管能够用批处理来实现,但还是有点不爽.下载试用了Radeon RAMDisk(Radeon_RAMDis ...
- 关于ffmpeg的安装具体步骤和说明
一.windows安装: 直接下载地址: https://ffmpeg.zeranoe.com/builds/ https://ffmpeg.zeranoe.com/builds/win64/stat ...
- bzoj5038 四叶草魔杖
很有意思的最小生成树啊. 网上的题解大多是状压+最小生成树,经过我的试验,其实只要把每个联通块找出来,一个个做一次就可以了. 放一个状压的.懒得再写一个搜索找联通块 #include<cstdi ...
- 【HNOI 2004】 L语言
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1212 [算法] 字典树 + dp [代码] #include<bits/std ...
- 【BZOJ 1590】 Secret Message
[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1590 [算法] 字典树 [代码] #include<bits/stdc++.h ...
- wireshark解析rtp协议,流媒体中的AMR/H263/H264包的方法
抓到完整的流媒体包之后,用wireshark打开,其中的包可能不会自动映射成RTP+AMR/H263/H264的包,做如下修改操作即可:1. 把UDP 包解析成RTP/RTCP包.选中UDP包,右键 ...
- 杂项:UML
ylbtech-杂项:UML Unified Modeling Language (UML)又称统一建模语言或标准建模语言,是始于1997年一个OMG标准,它是一个支持模型化和软件系统开发的图形化语言 ...