This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define N 1000001
using namespace std;
typedef long long ll;
const ll INF = 1e18, MOD = 1e9 + 7, MOD2 = 1e6 + 3;
ll n, m;
struct BIT {
ll t[N];
void update (int l, int r) {
r++;
for (; r <= m; r = (r | (r + 1)))
t[r]--;
for (; l <= m; l = (l | (l + 1)))
t[l]++;
}
ll get (ll x) {
ll sum = 0;
for (; x >= 0; x = (x & (x + 1)) - 1)
sum += t[x];
return sum;
}
ll lower_bound (ll n, ll x) {
int l = 0, r = n - 1;
while (l < r) {
int mid = (l + r + 1) / 2;
if (get (mid) > x) r = mid - 1;
else l = mid;
}
return l;
}
ll upper_bound (ll n, ll x) {
int l = 0, r = n - 1;
while (l < r) {
int mid = (l + r) / 2;
if (get (mid) < x) l = mid + 1;
else r = mid;
}
return l;
}
} t;
pair <ll, ll> a[N];
int main () {
cin >> n;
for (ll i = 0; i < n; i++) {
cin >> a[i].first >> a[i].second;
m = max (m, a[i].first);
}
sort (a, a + n);
for (ll i = n - 1; i >= 0; i--) {
if (!a[i].second) continue;
ll x = t.get (a[i].second - 1);
ll l = t.upper_bound (a[i].first, x);
ll r = t.lower_bound (a[i].first, x);
if (l) t.update (0, l - 1);
a[i].second -= l;
t.update (r - a[i].second + 1, r);
}
ll sum = 0;
for (ll i = 0; i < m; i++) {
ll x = t.get (i);
sum += x * (x - 1) / 2;
}
cout << sum;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |