Petya and Array CodeForces - 1042D (树状数组)
2 seconds
256 megabytes
standard input
standard output
Petya has an array aa consisting of nn integers. He has learned partial sums recently, and now he can calculate the sum of elements on any segment of the array really fast. The segment is a non-empty sequence of elements standing one next to another in the array.
Now he wonders what is the number of segments in his array with the sum less than tt. Help Petya to calculate this number.
More formally, you are required to calculate the number of pairs l,rl,r (l≤rl≤r) such that al+al+1+⋯+ar−1+ar<tal+al+1+⋯+ar−1+ar<t.
The first line contains two integers nn and tt (1≤n≤200000,|t|≤2⋅10141≤n≤200000,|t|≤2⋅1014).
The second line contains a sequence of integers a1,a2,…,ana1,a2,…,an (|ai|≤109|ai|≤109) — the description of Petya's array. Note that there might be negative, zero and positive elements.
Print the number of segments in Petya's array with the sum of elements less than tt.
5 4
5 -1 3 4 -1
5
3 0
-1 2 -3
4
4 -1
-2 1 -2 3
3
In the first example the following segments have sum less than 44:
- [2,2][2,2], sum of elements is −1−1
- [2,3][2,3], sum of elements is 22
- [3,3][3,3], sum of elements is 33
- [4,5][4,5], sum of elements is 33
- [5,5][5,5], sum of elements is −1−1
先补上树状数组的解法
sum[i] - sum[j] < t , 1 <= j < i,所以sum[i] - t < sum[j]
因为j是i之前的前缀和,那么我们可以找当前sum[j],小于等于sum[i]-t的,然后用ans+=(i-query),然后一直wa,看了网上题解,加入一个0的虚拟节点后(相当于每个当前前缀和),树状数组记录当前时刻1到n+1,而不是1到n;
这样就可以知道当前前缀和之前的前缀和出现情况。
#include<bits/stdc++.h>
using namespace std; typedef long long ll;
const int maxn = 2e5+; ll tree[maxn];
ll sum[maxn];
ll num[maxn];
int n;
ll t; int lowbit(int x)
{
return x&(-x);
} void add(int x)
{
for(int i=x;i<=n+;i+=lowbit(i))
{
tree[i]++;
}
} ll query(int x)
{
ll ans = ;
for(int i=x;i>;i-=lowbit(i))
{
ans += tree[i];
}
return ans;
} int main()
{
scanf("%d%lld",&n,&t);
for(int i=;i<=n;i++)
{
scanf("%lld",&sum[i]);
sum[i] += sum[i-];
num[i] = sum[i];
}
sort(num,num+n+);
ll ans = ;
for(int i=;i<=n;i++)
{
add(lower_bound(num,num+n+,sum[i-])-num+);
ans += i - query(upper_bound(num,num+n+,sum[i]-t)-num);
}
printf("%lld\n",ans);
}
Petya and Array CodeForces - 1042D (树状数组)的更多相关文章
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
- HDU 3333 | Codeforces 703D 树状数组、离散化
HDU 3333:http://acm.hdu.edu.cn/showproblem.php?pid=3333 这两个题是类似的,都是离线处理查询,对每次查询的区间的右端点进行排序.这里我们需要离散化 ...
- codeforces 341d (树状数组)
problem Iahub and Xors 题目大意 一个n*n的矩阵,要求支持两种操作. 操作1:将一个子矩阵的所有值异或某个数. 操作2:询问某个子矩阵的所以值的异或和. 解题分析 由于异或的特 ...
- CodeForces 396C 树状数组 + DFS
本主题开始看到以为段树或树状数组,但是,对于一个节点的有疑问的所有子节点的加权,这一条件被视为树的根,像 然后1号是肯定在第一层中,然后建立一个单向侧倒查,然后记录下来 其中每个节点 层,终于 两个节 ...
- 【HDU4947】GCD Array (莫比乌斯反演+树状数组)
BUPT2017 wintertraining(15) #5H HDU- 4947 题意 有一个长度为l的数组,现在有m个操作,第1种为1 n d v,给下标x 满足gcd(x,n)=d的\(a_x\ ...
- Codeforces 276E(树状数组)
题意:一棵树有n个节点,1是根节点,根节点的子节点是单链,然后如今有两种操作0 v x d表示距离节点v为d的节点权值都加x,操作1 v问v节点的权值,初始节点权值都是0. 题解:看了别人的题解才会的 ...
- Tokitsukaze and Strange Rectangle CodeForces - 1191F (树状数组,计数)
大意: 给定$n$个平面点, 定义集合$S(l,r,a)$表示横坐标$[l,r]$纵坐标$[a,\infty]$内的所有点. 求可以得到多少种不同的集合. 从上往下枚举底层最右侧点, 树状数组统计贡献 ...
- Petya and Array CodeForces - 1042D
很不错的一道题 给你一个长度为n的数组,问共有多少个区间满足区间之和小于给定的数t 这种题一般做法肯定是枚举,固定左端点枚举右端点,枚举的过程需要优化,否则就是n方 这道题我先求一个前缀和,然后逆着枚 ...
- codeforces 629D 树状数组+LIS
题意:n个圆柱形蛋糕,给你半径 r 和高度 h,一个蛋糕只能放在一个体积比它小而且序号小于它的蛋糕上面,问你这样形成的上升序列中,体积和最大是多少 分析:根据他们的体积进行离散化,然后建树状数组,按照 ...
随机推荐
- 瑞联科技:Pwp3框架 调用存储过程返还数据集合 到前端界面展示
一:代码结构: 1:Js 代码结构 2:Java 代码结构 二:前端界面展示效果 为了数据安全性:界面数据做了处理 三:全端代码展示 1:main.vop <html> <head& ...
- Confluence 6 导入一个文本文件
Confluence 允许你从 Confluence 服务器上的一个目录中导入一个文本文件,然后将这个文本文件转换为 Confluence 的页面.每一个文本文件将会在 Confluence 中创建一 ...
- matlab 测试 数字二次混频
% test2 clear; clf; close all Fs=800000;%采样频率800k fz=80000;%载波频率80k fz1=3000;%载波频率3k fj=79000;%基波频率7 ...
- PHP 抽象类、接口,traint详解
PHP底层实现(http://blog.jobbole.com/94475/) 一,抽象类:abstract abstract class HeHe{ public $age=18;//可以定义属性 ...
- redis-cluster集群搭建
Redis3.0版本之前,可以通过Redis Sentinel(哨兵)来实现高可用 ( HA ),从3.0版本之后,官方推出了Redis Cluster,它的主要用途是实现数据分片(Data Sha ...
- 左查询left join on简单总结
应用场景分析:(个人观点,欢迎小祖宗们指正补充) 适合存在父子关系的单表,以及多表的查询 话不多说上代码 代码:mapper里的sql 表名字段什么的本来是单独集中配置的,现在还原到sql中了 & ...
- 【scapy】读取pcap
scapy读取pcap包 假设有pcap包test.pcap,读取其中的分层流量信息 代码: import scapy_http.http try: import scapy.all as scapy ...
- jsp 运行时报错Cannot find a method to write property [firstName] of type [java.lang.String] in a bean of type [main.Employee]
原因: 代码没有安装bean的格式写 setFirstName写成了setFristName 错误代码 public void setFristName(String firstName) { thi ...
- eclipse java ee ide 配置
配置WEB服务器 windows->preferences->server->Runtime-Environments->Add->选择对应版本->选择安装路径-& ...
- XSS-HTML&javaSkcript&CSS&jQuery&ajax-CSS
CSS 1.表单的处理 <style> table, td, th{ border:1px; solid green;} th{ background-color:green; color ...