题意:

给出一个数组,元素有正有负有0,问其区间和小于 t 的子区间的个数。

sum[ r ]-sum[ l-1 ]<t,其中sum是a的前缀和。

实现的方法就是从前往后对于每一个sum[ i ],看在它前面有多少个大于等于sum[ i ] - t 的前缀和。

树状数组维护的是 i 前面有几个数小于等于它

 #include <cstdio>
#include <cstring>
#include <queue>
#include <cmath>
#include <algorithm>
#include <set>
#include <iostream>
#include <map>
#include <stack>
#include <string>
#include <vector>
#include <bits/stdc++.h>
#define pi acos(-1.0)
#define eps 1e-9
#define fi first
#define se second
#define rtl rt<<1
#define rtr rt<<1|1
#define bug printf("******\n")
#define mem(a,b) memset(a,b,sizeof(a))
#define name2str(x) #x
#define fuck(x) cout<<#x" = "<<x<<endl
#define f(a) a*a
#define sf(n) scanf("%d", &n)
#define sff(a,b) scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
#define pf printf
#define FRE(i,a,b) for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b) for(i = a; i < b; i++)+
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define FIN freopen("data.txt","r",stdin)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) x&-x
#define rep(i,a,b) for(int i=a;i<b;++i)
#define per(i,a,b) for(int i=a-1;i>=b;--i)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const int maxn = 2e5 + ;
int n;
LL a[maxn], sum[maxn], t, cnt[maxn], c[maxn];
void update ( int pos, LL x ) {
while ( pos <= n + ) {
c[pos] += x;
pos += lowbit ( pos );
}
}
LL getsum ( int pos ) {
LL sum = ;
while ( pos > ) {
sum += c[pos];
pos -= lowbit ( pos );
}
return sum;
}
int main() {
scanf ( "%d%lld", &n, &t );
for ( int i = ; i <= n ; i++ ) {
scanf ( "%lld", &a[i] );
sum[i] = sum[i - ] + a[i];
cnt[i] = sum[i];
}
sort ( cnt, cnt + n + );
LL ans = ;
for ( int i = ; i <= n ; i++ ) {
int pos = lower_bound ( cnt, cnt + n + , sum[i - ] ) - cnt + ;//树状数组下标不能是0 所以需要将下标++
update ( pos, );
pos = lower_bound ( cnt, cnt + n + , sum[i] - t + ) - cnt; //求有多少个数小于等于sum[i] - t
ans += ( i - getsum ( pos ) );//区间的总数是 i-1 小于等于的个数是 getsum ( pos ) - 1
}
printf ( "%lld\n", ans );
return ;
}

D. Petya and Array 树状数组的更多相关文章

  1. HDU 3854 Glorious Array(树状数组)

    题意:给一些结点,每个结点是黑色或白色,并有一个权值.定义两个结点之间的距离为两个结点之间结点的最小权值当两个结点异色时,否则距离为无穷大.给出两种操作,一种是将某个结点改变颜色,另一个操作是询问当前 ...

  2. 1042.D Petya and Array 前缀 + 树状数组

    11.19.2018 1042.D Petya and ArrayNew Point: 前缀 + 树状数组 :树状数组逐个维护前缀个数 Describe: 给你一个数组,一个标记数,问你有多少区间[l ...

  3. Petya and Array CodeForces - 1042D (树状数组)

    D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)

    D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...

  5. 【CF1042D】Petya and Array 离散化+树状数组

    题目大意:给定一个长度为 N 的序列,给定常数 t,求有多少个区间 [l,r] 满足 \(\sum\limits_{i=l}^{r}a_i<t\). 题解:先跑一边前缀和,问题等价于求有多少个数 ...

  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 1042D - Petya and Array【树状数组+离散化】

    题目:戳这里 题意:有n个数,问有多少个区间满足[L,R]内的和小于t. 解题思路: [L,R]内的和小于t等价于sum[R]-sum[L-1]<t,将sum[L-1]左移,可以看出R与L的关系 ...

  8. CodeForces 122G Lucky Array(一脸懵逼的树状数组)

    Petya loves lucky numbers. Everybody knows that lucky numbers are positive integers whose decimal re ...

  9. HDU 4947 GCD Array 容斥原理+树状数组

    GCD Array Time Limit: 11000/5500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

随机推荐

  1. Mac 终端快捷键

    ctrl+A           跳转到行开头 ctrl+E           跳转到行结尾 ctrl+U           清空当前行 Command+K 清屏 Command+→多终端页面跳转 ...

  2. Notes of Daily Scrum Meeting(11.17)

    Notes of Daily Scrum Meeting(11.17) 今天是第四周的周一,也就是说距离最后发布也只剩下一周的时间,但我们的工程里面还有很多的问题没有解决,我关注过 其他一两个小组,他 ...

  3. Leetcode题库——34.在排序数组中国查找元素的第一个和最后一个位置

    @author: ZZQ @software: PyCharm @file: searchRange.py @time: 2018/11/12 19:19 要求:给定一个按照升序排列的整数数组 num ...

  4. git学习-综合性文章

    文章:[转载]理解 Git 分支管理最佳实践 首先介绍了git各种分支:

  5. Freemarker中Configuration的setClassForTemplateLoading方法参数问题

    今天使用freemarker中Configuration的setClassForTemplateLoading方法遇到了加载模板目录的一个小问题. 由于网上的其他论坛,博客写的有点乱,故记录一下. F ...

  6. FineReport基本使用

    FineReport官方开发文档链接:http://help.finereport.com 1.FineReport是帆软软件有限公司自主研发的一款企业级web报表软件产品.FineReport报表软 ...

  7. 对it行业的一些看法

    随着世界产业转移的加速,欧美.日本等发达国家将大量的软件开发业务转移到中国.印度等国家,随之而来的是这些国家对it人才的急切需求! 对比国内的大学生就业形势而言,无疑是it相关专业的毕业生就业压力较少 ...

  8. matlab的应用

    MATLAB在信号与系统中的应用 一: 看到MATLAB在信号与系统中的应用,对这部分我比较熟悉,在此举一个小例子.    impulse(sys)表示求连续系统sys的冲击响应    subplot ...

  9. HDU 1231 最大子序列

    http://acm.hdu.edu.cn/showproblem.php?pid=1231 Problem Description 给定K个整数的序列{ N1, N2, ..., NK },其任意连 ...

  10. modify headers插件的使用

    Modity headers是firefox浏览器的一个插件,作用是改变http请求的IP地址 (一)在firefox中添加该插件 步骤一:打开firefox浏览器,打开地址: https://add ...