#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAX = 2e5 + 15;
const int INF = 0x3f3f3f3f;
#define int long long int
struct Node{
int f, lzsum;
Node(int a = 0, int b = 0){
f = a, lzsum = b;
}
Node operator + (Node other){
if(f < other.f) return Node(f, 0);
return Node(other.f, 0);
}
};
int n, fim;
vector<pair<int, int>> v;
Node seg[4 * MAX];
void build(int p, int l, int r){
if(l == r){
seg[p].f = 0;
seg[p].lzsum = 0;
return;
}
int m = (l + r) >> 1, e = 2 * p, d = e + 1;
build(e, l, m); build(d, m + 1, r);
seg[p] = seg[e] + seg[d];
}
void refresh(int p, int l, int r){
if(seg[p].lzsum == 0) return;
int add = seg[p].lzsum;
seg[p].f += add;
seg[p].lzsum = 0;
if(l == r) return;
int e = 2 * p, d = e + 1;
seg[e].lzsum += add;
seg[d].lzsum += 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){
seg[p].lzsum += 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] = seg[e] + seg[d];
}
Node query(int a, int b, int p, int l, int r){
refresh(p, l, r);
if(a > r || b < l) return Node(INF, 0);
if(a <= l && r <= b) return seg[p];
int m = (l + r) >> 1, e = 2 * p, d = e + 1;
return query(a, b, e, l, m) + query(a, b, d, m + 1, r);
}
int bb(int x, int p, int l, int r){ //first lower
if(l == r) return l;
refresh(p, l, r);
int m = (l + r) >> 1, e = 2 * p, d = e + 1;
if(seg[e].f < x) return bb(x, e, l, m);
return bb(x, d, m + 1, r);
}
int eq(int x, int p, int l, int r){ //first equal
if(l == r) return l;
refresh(p, l, r);
int m = (l + r) >> 1, e = 2 * p, d = e + 1;
if(seg[e].f <= x) return eq(x, e, l, m);
return eq(x, d, m + 1, r);
}
void add(int h, int k){
int val = query(h - k + 1, h - k + 1, 1, 1, fim).f;
if(seg[1].f < val){ //atualiza "topo" - "prefixo"
int pos = bb(val, 1, 1, fim);
if(pos <= h){ //atualiza "bottom" - "sufixo"
update(pos, h, 1, 1, 1, fim);
//cerr << "ADDED IN INTERVAL " << pos << ' ' << h << '\n';
k -= h - pos + 1;
}
}
int t = eq(val, 1, 1, fim);
update(t, t + k - 1, 1, 1, 1, fim);
//cerr << "ADDED IN INTERVAL " << t << ' ' << t + k - 1 << '\n';
}
int32_t main(void){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> n;
for(int i = 1; i <= n; i++){
int h, k; cin >> h >> k;
v.emplace_back(h, k);
fim = max(fim, h);
}
build(1, 1, fim);
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 = query(i, i, 1, 1, fim).f;
ans += (cur * (cur - 1)) / 2;
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
12756 KB |
Output is correct |
2 |
Correct |
5 ms |
12780 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
12756 KB |
Output is correct |
2 |
Correct |
7 ms |
12756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12756 KB |
Output is correct |
2 |
Correct |
7 ms |
12756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
6 ms |
12756 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
12884 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
17 ms |
13012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
44 ms |
13400 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
66 ms |
13908 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
92 ms |
14928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
123 ms |
15012 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
148 ms |
14948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |