Submission #75552

# Submission time Handle Problem Language Result Execution time Memory
75552 2018-09-10T09:36:13 Z noclever Vudu (COCI15_vudu) C++14
140 / 140
413 ms 23928 KB
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
 
using namespace std;
 
template<typename T>
class fenwick {
  private:
    int n;
    vector<T> fenw;
  public:
 
    fenwick(int _n) : n(_n) {
      fenw.resize(n);
    }
 
    void modify(int x, T v) {
      while (x < n) {
        fenw[x] += v;
        x |= (x + 1);
      }
    }
 
    T get(int x) {
      T v{};
      while (x >= 0) {
        v += fenw[x];
        x = (x & (x + 1)) - 1;
      }
      return v;
    }
};
 
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  vector<long long> a(n);
  vector<long long> b(n);
  vector<int> mp(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  long long p;
  cin >> p;
  for (int i = 0; i < n; i++) {
    a[i] -= p;
  }
  b[0] = a[0];
  for (int i = 1; i < n; i++) {
    a[i] += a[i - 1];
    b[i] = a[i];
  } 
  sort(b.begin(), b.end());
  for (int i = 0; i < n; i++) {
    mp[i] = lower_bound(b.begin(), b.end(), a[i]) - b.begin();
  }
  fenwick<int> fenw(n);
  long long ans = 0ll;
  for (int i = 0; i < n; i++) {
    if (a[i] >= 0) {
      ans++;
    }
    int tmp = mp[i];
    ans += (fenw.get(tmp));
    fenw.modify(tmp, +1);
  }
  cout << ans << endl;
  return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 4 ms 504 KB Output is correct
2 Correct 4 ms 636 KB Output is correct
3 Correct 4 ms 636 KB Output is correct
4 Correct 413 ms 23104 KB Output is correct
5 Correct 255 ms 23104 KB Output is correct
6 Correct 352 ms 23104 KB Output is correct
7 Correct 362 ms 23104 KB Output is correct
8 Correct 331 ms 23104 KB Output is correct
9 Correct 402 ms 23928 KB Output is correct
10 Correct 364 ms 23928 KB Output is correct