This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
template <class T> class BIT {
private:
int size;
vector<T> bit;
vector<T> arr;
public:
BIT(int size) : size(size), bit(size + 1), arr(size) {}
void set(int ind, T val) { add(ind, val - arr[ind]); }
void add(int ind, T val) {
arr[ind] += val;
ind++;
for (; ind <= size; ind += ind & -ind) { bit[ind] += val; }
}
T pref_sum(int ind) {
ind++;
T total = 0;
for (; ind > 0; ind -= ind & -ind) { total += bit[ind]; }
return total;
}
};
void solve() {
int n;
cin >> n;
vector<pair<int,int>>v(n);
for(int i=0; i<n; ++i) cin >> v[i].first >> v[i].second;
sort(v.begin(), v.end());
BIT<int> bit(N);
const auto first = [&](int val, int r) {
int l = 0;
while(l < r) {
int mid = l + (r - l) / 2, x = bit.pref_sum(mid);
if(x < val) r = mid;
else l = mid + 1;
}
return l;
};
for(int i=0; i<n; ++i) {
int len = v[i].first - v[i].second, val = bit.pref_sum(len), x = first(val, v[i].first), y = first(val+1, v[i].first);
bit.add(x, 1);
bit.add(v[i].first, -1);
bit.add(y, 1);
bit.add(y + v[i].second - (v[i].first - x), -1);
}
long long ans = 0;
for(int i=0; i<N; ++i) {
int tmp = bit.pref_sum(i);
ans += 1ll * tmp * (tmp-1) / 2;
}
cout << ans << endl;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int t = 1;
while(t--) solve();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |