답안 #75447

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
75447 2018-09-09T19:53:14 Z WLZ Vudu (COCI15_vudu) C++17
140 / 140
307 ms 27968 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< pair<long long, int> > a(n);
  for (int i = 0; i < n; i++) {
    cin >> a[i].first;
    a[i].second = i;
  }
  long long p;
  cin >> p;
  for (int i = 0; i < n; i++) {
    a[i].first -= p;
  }
  for (int i = 1; i < n; i++) {
    a[i].first += a[i - 1].first;
  }
  sort(a.begin(), a.end());
  vector<int> rep(n);
  rep[a[0].second] = 0;
  for (int i = 1; i < n; i++) {
    rep[a[i].second] = rep[a[i - 1].second];
    if (a[i].first > a[i - 1].first) {
      rep[a[i].second]++;
    }
  }
  fenwick<long long> fenw(n + 1);
  long long ans = 0ll;
  for (int i = 0; i < n; i++) {
    if (a[i].first >= 0) {
      ans++;
    }
    ans += fenw.get(rep[i]);
    fenw.modify(rep[i], +1);
  }
  cout << ans << endl;
  return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 4 ms 632 KB Output is correct
2 Correct 3 ms 632 KB Output is correct
3 Correct 4 ms 724 KB Output is correct
4 Correct 304 ms 27012 KB Output is correct
5 Correct 163 ms 27012 KB Output is correct
6 Correct 268 ms 27012 KB Output is correct
7 Correct 273 ms 27012 KB Output is correct
8 Correct 235 ms 27012 KB Output is correct
9 Correct 307 ms 27968 KB Output is correct
10 Correct 268 ms 27968 KB Output is correct