[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 ...
随机推荐
- c++ 双冒号的作用
转:原文:http://www.cnblogs.com/charley_yang/archive/2011/02/24/1964217.html #define FLY 10 #line 100 &q ...
- [Cypress] Create Aliases for DOM Elements in Cypress Tests
We’ll often need to access the same DOM elements multiple times in one test. Your first instinct mig ...
- update_notifier 造成nodejs进程数量增长的问题
最近运维老大j哥找到我说了一个事儿:某私有化部署的线上环境nodejs进程数量多达1000+,对比公版线上环境的66个进程数显得十分诡异.并且单个nodejs进程所占用swap空间也较大,也不释放空间 ...
- monitor weblogic server ,Very simple to use, weblogic监控、巡检、故障简单小工具
1. 开发了一个简单的监视weblogic执行情况的小程序.各位朋友下载下来试试,不用登陆console就能够知道server的执行状况,包含了jvm.线程.jdbc.状态jms等:另一个更简 ...
- 没有终结点在侦听可以接受消息的 http://192.168.1.63:8085/LoginService。这通常是由于不正确的地址或者 SOAP 操作导致的
2016-04-08 09:15:05,581 [8] ERROR System.Threading.Thread - ErrorSystem.ServiceModel.EndpointNotFoun ...
- codeforces 931E Logical Expression dp
time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...
- constraint和index--转载
primary key和unique约束是要依赖index的,下面通过试验来看看他们之间的依赖关系! SQL> select * from tt; ID NA --------- ...
- 使用Web Workers处理线程
使用HTML 4和JavaScript创建出来的Web程序中,因为所有的处理都是在单线程中 HTML 5的Web Workers API,HTML 5中,一个Worker实际上为一个后台运行的线程.
- php write excel
/** * 写excel方法 */ function writeExcel($tabArr, $dataArr,$path) { require_once CODE_BASE2 . '/util/ph ...
- resultType和resultMap区别,,,1-1一个订单只能对应一个用户,1对多,一个用户对应多个订单
-----------------1-多