#include <bits/stdc++.h>
#define Y8o "main"
#define maxn (int) 1e5 + 5
#define ll long long
#define pii pair<int, int>
#define gb(i, j) ((i >> j) & 1)
#define f first
#define s second
#define int long long
using namespace std;
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll GetRandom(ll l, ll r) {
return uniform_int_distribution<ll> (l, r) (rng);
}
void iof() {
if(fopen(Y8o".inp", "r")) {
freopen(Y8o".inp", "r", stdin);
// freopen(Y8o".out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(NULL), cout.tie(NULL);
}
void ctime() {
cerr << "\n" << "\nTime elapsed: " << 1000 * clock() / CLOCKS_PER_SEC << "ms\n";
}
int n;
pii a[maxn];
struct dl { int val, lazy; } st[4 * maxn];
void down(int id)
{
ll t = st[id].lazy;
st[id * 2].val += t, st[id * 2].lazy += t;
st[id * 2 + 1].val += t, st[id * 2 + 1].lazy += t;
st[id].lazy = 0;
}
void update(int u, int v, int id = 1, int l = 1, int r = maxn - 5)
{
if(v < l || r < u) return ;
if(u <= l && r <= v) return ++st[id].val, ++st[id].lazy, void();
int mid = (l + r) >> 1; down(id);
update(u, v, id * 2, l, mid), update(u, v, id * 2 + 1, mid + 1, r);
st[id].val = max(st[id * 2].val, st[id * 2 + 1].val);
}
int get(int i, int id = 1, int l = 1, int r = maxn - 5)
{
if(i < l || r < i) return 0;
if(l == r) return st[id].val;
int mid = (l + r) >> 1; down(id);
return max(get(i, id * 2, l, mid), get(i, id * 2 + 1, mid + 1, r));
}
void solve()
{
cin >> n;
for(int i = 1; i <= n; i ++) cin >> a[i].f >> a[i].s;
sort(a + 1, a + n + 1);
// sort(a + 1, a + n + 1, [](pii x, pii y) {
// return x.f == y.f ? x.s > y.s : x.f < y.f;
// });
for(int i = 1; i <= n; i ++)
{
int h, sl; tie(h, sl) = a[i];
int st = h - sl + 1, val = get(st);
int l = 1, r = h;
while(l <= r)
{
int mid = (l + r) >> 1;
if(get(mid) >= val) l = mid + 1;
else r = mid - 1;
}
int R = r;
l = 1, r = h;
while(l <= r)
{
int mid = (l + r) >> 1;
if(get(mid) <= val) r = mid - 1;
else l = mid + 1;
}
int L = l;
update(R + 1, min((R + 1) + sl - 1, h));
if(h - R < sl)
{
sl -= h - R;
update(L, L + sl - 1);
}
}
ll ans = 0;
for(int i = 1; i <= maxn - 5; i ++)
{
int val = get(i);
ans += 1ll * val * (val - 1) / 2;
}
cout << ans;
}
signed main()
{
iof();
int nTest = 1;
// cin >> nTest;
while(nTest --) {
solve();
}
ctime();
return 0;
}
Compilation message
sails.cpp: In function 'void iof()':
sails.cpp:19:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(Y8o".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
4444 KB |
Output is correct |
2 |
Correct |
13 ms |
4548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
4444 KB |
Output is correct |
2 |
Correct |
13 ms |
4576 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4440 KB |
Output is correct |
2 |
Correct |
13 ms |
4444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
4444 KB |
Output is correct |
2 |
Correct |
12 ms |
4592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
4444 KB |
Output is correct |
2 |
Correct |
18 ms |
4584 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
4688 KB |
Output is correct |
2 |
Correct |
96 ms |
5200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
98 ms |
5204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
225 ms |
5716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
348 ms |
6480 KB |
Output is correct |
2 |
Correct |
331 ms |
6480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
372 ms |
6880 KB |
Output is correct |
2 |
Correct |
371 ms |
6472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
488 ms |
7248 KB |
Output is correct |
2 |
Correct |
359 ms |
6996 KB |
Output is correct |