#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 mx, sum, mn;
} t[4 * N];
void push(int i, int l, int r) {
if(!lazy[i]) return;
t[i].mx += lazy[i];
t[i].sum += (r - l + 1) * lazy[i];
t[i].mn += 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.mx = max(a.mx, b.mx);
c.sum = a.sum + b.sum;
c.mn = min(a.mn, b.mn);
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 {-1, 0, INT_MAX};
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));
}
int findl(int i, int l, int r, int val) {
push(i, l, r);
if(t[i].mn > val) return -1;
if(l == r) return l;
int mid = l + r >> 1;
int x = findl(2 * i, l, mid, val);
if(x == -1) return findl(2 * i + 1, mid + 1, r, val);
return x;
}
int findr(int i, int l, int r, int val) {
push(i, l, r);
if(t[i].mx < val) return -1;
if(l == r) return l;
int mid = l + r >> 1;
int x = findr(2 * i + 1, mid + 1, r, val);
if(x == -1) return findr(2 * i, l, mid, val);
return x;
}
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; --a[i].first;
}
ll ans = 0;
sort(all(a));
for(int i = 0; i < n; ++i) {
int UP = a[i].first, DOWN = a[i].first - a[i].second + 1;
ans += query(1, 0, N - 1, DOWN, UP).sum;
int mn = query(1, 0, N - 1, DOWN, DOWN).sum;
int posl = findl(1, 0, N - 1, mn), posr = findr(1, 0, N - 1, mn);
int ln = posr - DOWN + 1;
if(mn == 0) ln = a[i].second;
modif(1, 0, N - 1, posl, posl + ln - 1, 1);
a[i].second -= ln;
if(a[i].second) {
modif(1, 0, N - 1, posr + 1, posr + a[i].second, 1);
}
}
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 'long long int findl(long long int, long long int, long long int, long long int)':
sails.cpp:63:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
63 | int mid = l + r >> 1;
| ~~^~~
sails.cpp: In function 'long long int findr(long long int, long long int, long long int, long long int)':
sails.cpp:72:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
72 | int mid = l + r >> 1;
| ~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
724 KB |
Output is correct |
2 |
Correct |
7 ms |
8276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
2900 KB |
Output is correct |
2 |
Correct |
63 ms |
1148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
56 ms |
3296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
104 ms |
5216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
190 ms |
9720 KB |
Output is correct |
2 |
Correct |
203 ms |
9704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
9932 KB |
Output is correct |
2 |
Correct |
106 ms |
8592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
366 ms |
10016 KB |
Output is correct |
2 |
Correct |
167 ms |
4420 KB |
Output is correct |