题意:

给出一个数组,元素有正有负有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. ansible软件2

    常用软件安装及使用目录  ansible使用1 第1章 copy模块 1.1 创建文件及写入内容 1. [root@m01 scripts]# ansible oldboy -m copy -a &q ...

  2. 关于MySql8.X设置允许root远程登陆的问题

    这是最近在mac上使用mysql workbench上遇到的一个小问题,仔细想了想其实这个问题本身就有毛病,论起正式环境来哪家公司是直接使用root去远程登录的呢?恐怕没几个,so不纠结root了创建 ...

  3. Node.js中exports,module.exports以及require方法

    在Node.js中,使用module.exports.f = ...与使用exports.f = ...是一样的,此时exports就是module.exports的一种简写方式.但是,需要注意的是, ...

  4. C++:构造函数3——浅拷贝和深拷贝

    一.默认拷贝构造函数 拷贝构造函数是一种特殊的构造函数(详情见:http://www.cnblogs.com/duwenxing/p/7429777.html),如果用户在定义类时没有显式地编写拷贝构 ...

  5. delphi 图像处理 二值化

    procedure TDR_QM_ZP_Form.Image_EZH( Bmp: TBitmap ); var p: PByteArray; Gray, x, y: Integer; begin // ...

  6. Codeforces Round #182 (Div. 1) B. Yaroslav and Time 最短路

    题目链接: http://codeforces.com/problemset/problem/301/B B. Yaroslav and Time time limit per test2 secon ...

  7. js一些常用方法总结

    这两天开始在牛客网上做一些js在线编程,发现很多编程题其实调用的js方法都差不多一样,所以觉得可以汇总一下,方便记忆也可以多多熟悉. 1.slice()方法 这个方法就是可以从已有的数组中返回选定的元 ...

  8. week3a:个人博客作业

    1.博客上的问题 阅读下面程序,请回答如下问题: using System; using System.Collections.Generic; using System.Text; namespac ...

  9. 分离链表法散列ADT

    分离链表法解决冲突的散列表ADT实现 数据结构定义如下: struct ListNode; typedef struct ListNode *Position; struct HashTbl; typ ...

  10. 抽奖系统 random()

    random() 方法可返回介于 0 ~ 1 之间的一个随机数. document.write(parseInt(10*Math.random())); //输出0-10之间的随机整数document ...