This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
using ar = array<int, 2>;
const int INF = 1ll << 30;
const int N = 1e6 + 6;
const int SQ = 3000;
const int SQQ = 2000 + 5;
int n, x[N], p[N], q, ans[N], mx, X[N], tmp[N], S[4 * N], delta[4 * N];
vector<int> V[N / SQ + 5];
ar M[SQQ], a[N], b[N];
void build(int v, int l, int r) {
if (l + 1 == r) {
S[v] = p[l];
return;
}
int m = l + r >> 1;
build(v << 1, l, m);
build(v << 1 | 1, m, r);
S[v] = min(S[v << 1], S[v << 1 | 1]);
}
void add(int v, int l, int r, int L, int R) {
if (R <= l || r <= L) return;
if (L <= l && r <= R) {
delta[v]--;
S[v]--;
return;
}
int m = l + r >> 1;
add(v << 1, l, m, L, R);
add(v << 1 | 1, m, r, L, R);
S[v] = min(S[v << 1], S[v << 1 | 1]) + delta[v];
}
int find(int v, int l, int r) {
if (l + 1 == r) {
S[v] = INF;
if (l + 1 != q) add(1, 0, q, l + 1, q);
return l;
}
int m = l + r >> 1, x;
if (S[v << 1 | 1] <= S[v << 1]) x = find(v << 1 | 1, m, r);
else x = find(v << 1, l, m);
S[v] = min(S[v << 1], S[v << 1 | 1]) + delta[v];
return x;
}
void seq() {
build(1, 0, q);
FOR(i, 0, q) {
int t = find(1, 0, q);
a[i][0] = t, a[i][1] = x[t];
}
}
void solve() {
while (true) {
ar lis = {0, INF};
fill(M, M + SQQ, lis);
FOR(i, 0, q) {
ar p = *max_element(M, M + a[i][1]);
p[0]++, p[1] = min(p[1], -a[i][0]);
M[a[i][1]] = max(M[a[i][1]], p);
}
lis = *max_element(M, M + SQQ);
for (int i = q - 1; i >= -lis[1]; i--) ans[i] = lis[0];
if (!lis[1]) return;
int ptr = 0;
FOR(i, 0, q) if (a[i][0] < -lis[1]) b[ptr++] = a[i];
q = ptr;
copy(b, b + ptr, a);
}
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> q;
int tq = q;
vector<int> X;
FOR(i, 0, q) {
cin >> p[i] >> x[i];
X.push_back(x[i]);
}
sort(X.begin(), X.end());
X.resize(unique(X.begin(), X.end()) - X.begin());
FOR(i, 0, q) {
x[i] = lower_bound(X.begin(), X.end(), x[i]) - X.begin() + 1;
}
assert(X.size() <= SQQ);
seq();
solve();
FOR(i, 0, tq) cout << ans[i] << '\n';
}
Compilation message (stderr)
Main.cpp: In function 'void build(int, int, int)':
Main.cpp:19:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
19 | int m = l + r >> 1;
| ~~^~~
Main.cpp: In function 'void add(int, int, int, int, int)':
Main.cpp:31:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | int m = l + r >> 1;
| ~~^~~
Main.cpp: In function 'int find(int, int, int)':
Main.cpp:42:19: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
42 | int m = l + r >> 1, x;
| ~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |