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;
#define int long long
#define endl "\n"
#define pb push_back
#define ff first
#define ss second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
const int lg = 20;
struct Fenwick{
int n; vector<int> fw;
Fenwick(int _n){
fw.assign((n = _n) + 1,0);
};
void upd(int pos,int x){
for(; pos <= n; pos += (pos & -pos)){
fw[pos] += x;
}
}
void upd(int l,int r,int x){
upd(l,1), upd(r + 1,-1);
}
int get(int pos){
int res = 0; for(; pos; pos -= (pos & -pos)){
res += fw[pos];
}
return res;
}
int find(int x,int sz){
int pos = 0;
for(int i = lg; i >= 0; i--){
if(pos + (1 << i) <= sz && fw[pos + (1 << i)] < x){
pos += (1 << i);
x -= fw[pos];
}
}
return pos;
}
};
void solve(){
int n;
cin >> n;
vector<pair<int,int>> v(n);
for(auto &[x,y] : v){
cin >> x >> y;
}
sort(rall(v));
auto check = [&](int m,int ty){
Fenwick fw(v[0].ff);
for(int i = 0; i < n; i++){
int pos = fw.find(m,v[i].ff);
if(pos - v[i].ss + 1 <= 0) return 0ll;
fw.upd(pos - v[i].ss + 1,pos,1);
}
int res = 0;
for(int i = 1; i <= v[0].ff; i++){
res += fw.get(i) * (fw.get(i) - 1) / 2;
}
return (ty ? res : 1ll);
};
int l = 1,r = 1e10;
while(l < r){
int m = (l + r) / 2;
if(check(m,0)) r = m;
else l = m + 1;
}
cout << check(l,1) << endl;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t = 1;
// cin >> t;
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... |