AtCoder - 2581 树状数组
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?
- All input values are integers.
 - 1≤N≤2×105
 - 1≤K≤109
 - 1≤ai≤109
 
Input
Input is given from Standard Input in the following format:
N K
a1
a2
:
aN
Output
Print the number of the non-empty contiguous subsequences with an arithmetic mean that is greater than or equal to K.
Sample Input 1
3 6
7
5
7
Sample Output 1
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.
Sample Input 2
1 2
1
Sample Output 2
0
Sample Input 3
7 26
10
20
30
40
30
20
10
Sample Output 3
13 询问有多少区间和满足<=k*len;
暴力肯定不行;
我们要求的就是sum[r]-sum[l-1]>=k*(r-l+1)
--> sum[r]-k*r-(sum[l-1]-k*(l-1))>=0;
也就是求sum[r]-k*r>=sum[l-1]-k*(l-1)的数量;
树状数组!;
当然由于数量级太大,我们可以先离散化;
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 2000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-4
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii;
inline ll rd() {
ll x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/ ll n, k;
ll s[maxn];
ll c[maxn<<2];
vector<ll>vc;
void add(ll x) {
while (x <maxn) {
c[x]++; x += x & -x;
}
} ll query(ll x) {
ll res = 0;
while (x > 0) {
res += c[x]; x -= x & -x;
}
return res;
} int main() {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> k;
for (int i = 1; i <= n; i++) {
ll x;
rdllt(x);
x -= k;
s[i] = s[i - 1] + x;
}
for (int i = 0; i <= n; i++)vc.push_back(s[i]);
sort(vc.begin(), vc.end());
vc.resize(unique(vc.begin(), vc.end()) - vc.begin());
for (int i = 0; i <= n; i++)s[i] = lower_bound(vc.begin(), vc.end(), s[i]) - vc.begin() + 1;
ll res = 0;
for (int i = 0; i <= n; i++) {
res += query(s[i]); add(s[i]);
}
cout << res << endl;
return 0;
}
AtCoder - 2581 树状数组的更多相关文章
- AtCoder Beginner Contest 261 F // 树状数组
		
题目链接:F - Sorting Color Balls (atcoder.jp) 题意: 有n个球,球有颜色和数字.对相邻的两球进行交换时,若颜色不同,需要花费1的代价.求将球排成数字不降的顺序,所 ...
 - AtCoder Beginner Contest 253 F - Operations on a Matrix // 树状数组
		
题目传送门:F - Operations on a Matrix (atcoder.jp) 题意: 给一个N*M大小的零矩阵,以及Q次操作.操作1(l,r,x):对于 [l,r] 区间内的每列都加上x ...
 - 【AtCoder - 2300】Snuke Line(树状数组)
		
BUPT2017 wintertraining(15) #9A 题意 有n个纪念品,购买区间是\([l_i,r_i]\).求每i(1-m)站停一次,可以买到多少纪念品. 题解 每隔d站停一次的列车,一 ...
 - AtCoder Regular Contest 101 (ARC101) D - Median of Medians 二分答案 树状数组
		
原文链接https://www.cnblogs.com/zhouzhendong/p/ARC101D.html 题目传送门 - ARC101D 题意 给定一个序列 A . 定义一个序列 A 的中位数为 ...
 - AtCoder Regular Contest 088 E - Papple Sort(树状数组+结论)
		
结论:每次把字符丢到最外面最优,用树状数组统计答案,把字符放到最外边后可以当成消失了,直接在树状数组上删掉就好. 感性理解是把字符丢到中间会增加其他字符的移动次数,但是丢到外面不会,所以是正确的. # ...
 - BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
		
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
 - bzoj1878--离线+树状数组
		
这题在线做很麻烦,所以我们选择离线. 首先预处理出数组next[i]表示i这个位置的颜色下一次出现的位置. 然后对与每种颜色第一次出现的位置x,将a[x]++. 将每个询问按左端点排序,再从左往右扫, ...
 - codeforces 597C C. Subsequences(dp+树状数组)
		
题目链接: C. Subsequences time limit per test 1 second memory limit per test 256 megabytes input standar ...
 - BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
		
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
 
随机推荐
- 原生的ado.net(访问sql server数据库)
			
本文介绍原生的ado.net(访问sql server数据库) 写在前面 数据库连接字符串 过时的写法 string str = "server=localhost;database=my_ ...
 - ghld data format
			
%CTF: 1.00%FileType: PROF strp "VelocityProfile"%PROFSpec: 1.00 2006 00 00%Manufacturer: C ...
 - flask 电影系统(2)
			
标签,电影,上映预告数据模型设计 标签数据类型 id:编号 name:标题 movies:电影外键关联 addtime:创建时间 定义电影数据模型 id:编号 title:电影标题 url:电影地址 ...
 - 算法Sedgewick第四版-第1章基础-018一解决不能声明泛型数组的两咱方法(强转或反射)
			
1. /****************************************************************************** * Compilation: ja ...
 - JavaPersistenceWithHibernate第二版笔记-第六章-Mapping inheritance-007Inheritance of embeddable classes(@MappedSuperclass、@Embeddable、@AttributeOverrides、、)
			
一.结构 二.代码 1. package org.jpwh.model.inheritance.embeddable; import javax.persistence.MappedSuperclas ...
 - GCD 学习(八)dispatch_semaphore
			
dispatch_semaphore 信号量基于计数器的一种多线程同步机制.在多个线程访问共有资源时候,会因为多线程的特性而引发数据出错的问题. dispatch_queue_t queue ...
 - Luogu 1445 樱花
			
BZOJ 2721 唔,太菜了弄不来. 先通分:得到 $\frac{x + y}{xy} = \frac{1}{n!}$ 两边乘一下 $(x + y)n! - xy = 0$ 两边加上$(n!)^2$ ...
 - Jtabbedpane设置透明、Jpanel设置透明
			
摘自 https://zhidao.baidu.com/question/983204331427010139.html java中如何设置Jtabbedpane为透明 20 在Jtabbedpane ...
 - IDEA工作中常用快捷键
			
ctrl+shift+t: Ubuntu中在一个工具栏中打开两个终端 shift+shift: 搜索任何类 ctrl+N: 搜索任何类 ctrl+right: forward----自定义 ctrl+ ...
 - appium自动化安装(一)
			
1.首先去 https://github.com/ 了解一下appium一些相关信息 2.安装node.js:node.js官方网站:https://nodejs.org/ 安装完成,打开Wind ...