Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array
题目链接:https://codeforces.com/contest/1042/problem/D
题意:
给出n个数,问一共有多少个区间,满足区间和小于t。
题解:
假设目前区间右端点为r,左端点为l,那么由前缀和可得知:sumr-suml-1<t,然后我们再边个形:sumr<t+suml-1,根据这个我们可以发现这有点类似于逆序对。
然后我们就可以用求解逆序对问题的解法来解这个问题了,这里不同的就是每次前面的加上t大于当前这个数即为一对逆序对。
我用的树状数组来做的,用树状数组用两种枚举方式,一种是从前往后,另一种是从后往前。
从前往后的话,如若当前是第i个位置,那么首先要保证前面i-1个数都update了,然后查询前面小于等于它减去t的数有多少,最后用i减去就可以了。
从后往前的话,如若当前是第i个位置,那么从i到最后一个位置都应插入进去,之后查询有多少个小于它加上r就行了。
这个题里面0也应该考虑进去,表示从1到x的这个区间。
代码如下(包含两种方式):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 2e5+;
ll n,t;
ll a[N],sum[N],c[N];
ll lowbit(ll x){
return x&(-x);
}
void upd(ll x,ll b){
for(;x<=N-;x+=lowbit(x)) c[x]+=b;
}
ll query(ll x){
ll ans = ;
for(;x>;x-=lowbit(x)) ans+=c[x];
return ans ;
}
int main(){
scanf("%I64d%I64d",&n,&t);
ll ans=;
for(int i=;i<=n;i++){
scanf("%I64d",&a[i]);
sum[i]=sum[i-]+a[i];
a[i]=sum[i];
}
sort(sum,sum+n+);
/*for(int i=n;i>=0;i--){
int pos1 = lower_bound(sum,sum+n+1,a[i]+t)-sum;
int pos2 = lower_bound(sum,sum+n+1,a[i])-sum+1;
ans+=query(pos1);
//printf("%d\n",pos1);
upd(pos2,1);
}*/
for(int i=;i<=n;i++){
int pos1 = lower_bound(sum,sum+n+,a[i-])-sum+;
int pos2 = upper_bound(sum,sum+n+,a[i]-t)-sum;
upd(pos1,);
ans+=i-query(pos2);
}
cout<<ans;
return ;
}
Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)的更多相关文章
- Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)
http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...
- Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力
E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...
- Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理
题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...
- Codeforces Round #300 F - A Heap of Heaps (树状数组 OR 差分)
F. A Heap of Heaps time limit per test 3 seconds memory limit per test 512 megabytes input standard ...
- D. Petya and Array 树状数组
题意: 给出一个数组,元素有正有负有0,问其区间和小于 t 的子区间的个数. sum[ r ]-sum[ l-1 ]<t,其中sum是a的前缀和. 实现的方法就是从前往后对于每一个sum[ i ...
- Codeforces Round #510 (Div. 2)
Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...
- Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组
Manthan, Codefest 19 (open for everyone, rated, Div. 1 + Div. 2)-D. Restore Permutation-构造+树状数组 [Pro ...
- 8VC Venture Cup 2016 - Final Round (Div. 2 Edition) D. Factory Repairs 树状数组
D. Factory Repairs 题目连接: http://www.codeforces.com/contest/635/problem/D Description A factory produ ...
- Codeforces Round #603 (Div. 2) E. Editor(线段树)
链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...
随机推荐
- ruby 操作csv
1.读取csv 文件中读取:一次读入全部(设置headers使 CSV#shift() 以CSV::Row对象返回而不是数组:使 CSV#read() 返回 CSV::Table 对象而不是数 ...
- My First Marathon【我的第一次马拉松】
My First Marathon A month before my first matathon, one of my ankles was injured and this meant not ...
- JAVA判断时间是否在时间区间内
package com.liying.tiger.test; import java.text.ParseException; import java.text.SimpleDateFormat; i ...
- cordova 框架下开发app推送
cordova提供官方的push pluging,使用的是Google的GCM消息推送服务,一些网络原因,国内GCM可能不怎么好用.所以选择国内的第三方插件. 可供选择的有百度云推送,腾讯云信鸽,极光 ...
- NSDictionary底层实现原理
一言以蔽之:在OC中NSDictionary是使用hash表来实现key和value的映射和存储的. 那么问题来了什么是hash表呢? 哈希表(hash表): 又叫做散列表,是根据关键码值(key v ...
- Java之枚举笔记(Enum)
package com.simope.ljm; public class MyEnum { public static void main(String[] args) { System.out.pr ...
- ant-design 实现一个登陆窗口
前提:已经完成项目实战(https://ant.design/docs/react/practical-projects-cn#定义-Model) 如果要想实现一个登陆窗口,首先得有一个ui,想到的是 ...
- 阴影效果的小 demo
早上没事干,感觉字体阴影的效果还是好看的,那么就来一个小demo吧! 1.这是html 简单的有一个标签或者盒子都可以 <div class="demo11">我爱考试 ...
- Android系统自带样式
android:theme="@android:style/Theme.Dialog" 将一个Activity显示为能话框模式 android:theme="@andr ...
- ardupilot_gazebo仿真(一)
ardupilot_gazebo仿真 标签(空格分隔): 未分类 ardupilot_gazebo仿真 官网网址 代码更新地址 Ardupilot Gazebo Plugin & Models ...