Submission #714522

#TimeUsernameProblemLanguageResultExecution timeMemory
714522four_specksSails (IOI07_sails)C++17
40 / 100
21 ms3960 KiB
#include <bits/stdc++.h> using namespace std; namespace { template <typename T> struct Fenwick { Fenwick(int _n) : n(_n), tree(n + 1, 0) {} void add(int p, T x) { for (++p; p <= n; p += p & -p) tree[p] += x; } T sum(int p) { T ret = 0; for (; p; p -= p & -p) ret += tree[p]; return ret; } private: int n; vector<T> tree; }; } // namespace void solve() { const int X = 100'000; int n; cin >> n; vector<long> a(n), b(n); for (int i = 0; i < n; i++) cin >> a[i] >> b[i]; Fenwick<long> fenw(X); for (int i = 0; i < n; i++) { fenw.add(a[i] - b[i], 1); fenw.add(a[i], -1); } vector<long> v(X); for (int x = 0; x < X; x++) v[x] = fenw.sum(x + 1); stack<pair<long, int>> st; for (int i = 0; i < n; i++) { long cur = v[i]; int cnt = 1; while (!st.empty() && st.top().first <= v[i]) { auto [x, y] = st.top(); st.pop(); cur += x * y; cnt += y; } long q = cur / cnt, d = cur % cnt; if (d > 0) st.emplace(q + 1, d); st.emplace(q, cnt - d); } int m = 0; long res = 0; while (!st.empty()) { auto [x, y] = st.top(); st.pop(); m += y; res += y * x * (x - 1) / 2; } assert(m == n); cout << res << '\n'; } int main() { ios_base::sync_with_stdio(false), cin.tie(NULL); solve(); return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...