#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
#define block_of_code if(true)
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll lcm(ll a, ll b){return a / gcd(a, b) * b;}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ull mask){return __builtin_ctzll(mask);}
int logOf(ull mask){return 63 - __builtin_clzll(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
double rngesus_d(double l, double r){
double cur = rngesus(0, MASK(60) - 1);
cur /= MASK(60) - 1;
return l + cur * (r - l);
}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T container, string separator = " ", string finish = "\n", ostream &out = cout){
for(auto item: container) out << item << separator;
out << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct SegmentTree{
struct Node{
int val, lazy;
Node(){val = lazy = 0;}
};
int n;
vector<Node> a;
SegmentTree(int _n){
n = _n;
a.resize(n * 4 + 4);
}
void down(int id){
#define push(i) {a[i].val += a[id].lazy; a[i].lazy += a[id].lazy;}
push(id * 2); push(id * 2 + 1);
a[id].lazy = 0;
}
void update(int u, int v, int val, int l, int r, int id){
if (u <= l && r <= v){
a[id].val += val;
a[id].lazy += val;
return;
}
down(id);
int mid = (l + r) >> 1;
if (u <= mid) update(u, v, val, l, mid, id * 2);
if (v > mid) update(u, v, val, mid + 1, r, id * 2 + 1);
a[id].val = max(a[id * 2].val, a[id * 2 + 1].val);
}
void update(int u, int v, int val){update(u, v, val, 1, n, 1);}
int get(int u, int v, int l, int r, int id){
if (u <= l && r <= v) return a[id].val;
down(id);
int mid = (l + r) >> 1;
int ans = 0;
if (u <= mid) maximize(ans, get(u, v, l, mid, id * 2));
if (v > mid) maximize(ans, get(u, v, mid + 1, r, id * 2 + 1));
return ans;
}
int get(int u, int v){return get(u, v, 1, n, 1);}
int walk(int v){
int id = 1, l = 1, r = n;
while(l < r){
down(id);
int mid = (l + r) >> 1;
if (a[id * 2 + 1].val >= v){
l = mid + 1;
id = id * 2 + 1;
}
else{
r = mid;
id = id * 2;
}
}
if (a[id].val < v) return 0;
return l;
}
};
int main(void){
ios::sync_with_stdio(0);cin.tie(0); cout.tie(0);
clock_t start = clock();
const int N = 1e5 + 69;
int n; cin >> n;
vector<pair<int, int>> a(n);
for(int i = 0; i<n; ++i) cin >> a[i].first >> a[i].second;
sort(ALL(a));
SegmentTree st(N);
for(pair<int,int> i: a){
int idx = i.first - i.second + 1;
int cur = st.get(idx,idx);
int l = st.walk(cur + 1) + 1, r = st.walk(cur);
minimize(r, i.first);
st.update(l, i.first, 1);
int excess = idx - l;
if (excess) st.update(r - excess + 1, r, -1);
}
ll ans = 0;
for(int i = 1; i<N; ++i){
int cur = st.get(i, i);
ans += 1LL * cur * (cur - 1) / 2;
}
cout << ans << "\n";
cerr << "Time elapsed: " << clock() - start << " ms\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3416 KB |
Output is correct |
2 |
Correct |
8 ms |
3600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3420 KB |
Output is correct |
2 |
Correct |
11 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
3416 KB |
Output is correct |
2 |
Correct |
8 ms |
3672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
3420 KB |
Output is correct |
2 |
Correct |
9 ms |
3420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
3612 KB |
Output is correct |
2 |
Correct |
10 ms |
3612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
14 ms |
3416 KB |
Output is correct |
2 |
Correct |
33 ms |
4116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
3672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
3928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
4132 KB |
Output is correct |
2 |
Correct |
85 ms |
4952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
4184 KB |
Output is correct |
2 |
Correct |
92 ms |
4876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
4188 KB |
Output is correct |
2 |
Correct |
95 ms |
5420 KB |
Output is correct |