Petya and Array CodeForces - 1042D
很不错的一道题
给你一个长度为n的数组,问共有多少个区间满足区间之和小于给定的数t
这种题一般做法肯定是枚举,固定左端点枚举右端点,枚举的过程需要优化,否则就是n方
这道题我先求一个前缀和,然后逆着枚举,显然问题转化为了对于一个数,如果寻找他右边的数哪些小于它+t,这就转化为了区间求和,可以树状数组或者线段树,但是数的范围太大,因此需要用离散化
这道题的还有一个问题是定位,有一个很简单的定位方法就是把每一个前缀和+t也放进去,这样去定位就很简单了
另外就是使用upper_bound不过找的数要减一
具体做法看代码
#include<bits/stdc++.h>
using namespace std;
#define endl "\n"
#define pf printf
#define int long long
#define IO ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
const int maxn=+;
int sum[maxn],a[maxn],up;
vector<int> v;
int n,t;
inline int lowbit(int x){return x&-x;}
void add(int x,int d)
{
while(x<=up){
sum[x]+=d;x+=lowbit(x);
}
}
int Sum(int x)
{
int ret=;
while(x>){
ret+=sum[x];
x-=lowbit(x);
}
return ret;
}
main()
{
IO;
int ans=;
cin>>n>>t;
for(int i=;i<=n;i++){
cin>>a[i];
a[i]+=a[i-];
v.push_back(a[i]);
if(a[i]<t) ans++;
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
up=v.size();
for(int i=n;i;i--){
ans+=Sum(upper_bound(v.begin(),v.end(),a[i]+t-)-v.begin());
add(lower_bound(v.begin(),v.end(),a[i])-v.begin()+,);
}
cout<<ans<<endl;
}
Petya and Array CodeForces - 1042D的更多相关文章
- Petya and Array CodeForces - 1042D (树状数组)
D. Petya and Array time limit per test 2 seconds memory limit per test 256 megabytes input standard ...
- codeforces 1042d//Petya and Array// Codeforces Round #510 (Div. 2)
题意:给出一个数组,求其中和小于t的区间数. 先计算前缀和数组sum[i].对当前的sum[i],查询树状数组中有几个比(sum[i]-t)大的数,那么用sum[i]减它就是一个合法区间.再将当前的s ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...
- Petya and Array (权值线段树+逆序对)
Petya and Array http://codeforces.com/problemset/problem/1042/D time limit per test 2 seconds memory ...
- 1042.D Petya and Array 前缀 + 树状数组
11.19.2018 1042.D Petya and ArrayNew Point: 前缀 + 树状数组 :树状数组逐个维护前缀个数 Describe: 给你一个数组,一个标记数,问你有多少区间[l ...
- [CodeForces]1042D
大意:求一个序列有几个子序列的和小于给定值,里面的数有正有负,序列长度≤200000. 列个式子,其实求的是sum[r]-sum[l-1]<T sum[r]-T<sum[l-1] 所以我们 ...
- Greg and Array CodeForces 296C 差分数组
Greg and Array CodeForces 296C 差分数组 题意 是说有n个数,m种操作,这m种操作就是让一段区间内的数增加或则减少,然后有k种控制,这k种控制是说让m种操作中的一段区域内 ...
- E - Petya and Exam CodeForces - 832B 字典树+搜索
E - Petya and Exam CodeForces - 832B 这个题目其实可以不用字典树写,但是因为之前写过poj的一个题目,意思和这个差不多,所以就用字典树写了一遍. 代码还是很好理解的 ...
- 【Codeforces 1042D】Petya and Array
[链接] 我是链接,点我呀:) [题意] 题意 [题解] 把a[i]处理成前缀和 离散化. 枚举i从1..n假设a[i]是区间和的a[r] 显然我们需要找到a[r]-a[l]<t的l的个数 即a ...
随机推荐
- Coroutine 预激装饰器
预激装饰器 讨论如何终止协程之前,我们要先谈谈如何启动协程.使用协程之前必须预激,可是这一 步容易忘记.为了避免忘记,可以在协程上使用一个特殊的装饰器.接下来介绍这样一个 装饰器. 预激协程的装饰器, ...
- 研发协同平台持续集成之Jenkins实践
导读 研发协同平台有两个核心目标,一是提高研发效率 ,二是提高研发质量,要实现这两个核心目标,实现持续集成是关键之一. 什么是持续集成 在<持续集成>一书中,对持续集成的定义如下:持续集成 ...
- 珠峰-架构6-es6
let aa = ; { console.log(aa); } // ----- let aa = ; { console.log(aa); // 报错 aa is not defined let a ...
- Python学习小记(1)---import小记
在这种目录结构下,import fibo会实际导入fibo文件夹这个module λ tree /F 卷 Programs 的文件夹 PATH 列表 卷序列号为 BC56-3256 D:. │ fib ...
- pikachu-XXE(xml external entity-injection)
部分图片来自于网络,如有侵权,请联系我及时删除~ 一.XXE的概念 1.1 什么是xml xml是一种可拓展的标记语言,可以用来存储数据,例如:我们经常看到一些.xml的文件:它还可以用来传输数据,我 ...
- C# 获取鼠标在屏幕上的位置
获取鼠标位置及鼠标单击了哪个按键.private void GetMousePoint() { Point ms = Control.MousePosition; this.label ...
- 【iOS】Spring Animations (弹性动画)
This interface shows how a spring animation can be created by specifying a “damping” (bounciness) an ...
- linux 查看系统资源使用信息的一些命令集合
linux上的进程查看及管理工具: pstree,ps,pidof,pgrep,top,htop,glances,pmap,vmstat,dstat,kill,pkill,job,bg,fg,nohu ...
- Linux nohup不输出日志文件的方法
引用:https://blog.csdn.net/guotao15285007494/article/details/84136234 最近在Linux上部署视频流推送应用时,由于网络不稳定等原因程序 ...
- Eclipse中Git图标表示内容
Eclipse中->属性->Team->Git->Label Decorations