#include "bits/stdc++.h"
using namespace std;
#define ll long long
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(),v.rend()
#define pb push_back
#define sz(a) (int)a.size()
#define int long long
const int N = 1e5 + 10;
int lazy[4 * N];
struct node {
int mn, last_min, mx;
} t[4 * N];
void push(int i, int l, int r) {
if(!lazy[i]) return;
t[i].mn += lazy[i];
t[i].mx += lazy[i];
if(l != r) {
lazy[2 * i] += lazy[i];
lazy[2 * i + 1] += lazy[i];
}
lazy[i] = 0;
}
node merge(node a, node b) {
node c;
c.mn = min(a.mn, b.mn);
c.mx = max(a.mx, b.mx);
if(a.mn < b.mn) c.last_min = a.last_min;
else c.last_min = b.last_min;
return c;
}
void modif(int i, int l, int r, int tl, int tr, int val) {
push(i, l, r);
if(l > tr || r < tl) return;
if(l >= tl && r <= tr) {
lazy[i] += val;
push(i, l, r);
return;
}
int mid = l + r >> 1;
modif(2 * i, l, mid, tl, tr, val);
modif(2 * i + 1, mid + 1, r, tl, tr, val);
t[i] = merge(t[2 * i], t[2 * i + 1]);
}
node query(int i, int l, int r, int tl, int tr) {
push(i, l, r);
if(l > tr || r < tl) return {INT_MAX, -1, -1};
if(l >= tl && r <= tr) return t[i];
int mid = l + r >> 1;
return merge(query(2 * i, l, mid, tl, tr), query(2 * i + 1, mid + 1, r, tl, tr));
}
void build(int i, int l, int r) {
if(l > r) return;
if(l == r) {
t[i].mn = 0;
t[i].last_min = l;
t[i].mx = 0;
return;
}
int mid = l + r >> 1;
build(2 * i, l, mid);
build(2 * i + 1, mid + 1, r);
t[i] = merge(t[2 * i], t[2 * i + 1]);
}
void solve() {
int n; cin >> n;
vector<pair<int, int>> a(n);
for(int i = 0; i < n; ++i) {
cin >> a[i].first >> a[i].second;
}
vector<int> cnt((int)1e5 + 5, 0);
ll ans = 0;
sort(rall(a));
build(1, 0, N - 1);
for(int i = n - 1; i >= 0; --i) {
vector<pair<int, int>> v;
int cnt = 0;
while(a[i].second) {
assert(++cnt <= 10);
node root = query(1, 0, N - 1, 1, a[i].first);
int UP = root.last_min;
int l = 1, r = UP, DOWN = -1;
while(l <= r) {
int mid = l + r >> 1;
if(query(1, 0, N - 1, mid, UP).mx != root.mn) {
l = mid + 1;
} else {
DOWN = max(mid, UP - a[i].second + 1);
r = mid - 1;
}
}
ans += root.mn * (UP - DOWN + 1);
v.pb({DOWN, UP});
modif(1, 0, N - 1, DOWN, UP, (int)1e5 + 5);
a[i].second -= UP - DOWN + 1;
}
for(auto x: v) {
modif(1, 0, N - 1, x.first, x.second, -(int)(1e5 + 4));
}
}
cout << ans << '\n';
}
int32_t main() {
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int t = 1;
//cin >> t;
while(t--) {
solve();
}
}
Compilation message
sails.cpp: In function 'void modif(long long int, long long int, long long int, long long int, long long int, long long int)':
sails.cpp:46:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | int mid = l + r >> 1;
| ~~^~~
sails.cpp: In function 'node query(long long int, long long int, long long int, long long int, long long int)':
sails.cpp:55:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | int mid = l + r >> 1;
| ~~^~~
sails.cpp: In function 'void build(long long int, long long int, long long int)':
sails.cpp:66:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
66 | int mid = l + r >> 1;
| ~~^~~
sails.cpp: In function 'void solve()':
sails.cpp:90:29: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
90 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7252 KB |
Output is correct |
2 |
Correct |
4 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7240 KB |
Output is correct |
2 |
Correct |
4 ms |
7268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
12 ms |
14568 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
12 ms |
14804 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
14 ms |
15188 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
19 ms |
15468 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
23 ms |
16136 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
41 ms |
19868 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
33 ms |
17376 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
45 ms |
18156 KB |
Execution killed with signal 6 |
2 |
Halted |
0 ms |
0 KB |
- |