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(树状数组)的更多相关文章

  1. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  2. Codeforces Round #368 (Div. 2) E. Garlands 二维树状数组 暴力

    E. Garlands 题目连接: http://www.codeforces.com/contest/707/problem/E Description Like all children, Ale ...

  3. Codeforces Round #216 (Div. 2) E. Valera and Queries 树状数组 离线处理

    题意:n个线段[Li, Ri], m次询问, 每次询问由cnt个点组成,输出包含cnt个点中任意一个点的线段的总数. 由于是无修改的,所以我们首先应该往离线上想, 不过我是没想出来. 首先反着做,先求 ...

  4. 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 ...

  5. D. Petya and Array 树状数组

    题意: 给出一个数组,元素有正有负有0,问其区间和小于 t 的子区间的个数. sum[ r ]-sum[ l-1 ]<t,其中sum是a的前缀和. 实现的方法就是从前往后对于每一个sum[ i ...

  6. Codeforces Round #510 (Div. 2)

    Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...

  7. 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 ...

  8. 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 ...

  9. Codeforces Round #603 (Div. 2) E. Editor(线段树)

    链接: https://codeforces.com/contest/1263/problem/E 题意: The development of a text editor is a hard pro ...

随机推荐

  1. 什么是高防服务器?如何搭建DDOS流量攻击防护系统

    关于高防服务器的使用以及需求,从以往的联众棋牌到目前发展迅猛的手机APP棋牌,越来越多的游戏行业都在使用高防服务器系统,从2018年1月到11月,国内棋牌运营公司发展到了几百家. 棋牌的玩法模式从之前 ...

  2. 675. Cut Off Trees for Golf Event

    // Potential improvements: // 1. we can use vector<int> { h, x, y } to replace Element, sortin ...

  3. 浅析express以及express中间件

    一.express: 1.express: Express是什么? Express是基于node.js平台的web应用开发框架: 作用:可以实现快速搭建骨架: 优点:开发web应用更加方便,更加快捷. ...

  4. 【TRICK】[0,n)中所有大小为k的子集的方法

    << k) - ; <<n)) { int x = comb & -comb, y = comb + x; comb = (((comb & ~y)/x)> ...

  5. Linux 下 PHP 扩展Soap 编译安装

    1.进入 PHP 的软件包 pdo 扩展目录中(注:不是 PHP 安装目录) [root@tester /]# /home/tdweb/php-5.4.34/ext/soap 执行 phpize 命令 ...

  6. performance_schema实现套路

      实现中大量使用多层次嵌套预编译,大量if else,wrapper等 不建议去看这个实现 start_mutex_wait_v1 if (flag_thread_instrumentation) ...

  7. 源码-集合:ArrayList

    只是文章摘录,还未研究 JAVA ArrayList详细介绍(示例) http://www.jb51.net/article/42764.htm Jdk1.6 JUC源码解析汇总 - 永远保持敬畏之心 ...

  8. Python中send()和sendall()的区别

    Python中send()和sendall()的区别 估计每个学习Python网络编程的人,都会遇到过这样的问题: send()和sendall()到底有什么区别? send()和sendall()原 ...

  9. 孤荷凌寒自学python第七十六天开始写Python的第一个爬虫6

    孤荷凌寒自学python第七十六天开始写Python的第一个爬虫6 (完整学习过程屏幕记录视频地址在文末) 今天在上一天的基础上继续完成对我的第一个代码程序的书写. 不过由于对python-docx模 ...

  10. hdu 1556 Color the ball (区间更新 求某点值)

    Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a ...