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,一个蛋糕只能放在一个体积比它小而且序号小于它的蛋糕上面,问你这样形成的上升序列中,体积和最大是多少 分析:根据他们的体积进行离散化,然后建树状数组,按照 ...
随机推荐
- html学习——基础分类总结
1. html 超文本标记语言HyperText Markup Language.html文档基本结构: <!DOCTYPE html><head> <! ...
- spring各版本jar包和源码
spring各版本jar包和源码 spring历史版本源码:https://github.com/spring-projects/spring-framework/tags spring历史jar包和 ...
- Oracle 数据库架构
Oracle 数据库架构 查看数据库在linux系统的安装目录路径情况: [root@localhost ~]# cd /u01/app/oracle/oradata/orcl/ [root@loca ...
- SpringMVC视图及REST风格
点击进入第二章:SpringMVC基础配置 什么是视图解析器? springMVC用于处理视图最重要的两个接口是ViewResolver和View. ViewResolver的主要作用是把一个逻辑上的 ...
- extjs中store的reload事件异步问题解决
转载自:http://blog.sina.com.cn/s/blog_8f8b7fc10100zd75.html store0.reload({params:{start:0, limit:10}}) ...
- LeetCode(92):反转链表 II
Medium! 题目描述: 反转从位置 m 到 n 的链表.请使用一趟扫描完成反转. 说明:1 ≤ m ≤ n ≤ 链表长度. 示例: 输入: 1->2->3->4->5-&g ...
- jQuery 选择器demo练习
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...
- Loadrunner 接口依赖测试
Action() { //利用关联获取第一个GET请求的返回XXX字段的值,并存储到response_XXX变量中. web_reg_save_param_ex( "ParamName=re ...
- stream to byte[], byte[] to srting
byte[] myBinary = new byte[paramFile.Length]; paramFile.Read(myBinary, , (int)paramFile.Length); str ...
- 2019-3-9,Servlet转跳链接详解
//以下代码,可以传递request和response对象及其属性和变量至指定页面 request.getRequestDispatcher("showAttribut.jsp") ...