You are given an integer sequence of length N, a= {a1,a2,…,aN}, and an integer K.
a has N(N+1)⁄2 non-empty contiguous subsequences, {al,al+1,…,ar} (1≤l≤r≤N). Among them, how many have an arithmetic mean that is greater than or equal to K?

Constraints
All input values are integers.
1≤N≤2×105
1≤K≤109
1≤ai≤109

 

输入

Input is given from Standard Input in the following format:
N K
a1
a2
:
aN

输出

Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K.

样例输入

3 6
7
5
7

样例输出

5

提示

All the non-empty contiguous subsequences of a are listed below:
{a1} = {7}
{a1,a2} = {7,5}
{a1,a2,a3} = {7,5,7}
{a2} = {5}
{a2,a3} = {5,7}
{a3} = {7}
Their means are 7, 6, 19⁄3, 5, 6 and 7, respectively, and five among them are 6 or greater. Note that {a1} and {a3} are indistinguishable by the values of their elements, but we count them individually.

数组要从0开始算,不然会少算长度为1的连续子序列。
AC代码:

#include <bits/stdc++.h>
#define ll long long
using namespace std;
struct poi{ll sum,pos;}a[200010];
ll n,k,ans,cnt,tree[200010],lisan[200010];
void read(ll &k)
{
k=0;int f=1;char c=getchar();
while(c<'0'||c>'9')c=='-'&&(f=-1),c=getchar();
while(c<='9'&&c>='0')k=k*10+c-'0',c=getchar();
k*=f;
}
bool cmp(poi a,poi b){return a.sum<b.sum;}
int lowbit(int x){return x&-x;}
void add(int x,int delta)
{
for(int i=x;i<=cnt;i+=lowbit(i))
tree[i]+=delta;
}
int sum(int x)
{
int s=0;
for(int i=x;i>=1;i-=lowbit(i))
s+=tree[i];
return s;
}
int main()
{
read(n);read(k);
for(int i=1;i<=n;i++)read(a[i].sum),a[i].sum-=k;
for(int i=1;i<=n;i++)a[i].sum+=a[i-1].sum,a[i].pos=i;
sort(a,a+1+n,cmp);
for(int i=0;i<=n;i++)
{
if(a[i].sum!=a[i-1].sum||i==0)cnt++;
lisan[a[i].pos]=cnt;
}
for(int i=n;i>=0;i--)
{
ans+=sum(cnt)-sum(lisan[i]-1);
add(lisan[i],1);
}
printf("%lld\n",ans);
}

Meaningful Mean的更多相关文章

  1. clean code meaningful names

    ---恢复内容开始--- Meaningful Names: use Intention-Revealing Names //nice,Everyone who reads your code (in ...

  2. 有意义的命名 Meaningful names

    名副其实 use intention-revealing names 变量.函数或类的名称应该已经答复了所有的大问题.它该告诉你,他为什么会存在,他做什么事,应该怎么用.我们应该选择都是致命了计量对象 ...

  3. Clean Code – Chapter 2: Meaningful Names

    Use Intention-Revealing Names The name should tell you why it exists, what it does, and how it is us ...

  4. The Info-Button Standard: Bring Meaningful Use To the Patient

    http://thehealthcareblog.com/blog/2010/01/28/the-info-button-standard-bringing-meaningful-use-to-the ...

  5. 英语阅读——A meaningful life

    这篇文章是<新视野大学英语>第四册的第八单元的文章. 1 The death of an angel of animal rights activism(活动家) does not rat ...

  6. Meaningful Use 中与HL7相关的消息及医疗文档

    汇总 HL7 消息 入出转消息 ADT A01,A03,A04,A08 免疫消息 VXU V04 电子处方消息 NEWRX Message v8.1 EDIFACT, v8.1 XML,  v10.6 ...

  7. Why Creating a Meaningful Morning Routine Will Make You More Successful

    https://medium.com/life-learning/how-creating-a-meaningful-morning-routine-will-make-you-more-succes ...

  8. [AngularJS] Transforming raw JSON data to meaningful output in AngularJS

    angular.module('APP', []) .controller('MainController', function($scope, UserConstants){ var user = ...

  9. AtCoder Regular Contest 075 E - Meaningful Mean(树状数组)

    题目大意:求一个数组中,平均值不小于k的连续子序列个数 所有数减去k,算个前缀和出来,就变成二维数点问题了. 没有修改,离线的话就是CZL所说的“NOIP最喜欢的套路”了:倒着加进BIT,以权值为数组 ...

  10. ARC075 E.Meaningful Mean(树状数组)

    题目大意:给定n和k,问an中有多少子区间的平均值大于等于k 很巧妙的一个式子,就是如果一个区间[l, r]满足条件 那么则有 sum[r] - sum[l-1] >= (r-l+1)*k 整理 ...

随机推荐

  1. Apt本地源制作

    方法一 缓存deb包 # apt install XXX # mkdir -p /usr/local/mydebs # find /var/cache/apt/archives/ -name *.de ...

  2. SAS批量导出sas7bdata至excel

    /*创建输出excel的宏*/ %macro export(inlib,intbl,outpath,outfile); proc export data=&inlib..&intbl ...

  3. Docker 安装部署RabbitMQ

    获查询镜像 docker search rabbitmq:management 可以看到如下结果: 获取镜像 docker pull rabbitmq:management 运行镜像 docker r ...

  4. Linux对外提供服务 网络操作 端口操作 1.开启服务监听端口 2.设置防火墙,放行访问端口的包 iptables&netfilter 四表五链和通堵策略

    主题: Linux服务器上软件提供服务 1.网络操作 2.端口操作 1.网络操作 本机必须能够ping通目标主机(本地虚拟机或者远程主机) 2.端口操作 1.开启服务监听端口 2.设置防火墙,放行访问 ...

  5. IOS字符串截取保留小数点后两位

    -(NSString*)getTheCorrectNum:(NSString*)tempString { //计算截取的长度 NSUInteger endLength = tempString.len ...

  6. VUE中获取url中的值

    如图:获取值 一:main.js中写入 const router = new VueRouter({ routes: [ { path: '/goodsinfo/:goodsId', componen ...

  7. QDU-GZS and String

    Description GZS has two strings s and t. In each step, GZS can select arbitrary character c of s and ...

  8. CountDownLatch MyUncaughtExceptionHandler

    package com.yd.wmsc.util; import java.text.SimpleDateFormat; import java.util.Date; import java.util ...

  9. struts的logic标签和<bean:write/>详情

    https://baike.baidu.com/item/Logic%E6%A0%87%E7%AD%BE/2292947?fr=aladdin https://blog.csdn.net/applea ...

  10. Django使用网站图标

    默认情况下,浏览器访问一个网站的时候,同时还会向服务器请求“/favicon.ico”这个URL,目的是获取网站的图标. 若是没有配置的话,Django就会返回一个404错误,并且浏览器接收到这个40 ...