#include <bits/stdc++.h>
/*
Wake up, I'm wake up
Thu sang roi, em thay khong?
*/
using namespace std;
using ll = long long;
#define int long long
#define pii pair<ll, ll>
#define fi first
#define se second
const ll N = 2e5 + 5, inf = 1e18, mod = 1e9 + 7, block = 320, lim = 19;
int n;
pii a[N];
struct ST {
vector <int> st, lz;
ST (int _n) {
st.assign(4 * (_n + 1), 0);
lz.assign(4 * (_n + 1), 0);
}
void lazy(int i, int l, int r) {
if (lz[i] != 0) {
if (l < r) {
st[2 * i] += lz[i];
st[2 * i + 1] += lz[i];
lz[2 * i]++;
lz[2 * i + 1]++;
}
lz[i] = 0;
}
}
void update(int i, int l, int r, int u, int v, int val) {
if (u > r || v < l) return;
if (u <= l && r <= v) {
st[i]++;
lz[i]++;
return;
}
lazy(i, l, r);
int mid = (l + r) / 2;
update(2 * i, l, mid, u, v, val);
update(2 * i + 1, mid + 1, r, u, v, val);
st[i] = min(st[2 * i], st[2 * i + 1]);
}
int get(int i, int l, int r, int u, int v) {
lazy(i, l, r);
if (u > r || v < l) return inf;
if (u <= l && r <= v) return st[i];
int mid = (l + r) / 2;
return min(get(2 * i, l, mid, u, v), get(2 * i + 1, mid + 1, r, u, v));
}
int findL(int i, int l, int r, int val) {
lazy(i, l, r);
if (st[i] > val) return -1;
if (l == r) return l;
int mid = (l + r) / 2;
int ans = -1;
if (st[2 * i] <= val) ans = findL(2 * i, l, mid, val);
if (ans == -1) ans = findL(2 * i + 1, mid + 1, r, val);
return ans;
}
} st(N);
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
if (fopen(".inp", "r")) {
freopen(".inp", "r", stdin);
freopen(".out", "w", stdout);
}
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].fi >> a[i].se;
sort(a + 1, a + n + 1);
// for (int i = 1; i <= n; i++) cout << a[i].fi << ' ' << a[i].se << '\n';
int maxx = 0;
for (int i = 1; i <= n; i++) maxx = max(maxx, a[i].fi);
for (int i = 1; i <= n; i++) {
// cout << a[i].fi << ' ' << a[i].se << '\n';
int value = st.get(1, 1, maxx, 1, a[i].fi);
// cout << value << '\n';
int L = st.findL(1, 1, maxx, value);
int R = min(a[i].fi, L + a[i].se - 1);
st.update(1, 1, maxx, L, R, 1);
if (L + a[i].se - 1 > a[i].fi) {
int remain = (L + a[i].se - 1) - a[i].fi;
value = st.get(1, 1, maxx, 1, L - remain);
int nxtL = st.findL(1, 1, maxx, value);
st.update(1, 1, maxx, nxtL, nxtL + remain - 1, 1);
}
}
int ans = 0;
for (int i = 1; i <= maxx; i++) {
int val = st.get(1, 1, maxx, i, i);
ans += val * (val - 1) / 2;
}
cout << ans;
return 0;
}
Compilation message (stderr)
sails.cpp: In function 'int main()':
sails.cpp:72:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | freopen(".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~
sails.cpp:73:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | freopen(".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# | 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... |