//In the name of God
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
const ll maxn = 2e5 + 100;
const ll mod = 1e9 + 7;
const ll inf = 1e18;
#define fast_io ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define file_io freopen("input.txt", "r+", stdin);freopen("output.txt", "w+", stdout);
#define pb push_back
#define Mp make_pair
#define F first
#define S second
#define Sz(x) ll((x).size())
#define all(x) (x).begin(), (x).end()
#define lc (id << 1)
#define rc (lc | 1)
struct node{
ll al, ar, sl, sr;
node(){
al = ar = sl = sr = 0;
}
};
ll n, lz[maxn << 2];
pll a[maxn];
node seg[maxn << 2];
void bld(ll id, ll s, ll e){
seg[id].sl = e;
seg[id].sr = s;
if(e - s == 1) return;
ll mid = (s + e) >> 1;
bld(lc, s, mid);
bld(rc, mid, e);
return;
}
void Do(ll id, ll x){
seg[id].al += x;
seg[id].ar += x;
lz[id] += x;
}
pll get(ll id, ll s, ll e, ll x){
if(e - s == 1){
return Mp(s, e);
}
ll mid = (s + e) >> 1;
Do(lc, lz[id]);
Do(rc, lz[id]);
lz[id] = 0;
if(x < mid){
pll p = get(lc, s, mid, x);
if(p.S == mid && seg[lc].ar == seg[rc].al){
p.S = seg[rc].sl;
}
return p;
}
pll p = get(rc, mid, e, x);
if(p.F == mid && seg[lc].ar == seg[rc].al){
p.F = seg[lc].sr;
}
return p;
}
void upd(ll id, ll s, ll e, ll l, ll r, ll x){
if(r <= s || e <= l) return;
if(l <= s && e <= r){
Do(id, x);
return;
}
ll mid = (s + e) >> 1;
Do(lc, lz[id]);
Do(rc, lz[id]);
lz[id] = 0;
upd(lc, s, mid, l, r, x);
upd(rc, mid, e, l, r, x);
seg[id].al = seg[lc].al;
seg[id].sl = seg[lc].sl;
seg[id].ar = seg[rc].ar;
seg[id].sr = seg[rc].sr;
if(seg[rc].ar == seg[lc].al){
if(seg[id].sl == mid) seg[id].sl = seg[rc].sl;
if(seg[id].sr == mid) seg[id].sr = seg[lc].sr;
}
return;
}
ll cal(ll id, ll s, ll e){
if(e - s == 1){
return seg[id].al * (seg[id].al - 1) / 2;
}
ll mid = (s + e) >> 1;
Do(lc, lz[id]);
Do(rc, lz[id]);
lz[id] = 0;
return cal(lc, s, mid) + cal(rc, mid, e);
}
int main(){
fast_io;
bld(1, 0, maxn);
cin >> n;
for(ll i = 0; i < n; i++){
cin >> a[i].F >> a[i].S;
}
sort(a, a + n);
for(ll i = 0; i < n; i++){
pll p = get(1, 0, maxn, a[i].F - a[i].S);
p.S = min(p.S, a[i].F);
upd(1, 0, maxn, p.S, a[i].F, 1);
upd(1, 0, maxn, p.F, p.F + (a[i].S + p.S - a[i].F), 1);
}
cout << cal(1, 0, maxn);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
29548 KB |
Output is correct |
2 |
Correct |
22 ms |
29548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
29548 KB |
Output is correct |
2 |
Correct |
22 ms |
29676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
29548 KB |
Output is correct |
2 |
Correct |
21 ms |
29548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
23 ms |
29548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
23 ms |
29548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
32 ms |
29676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
60 ms |
30336 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
101 ms |
30812 KB |
Output isn't correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
161 ms |
31596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
154 ms |
31996 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
198 ms |
32228 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |