#include <bits/stdc++.h>
#define int long long
#define pi pair<int,int>
#define pb push_back
#define V vector
#define vi V<int>
#define mi map<int,int>
#define MOD 1000000007
#define MOD2 998244353
#define mp make_pair
#define ins insert
#define qi queue<int>
#define pqi priority_queue<int>
#define si set<int>
#define v2i V<vi>
#define v3i V<v2i>
#define v4i V<v3i>
#define v5i V<v4i>
#define INF 1e18
#define all(x) begin(x), end(x)
#define sz(x) (int) (x).size()
#define fastio ios_base::sync_with_stdio(false); cin.tie(nullptr);
#define endl "\n"
#define lb lower_bound
#define ub upper_bound
#define print(x) cout << x<<" ";
#define vpi V<pi>
#define Y cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define msi multiset<int>
#define F first
#define S second
#define ld long double
#define RE return;
#define IMP cout<<-1<<endl;return;
using namespace std;
template <typename T>
std::istream& operator>>(std::istream& in, std::vector<T>& vec) {
for (T& x : vec) {
in >> x;
}
return in;
}
vi arr; vi heights;
void build(int node, int l, int r){
if(l==r){
arr[node] = (l==0 ? heights[l] : heights[l]-heights[l-1]);
return;
}
int mid = (l+r)/2;
build(2*node,l,mid); build(2*node+1, mid+1, r);
arr[node] = arr[2*node]+arr[2*node+1];
}
void update(int node, int l, int r, int i, int val){
if(i == sz(heights)) return;
if(l==r){
arr[node] += val;
return;
}
int mid = (l+r)/2;
if(mid<i) update(2*node+1, mid+1, r, i, val);
else update(2*node, l, mid, i, val);
arr[node] = arr[2*node]+arr[2*node+1];
}
int query(int node, int l, int r, int tl, int tr){
if(tl>tr) return 0;
if(l==tl && r==tr) return arr[node];
int mid = (l+r)/2;
return query(2*node, l, mid, tl, min(mid, tr)) + query(2*node+1, mid+1, r, max(tl, mid+1), tr);
}
int findl(int val){
int l=0; int r = sz(heights)-1; int best = -1; int rand=0;
while(l<=r && rand<=2){
int mid = (l+r+1)/2;
if(query(1, 0, sz(heights)-1, 0, mid)<=val){
best = mid;
l = mid;
}else r = mid-1;
if(l==r) rand++;
}
return best;
}
signed main(){
int n; cin >> n;
vpi t(n);
for(int i=0; i<n; i++){
cin >> t[i].F>>t[i].S;
}
sort(all(t));
heights.resize(t[n-1].F,0); arr.resize(4*t[n-1].F);
build(1, 0, t[n-1].F-1);
for(auto &x: t){
int fst = t[n-1].F -x.F;
int lst = fst + x.S-1;
if(fst==t[n-1].F) continue;
if(fst + x.S>=t[n-1].F){
update(1, 0, t[n-1].F-1, fst, 1);
continue;
}
int jhxt = query(1, 0, t[n-1].F-1, 0, lst);
int aft = findl(jhxt); int bef = findl(jhxt-1);
if(bef>=fst){
update(1, 0, t[n-1].F-1, fst, 1);
update(1, 0, t[n-1].F-1, bef+1, -1);
update(1, 0, t[n-1].F-1, aft+1, -1);
update(1, 0, t[n-1].F-1, aft - (lst - bef)+1, 1);
}else{
update(1, 0, t[n-1].F-1, aft+1,-1);
update(1, 0, t[n-1].F-1, aft-x.S+1, 1);
}
}
int ans=0; int curr=0;
for(int i=0; i<t[n-1].F; i++){
curr = query(1,0,sz(heights)-1, 0, i);
ans += curr*(curr-1)/2;
}
cout<<ans<<endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
600 KB |
Output is correct |
2 |
Correct |
15 ms |
4184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
1780 KB |
Output is correct |
2 |
Correct |
62 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
2408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
3716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
272 ms |
6388 KB |
Output is correct |
2 |
Correct |
268 ms |
6428 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
306 ms |
6820 KB |
Output is correct |
2 |
Correct |
228 ms |
6020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
382 ms |
7052 KB |
Output is correct |
2 |
Correct |
261 ms |
4232 KB |
Output is correct |