//#pragma GCC optimize("Ofast")
//#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define uint unsigned int
#define db long double
#define pb push_back
#define ppb pop_back
#define fi first
#define se second
#define mp make_pair
#define all(x) (x).begin(), (x).end()
void dout() { cerr << '\n'; }
template <typename Head, typename... Tail>
void dout(Head H, Tail... T) {
cerr << " " << H;
dout(T...);
}
#ifdef LOCAL
#define dbg(...) cerr << #__VA_ARGS__, dout(__VA_ARGS__)
#else
#define dbg(...) ;
#endif
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef pair <int, int> pii;
const int N = 100003, H = 100000;
int n;
vector <pii> vec;
struct node {
int l = 0, r = 0, mn = 0, mx = 0, lazy = 0;
void merge(const node & x, const node & y) {
mn = min(x.mn, y.mn);
mx = max(x.mx, y.mx);
}
void upd(int x) {
mn += x;
mx += x;
lazy += x;
}
};
node t[(1 << 18) + 1];
void build(int v, int l, int r) {
t[v].l = l, t[v].r = r;
if (l == r) {
return;
}
int m = l + r >> 1;
build(v << 1, l, m);
build(v << 1 | 1, m + 1, r);
t[v].merge(t[v << 1], t[v << 1 | 1]);
}
void push(int v) {
if (t[v].lazy) {
t[v << 1].upd(t[v].lazy);
t[v << 1 | 1].upd(t[v].lazy);
t[v].lazy = 0;
}
}
void update(int v, int l, int r) {
if (t[v].l > r || t[v].r < l) {
return;
}
if (t[v].l >= l && t[v].r <= r) {
t[v].upd(1);
return;
}
push(v);
update(v << 1, l, r);
update(v << 1 | 1, l, r);
t[v].merge(t[v << 1], t[v << 1 | 1]);
}
int get(int v, int x, int r) {
if (t[v].l > r) {
return 0;
}
if (t[v].r <= r) {
if (t[v].mx <= x) {
return t[v].r - t[v].l + 1;
}
if (t[v].mn > x) {
return 0;
}
}
push(v);
return get(v << 1, x, r) + get(v << 1 | 1, x, r);
}
int getmax(int v, int l, int r) {
if (t[v].l > r || t[v].r < l) {
return -1;
}
if (t[v].l >= l && t[v].r <= r) {
return t[v].mx;
}
push(v);
return max(getmax(v << 1, l, r), getmax(v << 1 | 1, l, r));
}
int main() {
ios_base::sync_with_stdio(false), cin.tie(NULL);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
#endif
cin >> n;
int x, y;
for (int i = 1; i <= n; i++) {
cin >> x >> y;
vec.pb({x, y});
}
sort(all(vec));
int h, k;
build(1, 1, H);
for (int i = 1; i <= n; i++) {
h = vec[i - 1].fi, k = vec[i - 1].se;
int l = -1, r = i + 1;
while (l < r - 1) {
int mid = l + r >> 1;
if (get(1, mid, h) <= k) {
l = mid;
} else {
r = mid;
}
}
int tmp = get(1, l, h);
if (l != -1) {
update(1, h - tmp + 1, h);
k -= tmp;
}
if (k > 0) {
int pos = h - get(1, l + 1, h) + 1;
update(1, pos, pos + k - 1);
}
}
ll ans = 0;
for (int i = 1; i <= H; i++) {
int x = getmax(1, i, i);
ans += (ll)x * (x - 1) / 2;
}
cout << ans;
}
Compilation message
sails.cpp: In function 'void build(int, int, int)':
sails.cpp:58:15: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int m = l + r >> 1;
~~^~~
sails.cpp: In function 'int main()':
sails.cpp:132:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid = l + r >> 1;
~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
5496 KB |
Output is correct |
2 |
Correct |
27 ms |
5368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
5496 KB |
Output is correct |
2 |
Correct |
29 ms |
5624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
5496 KB |
Output is correct |
2 |
Correct |
27 ms |
5496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
5496 KB |
Output is correct |
2 |
Correct |
28 ms |
5496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
5496 KB |
Output is correct |
2 |
Correct |
30 ms |
5496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
5752 KB |
Output is correct |
2 |
Correct |
150 ms |
6136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
6132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
281 ms |
6592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
413 ms |
7064 KB |
Output is correct |
2 |
Correct |
325 ms |
7272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
485 ms |
7408 KB |
Output is correct |
2 |
Correct |
283 ms |
7024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
576 ms |
7536 KB |
Output is correct |
2 |
Correct |
412 ms |
7404 KB |
Output is correct |