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>
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 |
---|
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... |