Skip to content

Instantly share code, notes, and snippets.

@Luzhiled Luzhiled/ARC075-E.cpp
Last active Jun 4, 2017

Embed
What would you like to do?
ARC075-E
#include <bits/stdc++.h>
using namespace std;
#define fs first
#define se second
#define all(v) v.begin(), v.end()
#define rep(i, n) for (int i = 0; i < n; ++i)
#define pb emplace_back
using pii = pair<int, int>;
using vi = vector<int>;
using lint = long long;
const int inf = 1001001001;
const lint linf = 1001001001001001001ll;
const int mod = 1e9 + 7;
const int dx[]{0, 1, 0, -1, -1, -1, 1, 1}, dy[]{1, 0, -1, 0, -1, 1, -1, 1};
template<typename T> inline bool chmin(T &a, T b) { if (a > b) { a = b; } return a > b; }
template<typename T> inline bool chmax(T &a, T b) { if (a < b) { a = b; } return a < b; }
template<typename T> inline void print(const T &x, string s = "\n") { cout << x << s; }
template<typename T> inline void print(const vector<T> &v, string s = " ")
{ rep(i, v.size()) cout << v[i] << (i + 1 == v.size() ? "\n" : s); }
inline bool inside(int y, int x, int H, int W) { return 0 <= y && y < H && 0 <= x && x < W; }
inline lint in() { lint x; std::cin>>x; return x; }
lint n, k;
vector<lint> a(1, 0), zip;
map<lint, lint> unzip;
lint bit[202020];
lint sum(int i) {
i++;
lint s = 0;
while (i > 0) {
s += bit[i];
i -= i & -i;
}
return s;
}
void add(int i, int x) {
i++;
while (i <= n + 1) {
bit[i] += x;
i += i & -i;
}
}
int main() {
cin >> n >> k;
rep(i, n) a.pb(in() - k);
for (int i = 1; i <= n; ++i) {
a[i] += a[i - 1];
}
for (int i = 0; i <= n; ++i) {
zip.pb(a[i]);
}
sort(all(zip));
zip.erase(unique(all(zip)), zip.end());
for (int i = 0; i < zip.size(); ++i) {
unzip[zip[i]] = i;
}
for (int i = 0; i <= n; ++i) {
a[i] = unzip[a[i]];
}
lint ans = 0;
for (int i = 0; i <= n; ++i) {
ans += sum(a[i]);
add(a[i], 1);
}
//print(zip);
//print(a);
cout << ans << endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.