【CF1042D】Petya and Array 离散化+树状数组
题目大意:给定一个长度为 N 的序列,给定常数 t,求有多少个区间 [l,r] 满足 \(\sum\limits_{i=l}^{r}a_i<t\)。
题解:先跑一边前缀和,问题等价于求有多少个数对 \((i,j)\) 满足 \(sum[i]-sum[j]<t\) 成立。sum 的值比较大,考虑离散化一下,将 sum[0] - sum[n] 下标映射为 1-tot。最后从前到后扫一遍树状数组更新答案即可。
注:在权值树状数组中查询小于 t 的数个数的时候,用 lower_bound 函数,查询小于等于 t 的时候,用 upper_bound 函数,大于和大于等于考虑容斥,转化为对应的小于和小于等于即可。
代码如下
#include <bits/stdc++.h>
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define all(x) x.begin(),x.end()
#define cls(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
const int dx[]={0,1,0,-1};
const int dy[]={1,0,-1,0};
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
const int maxn=2e5+10;
const double eps=1e-6;
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline ll sqr(ll x){return x*x;}
inline ll fpow(ll a,ll b,ll c){ll ret=1%c;for(;b;b>>=1,a=a*a%c)if(b&1)ret=ret*a%c;return ret;}
inline ll read(){
ll x=0,f=1;char ch;
do{ch=getchar();if(ch=='-')f=-1;}while(!isdigit(ch));
do{x=x*10+ch-'0';ch=getchar();}while(isdigit(ch));
return f*x;
}
/*------------------------------------------------------------*/
int n;
ll sum[maxn],d[maxn],tot,bit[maxn];
ll t,ans;
void modify(int pos,ll val){
for(int i=pos;i<=n+1;i+=i&-i)bit[i]+=val;
}
ll query(int pos){
ll ret=0;
for(int i=pos;i;i-=i&-i)ret+=bit[i];
return ret;
}
void read_and_parse(){
n=read(),t=read();
for(int i=2;i<=n+1;i++)sum[i]=d[i]=read()+sum[i-1];
sort(d+1,d+n+2);
tot=unique(d+1,d+n+2)-d-1;
for(int i=1;i<=n+1;i++)sum[i]=lower_bound(d+1,d+tot+1,sum[i])-d;
}
void solve(){
modify(sum[1],1);
for(int i=2;i<=n+1;i++){
int idx=upper_bound(d+1,d+tot+1,d[sum[i]]-t)-d-1;
ans+=i-1-query(idx);
modify(sum[i],1);
}
printf("%lld\n",ans);
}
int main(){
read_and_parse();
solve();
return 0;
}
【CF1042D】Petya and Array 离散化+树状数组的更多相关文章
- codeforces 1042D - Petya and Array【树状数组+离散化】
题目:戳这里 题意:有n个数,问有多少个区间满足[L,R]内的和小于t. 解题思路: [L,R]内的和小于t等价于sum[R]-sum[L-1]<t,将sum[L-1]左移,可以看出R与L的关系 ...
- 1042.D Petya and Array 前缀 + 树状数组
11.19.2018 1042.D Petya and ArrayNew Point: 前缀 + 树状数组 :树状数组逐个维护前缀个数 Describe: 给你一个数组,一个标记数,问你有多少区间[l ...
- Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)
D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...
- CodeForces 540E - Infinite Inversions(离散化+树状数组)
花了近5个小时,改的乱七八糟,终于A了. 一个无限数列,1,2,3,4,...,n....,给n个数对<i,j>把数列的i,j两个元素做交换.求交换后数列的逆序对数. 很容易想到离散化+树 ...
- Ultra-QuickSort(归并排序+离散化树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 50517 Accepted: 18534 ...
- HDU 5862 Counting Intersections(离散化+树状数组)
HDU 5862 Counting Intersections(离散化+树状数组) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5862 D ...
- BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组
BZOJ_4627_[BeiJing2016]回转寿司_离散化+树状数组 Description 酷爱日料的小Z经常光顾学校东门外的回转寿司店.在这里,一盘盘寿司通过传送带依次呈现在小Z眼前.不同的寿 ...
- poj-----Ultra-QuickSort(离散化+树状数组)
Ultra-QuickSort Time Limit: 7000MS Memory Limit: 65536K Total Submissions: 38258 Accepted: 13784 ...
- Code Forces 652D Nested Segments(离散化+树状数组)
Nested Segments time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...
随机推荐
- Day 4-1 模块的导入方法和路径
什么是模块? 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,我们把很多函数分组,分别放到不同的文件里,这样,每个文件包含的代码 ...
- AspectJ用注解替换xml配置
AspectJ基于注解的使用 AspectJ简介 AspectJ是一个基于Java语言的AOP框架,一般 其主要用途:自定义开发 一般情况下spring自动生成代理,要配置aop, 首先确定目标类,a ...
- Python自动化测试之selenium从入门到精通
1. 安装selenium 首先确保python安装成功,输入python -V 在windows下使用pip安装selenium,详情如图所示: 在ubuntu下使用pip install sele ...
- js定时函数,定时改变字体的大小
<html> <head> </head> <body> <div id="d"> js控制字体大小 </div& ...
- 【Python练习题】程序5
#题目:输入三个整数x,y,z,请把这三个数由小到大输出. # a = input('请输入整数: \n') # # b = input('请输入整数: \n') # # c = input('请输入 ...
- 【python练习题】程序2
2.题目:企业发放的奖金根据利润提成.利润(I)低于或等于10万元时,奖金可提10%:利润高于10万元,低于20万元时,低于10万元的部分按10%提成,高于10万元的部分,可提成7.5%:20万到40 ...
- Nginx 多核cpu负载均衡
L:122 查看Linux CPU 缓存大小 cat /sys/devices/system/cpu/cpu1/cache/index0/size //指令缓存 cat /sys/devices/sy ...
- json-lib和dom4j实现JSON转XML
package com.geostar.gfstack.operationcenter.test; import net.sf.json.JSONObject; import net.sf.json. ...
- Gym100496H-House of Representatives-树
树上每个元素有一个p,元素之间有距离d,计算一个元素u,使得sigma(d(i,u)*pi)最小. 两次dfs,第一次计算本节点以下的sigma(),第二次利用sump求解出ans. #include ...
- 【BZOJ2333】【SCOI2011】棘手的操作 treap合并
题目大意 有\(n\)个节点,标号从1到\(n\),这\(n\)个节点一开始相互不连通.第\(i\)个节点的初始权值为\(a_i\),接下来有如下一些操作: \(U~x~y\):加一条边,连接第\(x ...