Submission #362300

#TimeUsernameProblemLanguageResultExecution timeMemory
362300ngpin04Sails (IOI07_sails)C++14
100 / 100
59 ms2412 KiB
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair using namespace std; const int N = 1e5; pair <int, int> a[N + 5]; int bit[N + 5]; int n; void update(int pos, int val) { for (; pos <= N; pos += pos & -pos) bit[pos] += val; } void rngupd(int l, int r) { update(l, 1); update(r + 1, -1); } int getval(int pos) { int res = 0; for (; pos > 0; pos -= pos & -pos) res += bit[pos]; return res; } int getpos(int x) { int cur = 0; int val = 0; for (int i = 20; i >= 0; i--) if (cur + (1 << i) <= N) { if (val + bit[cur + (1 << i)] < x) { cur += (1 << i); val += bit[cur]; } } return cur + 1; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); //freopen("file.inp","r",stdin); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i].fi >> a[i].se; sort(a + 1, a + n + 1); for (int i = 1; i <= n; i++) a[i].fi = N - a[i].fi + 1; for (int i = 1; i <= n; i++) { int pos = a[i].fi; int cnt = a[i].se; int las = getval(pos + cnt - 1); int nxt = getpos(las) - 1; if (pos <= nxt) { rngupd(pos, nxt); cnt -= (nxt - pos + 1); } int laspos = getpos(las + 1) - 1; rngupd(laspos - cnt + 1, laspos); } long long ans = 0; for (int i = 1; i <= N; i++) { int cnt = getval(i); if (cnt > 0) ans += (long long) cnt * (cnt - 1) >> 1; } cout << ans; 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...