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;
struct V {
ll min, max, midl, midr;
V () {
min = max = midl = midr = 1e9;
}
V (ll x) {
min = max = midl = midr = x;
}
V (ll a, ll b, ll c, ll d) : min (a), max (b),
midl (c), midr (d) {}
};
struct LazySeg {
V t[N];
ll ta[N], start;
V merge (V a, V b) {
return V (a.min, b.max, a.max, b.min);
}
void push (ll node) {
t[node].min += ta[node];
t[node].max += ta[node];
t[node].midl += ta[node];
t[node].midr += ta[node];
if (node < start) {
ta[2 * node] += ta[node];
ta[2 * node + 1] += ta[node];
}
ta[node] = 0;
}
void build (ll n) {
start = 1;
while (start < n) start <<= 1;
for (ll i = 0; i < n; i++)
t[start + i] = V (0);
for (ll i = start - 1; i > 0; i--)
t[i] = merge (t[2 * i], t[2 * i + 1]);
}
void update (ll node, ll tl, ll tr, ll l, ll r, ll d) {
push (node);
if (r < tl || tr < l) return;
if (l <= tl && tr <= r) {
ta[node] += d;
push (node);
return;
}
ll tm = (tl + tr) / 2;
update (2 * node, tl, tm, l, r, d);
update (2 * node + 1, tm + 1, tr, l, r, d);
t[node] = merge (t[2 * node], t[2 * node + 1]);
}
ll get (ll node, ll tl, ll tr, ll x) {
push (node);
if (tl == tr) return t[node].min;
ll tm = (tl + tr) / 2;
if (x <= tm) return get (2 * node, tl, tm, x);
else return get (2 * node + 1, tm + 1, tr, x);
}
ll lower_bound (ll node, ll x) {
push (node);
if (node >= start) return node - start;
if (t[node].midr > x) return lower_bound (2 * node, x);
else return lower_bound (2 * node + 1, x);
}
ll upper_bound (ll node, ll x) {
push (node);
if (node >= start) return node - start;
if (t[node].midl < x) return upper_bound (2 * node + 1, x);
else return upper_bound (2 * node, x);
}
} t;
ll n, m;
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);
}
t.build (m);
sort (a, a + n);
for (ll i = n - 1; i >= 0; i--) {
ll x = t.get (1, 0, t.start - 1, a[i].second - 1);
ll l = t.upper_bound (1, x), r = min (t.lower_bound (1, x), a[i].first - 1);
t.update (1, 0, t.start - 1, 0, l - 1, 1);
a[i].second -= l;
t.update (1, 0, t.start - 1, r - a[i].second + 1, r, 1);
}
ll sum = 0;
for (ll i = 0; i < m; i++) {
ll x = t.get (1, 0, t.start - 1, 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... |