#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int NMAX = 1e5;
ll answer;
int n;
int h[NMAX + 1], k[NMAX + 1];
set<pair<int, int>> s;
int mars[NMAX + 5];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
for(int i = 1; i <= n; i++) {
cin >> h[i] >> k[i];
}
for(int i = 1; i <= n; i++) {
s.insert(make_pair(h[i], i));
}
while(!s.empty()) {
auto it = prev(s.end());
auto [curent_h, pos] = *it;
int next_h = max(curent_h - k[pos] + 1, 1);
mars[next_h]++;
mars[curent_h + 1]--;
k[pos] -= curent_h - next_h + 1;
curent_h = next_h - 1;
if(!k[pos]) {
s.erase(it);
}
while(!s.empty() && curent_h > 0) {
auto it = s.lower_bound(make_pair(curent_h, 0));
if(it == s.end()) {
break;
}
int pos = it->second;
int next_h = max(curent_h - k[pos] + 1, 1);
mars[next_h]++;
mars[curent_h + 1]--;
k[pos] -= curent_h - next_h + 1;
curent_h = next_h - 1;
if(!k[pos]) {
s.erase(it);
}
}
}
for(int i = 1; i <= NMAX; i++) {
mars[i] += mars[i - 1];
}
for(int i = 1; i <= NMAX; i++) {
answer += (ll) mars[i] * (mars[i] - 1) / 2;
}
cout << answer << '\n';
return 0;
}
| # | 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... |