#include <bits/stdc++.h>
using namespace std;
using ll = long long;
pair<int, int> a[100100];
int tree[400400], lazy[400400];
ll ans = 0;
void lzy(int now, int l, int r) {
if (lazy[now] == 0) return;
tree[now] += lazy[now];
if (l != r) {
lazy[2 * now] += lazy[now];
lazy[2 * now + 1] += lazy[now];
}
lazy[now] = 0;
}
void update(int ql, int qr, int now = 1, int l = 1, int r = 100000) {
lzy(now, l, r);
if (l > qr || r < ql) return;
if (ql <= l && r <= qr) {
lazy[now]++;
lzy(now, l, r);
return;
}
int mid = (l + r) / 2;
update(ql, qr, 2 * now, l, mid), update(ql, qr, 2 * now + 1, mid + 1, r);
tree[now] = max(tree[2 * now], tree[2 * now + 1]);
}
int query(int v, int now = 1, int l = 1, int r = 100000) {
lzy(now, l, r);
if (l == r) return tree[now];
int mid = (l + r) / 2;
if (v <= mid) return query(v, 2 * now, l, mid);
else return query(v, 2 * now + 1, mid + 1, r);
}
void print(int now = 1, int l = 1, int r = 100000) {
lzy(now, l, r);
if (l == r) {
if (l <= 4) cout << tree[now] << ' ';
return;
}
int mid = (l + r) / 2;
print(2 * now, l, mid);
print(2 * now + 1, mid + 1, r);
}
void dfs(int now = 1, int l = 1, int r = 100000) {
lzy(now, l, r);
if (l == r) {
ans += 1ll * tree[now] * (tree[now] - 1) / 2;
return;
}
int mid = (l + r) / 2;
dfs(2 * now, l, mid);
dfs(2 * now + 1, mid + 1, r);
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int n;
cin >> n;
for (int i = 1;i <= n;i++) {
cin >> a[i].first >> a[i].second;
}
sort(a + 1, a + 1 + n);
for (int i = 1;i <= n;i++) {
int h = a[i].first, k = a[i].second;
if (h == k) {
update(1, h);
continue;
}
int mn = query(h);
int v = query(h - k + 1);
int l = 1, r = h - k + 1;
while (l < r) {
int mid = (l + r) / 2;
if (query(mid) == v) r = mid;
else l = mid + 1;
}
int p = l;
if (v != mn) {
l = h - k + 2, r = n;
while (l < r) {
int mid = (l + r) / 2;
if (query(mid) < v) r = mid;
else l = mid + 1;
}
update(l, h);
k -= h - l + 1;
}
if (k != 0) update(p, p + k - 1);
}
dfs();
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
2 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
5 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
19 ms |
1116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
1364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
193 ms |
1696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
302 ms |
3064 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
300 ms |
3212 KB |
Output is correct |
2 |
Correct |
119 ms |
2612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
445 ms |
3428 KB |
Output is correct |
2 |
Correct |
219 ms |
1872 KB |
Output is correct |