#include <bits/stdc++.h>
using namespace std;
void just_do_it();
int main() {
#ifdef KAMIRULEZ
freopen("kamirulez.inp", "r", stdin);
freopen("kamirulez.out", "w", stdout);
#endif
ios_base::sync_with_stdio(0);
cin.tie(0);
just_do_it();
return 0;
}
const int maxn = 1e5 + 20;
pair<int, int> a[maxn];
int bit[maxn];
int h;
void update(int pos, int val) {
for (int i = pos; i <= h; i += i & (-i)) {
bit[i] += val;
}
}
int get(int pos) {
int res = 0;
for (int i = pos; i > 0; i -= i & (-i)) {
res += bit[i];
}
return res;
}
int find_max(int lt, int rt, int val) {
int res = -1;
while (lt <= rt) {
int mt = (lt + rt) / 2;
if (get(mt) >= val) {
res = mt;
lt = mt + 1;
}
else {
rt = mt - 1;
}
}
return res;
}
int find_min(int lt, int rt, int val) {
int res = -1;
while (lt <= rt) {
int mt = (lt + rt) / 2;
if (get(mt) <= val) {
res = mt;
rt = mt - 1;
}
else {
lt = mt + 1;
}
}
return res;
}
void just_do_it() {
int n;
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i].first >> a[i].second;
}
sort(a + 1, a + n + 1);
h = a[n].first;
for (int i = 1; i <= n; i++) {
int rt2 = a[i].first;
int cnt = a[i].second;
int mt = rt2 - cnt + 1;
int val = get(mt);
int rt1 = find_max(mt, rt2, val);
int lt = find_min(1, mt, val);
update(rt1 + 1, 1);
update(rt2 + 1, -1);
update(lt, 1);
update(lt + cnt - (rt2 - rt1), -1);
}
long long res = 0;
for (int i = 1; i <= h; i++) {
int val = get(i);
res += 1LL * val * (val - 1) / 2;
}
cout << res;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
340 KB |
Output is correct |
2 |
Correct |
3 ms |
724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
468 KB |
Output is correct |
2 |
Correct |
18 ms |
584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
672 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
64 ms |
1236 KB |
Output is correct |
2 |
Correct |
64 ms |
2192 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
1384 KB |
Output is correct |
2 |
Correct |
46 ms |
1972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
89 ms |
1352 KB |
Output is correct |
2 |
Correct |
53 ms |
2216 KB |
Output is correct |