#include <bits/stdc++.h>
using namespace std;
#define int long long
#define str string
#define endl '\n'
#define all(a) a.begin(), a.end()
#define fastio ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
#define print(a) \
for (auto x : a) \
cout << x << ' '; \
cout << endl;
#define printmp(a) \
for (auto x : a) \
cout << x[0] << ' ' << x[1] << endl;
int N = 1e5 + 5;
struct SegTree {
int size = 1;
vector<int> t, ad;
SegTree (){
while(size < N)
size <<= 1;
t.resize(size << 1);
ad.resize(size << 1);
}
void add(int l, int r, int x, int lx, int rx){
if(l <= lx and rx <= r){
ad[x] ++;
return;
}
if(r <= lx or rx <= l)
return;
int mid = (lx + rx) >> 1;
add(l, r, (x << 1) + 1, lx, mid);
add(l, r, (x << 1) + 2, mid, rx);
t[x] = t[(x << 1) + 1] + ad[(x << 1) + 1] * (mid - lx) + t[(x << 1) + 2] + ad[(x << 1) + 2] * (rx - mid);
}
void add(int l, int r){
add(l, r, 0, 0, size);
}
int get(int i, int x, int lx, int rx){
if(lx + 1 == rx)
return ad[x];
int mid = (lx + rx) >> 1;
if(i < mid)
return ad[x] + get(i, (x << 1) + 1, lx, mid);
return ad[x] + get(i, (x << 1) + 2, mid, rx);
}
int get(int x){
return get(x, 0, 0, size);
}
int get(int l, int r, int x, int lx, int rx, int sum = 0){
sum += ad[x];
if(l <= lx and rx <= r)
return t[x] + sum * (rx - lx);
if(rx <= l or r <= lx)
return 0;
int mid = (lx + rx) >> 1;
return get(l, r, (x << 1) + 1, lx, mid, sum) + get(l, r, (x << 1) + 2, mid, rx, sum);
}
int get(int l, int r){
return get(l, r, 0, 0, size);
}
};
void solve(){
int n;
cin >> n;
vector<array<int, 2>> a(n);
for(int i = 0; i < n; i ++)
cin >> a[i][0] >> a[i][1];
sort(all(a));
SegTree s;
int ans = 0;
for(int i = 0; i < n; i ++){
int val = s.get(a[i][0] - a[i][1] + 1);
int l1 = 1, r1 = a[i][0] - a[i][1] + 1;
while(l1 < r1){
int mid = (l1 + r1) >> 1;
if(s.get(mid) == val)
r1 = mid;
else
l1 = mid + 1;
}
int l2 = a[i][0] - a[i][1] + 1, r2 = a[i][0];
while(l2 < r2){
int mid = (l2 + r2 + 1) >> 1;
if(s.get(mid) == val)
l2 = mid;
else
r2 = mid - 1;
}
ans += s.get(l2 + 1, a[i][0] + 1);
s.add(l2 + 1, a[i][0] + 1);
a[i][1] -= a[i][0] - l2;
ans += s.get(l1, l1 + a[i][1]);
s.add(l1, l1 + a[i][1]);
}
cout << ans;
}
signed main()
{
// fastio;
int t = 1;
// cin >> t;
while(t --){
solve();
cout << endl;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
4440 KB |
Output is correct |
2 |
Correct |
2 ms |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
4440 KB |
Output is correct |
2 |
Correct |
3 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
4444 KB |
Output is correct |
2 |
Correct |
5 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
4744 KB |
Output is correct |
2 |
Correct |
89 ms |
5212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
5472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
5716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
260 ms |
6588 KB |
Output is correct |
2 |
Correct |
237 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
249 ms |
6740 KB |
Output is correct |
2 |
Correct |
108 ms |
6644 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
329 ms |
7232 KB |
Output is correct |
2 |
Correct |
179 ms |
6964 KB |
Output is correct |