#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 2e5 + 15;
const int INF = 1e18 + 10;
int n, fim, seg[4 * MAX], lzsum[4 * MAX];
vector<pair<int, int>> v;
void refresh(int p, int l, int r){
if(lzsum[p] == 0) return;
int add = lzsum[p];
seg[p] += add;
lzsum[p] = 0;
if(l == r) return;
int e = 2 * p, d = e + 1;
lzsum[e] += add;
lzsum[d] += add;
}
void update(int a, int b, int x, int p, int l, int r){
refresh(p, l, r);
if(a > r || b < l) return;
if(a <= l && r <= b){
lzsum[p] += x;
refresh(p, l, r);
return;
}
int m = (l + r) >> 1, e = 2 * p, d = e + 1;
update(a, b, x, e, l, m);
update(a, b, x, d, m + 1, r);
seg[p] = min(seg[e], seg[d]);
}
int getValue(int i, int p, int l, int r){
if(l == r) return seg[p];
refresh(p, l, r);
int m = (l + r) >> 1, e = 2 * p, d = e + 1;
refresh(e, l, m); refresh(d, m + 1, r);
if(i <= m) return getValue(i, e, l, m);
return getValue(i, d, m + 1, r);
}
int bb(int x, int p, int l, int r){
if(l == r) return l;
refresh(p, l, r);
int m = (l + r) >> 1, e = 2 * p, d = e + 1;
refresh(e, l, m); refresh(d, m + 1, r);
if(seg[e] < x) return bb(x, e, l, m);
return bb(x, d, m + 1, r);
}
int eq(int x, int p, int l, int r){
if(l == r) return l;
refresh(p, l, r);
int m = (l + r) >> 1, e = 2 * p, d = e + 1;
refresh(e, l, m); refresh(d, m + 1, r);
if(seg[e] <= x) return eq(x, e, l, m);
return eq(x, d, m + 1, r);
}
void add(int h, int k){
int val = getValue(h - k + 1, 1, 1, fim);
if(seg[1] < val){
int pos = bb(val, 1, 1, fim);
if(pos <= h){
update(pos, h, 1, 1, 1, fim);
k -= h - pos + 1;
}
}
int t = eq(val, 1, 1, fim);
update(t, t + k - 1, 1, 1, 1, fim);
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 0; i < n; i++){
int h, k; cin >> h >> k;
v.push_back({h, k});
fim = max(fim, h);
}
sort(v.begin(), v.end());
for(auto p : v){ add(p.first, p.second); }
ll ans = 0;
for(int i = 1; i <= fim; i++){
ll cur = getValue(i, 1, 1, fim);
ans += (cur * (cur - 1)) >> 1;
}
cout << ans << '\n';
return 0;
}
Compilation message
sails.cpp:6:22: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
6 | const int INF = 1e18 + 10;
| ~~~~~^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
3 ms |
436 KB |
Output is correct |
2 |
Correct |
14 ms |
2408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
940 KB |
Output is correct |
2 |
Correct |
31 ms |
764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
1856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
134 ms |
3088 KB |
Output is correct |
2 |
Correct |
118 ms |
3036 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
3172 KB |
Output is correct |
2 |
Correct |
69 ms |
3092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
3256 KB |
Output is correct |
2 |
Correct |
110 ms |
1724 KB |
Output is correct |