#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;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
31736 KB |
Output is correct |
2 |
Correct |
20 ms |
31608 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
31608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
20 ms |
31608 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
31660 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
26 ms |
31736 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
42 ms |
32248 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
70 ms |
32992 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
117 ms |
33784 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
204 ms |
35300 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
171 ms |
35704 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
250 ms |
36132 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |