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>
#define IO_OP ios::sync_with_stdio(0), cin.tie(0)
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, N = 5e5 + 7;
int n, m, k;
int *a, *l, *r;
struct node {
int sum, lz;
node() { lz = -1; }
} t[N * 4];
void apply(int v, int x, int tl, int tr) {
t[v].sum = x * (tr - tl);
t[v].lz = x;
}
void push(int v, int tl, int tr) {
if(t[v].lz != -1) {
int tm = (tl + tr) / 2;
apply(v * 2, t[v].lz, tl, tm);
apply(v * 2 + 1, t[v].lz, tm, tr);
t[v].lz = -1;
}
}
void pull(int v) {
t[v].sum = t[v * 2].sum + t[v * 2 + 1].sum;
}
void build(int v = 1, int tl = 0, int tr = n + 1) {
if(tr - tl == 1) {
apply(v, 1, tl, tr);
return;
}
int tm = (tl + tr) / 2;
build(v * 2, tl, tm);
build(v * 2 + 1, tm, tr);
pull(v);
}
void upd(int l, int r, int x, int v = 1, int tl = 0, int tr = n + 1) {
if(l <= tl && tr <= r) {
apply(v, x, tl, tr);
return;
}
push(v, tl, tr);
int tm = (tl + tr) / 2;
if(l < tm) upd(l, r, x, v * 2, tl, tm);
if(r > tm) upd(l, r, x, v * 2 + 1, tm, tr);
pull(v);
}
int get_first(int x, int v = 1, int tl = 0, int tr = n + 1) {
assert(x && t[v].sum >= x);
if(tr - tl == 1) return tl;
push(v, tl, tr);
int tm = (tl + tr) / 2;
if(t[v * 2].sum >= x) {
return get_first(x, v * 2, tl, tm);
} else {
return get_first(x - t[v * 2].sum, v * 2 + 1, tm, tr);
}
}
int st[N][20];
int p[N][20];
int GetBestPosition(int _n, int _m, int _k, int *_a, int *_l, int *_r) {
n = _n, m = _m, k = _k, a = _a, l = _l, r = _r;
build();
for(int i = 0; i < n - 1; i++)
st[i][0] = a[i];
for(int j = 1; j < 20; j++)
for(int i = 0; i + (1 << j) - 1 < n - 1; i++)
st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
auto qrt_mx = [&] (int l, int r) { // [l, r]
assert(0 <= l && l <= r && r < n - 1);
int k = __lg(r - l + 1);
return max(st[l][k], st[r - (1 << k) + 1][k]);
};
V<array<int, 3>> ev;
for(int i = 0; i < n; i++) {
ev.PB({i, 1, -i});
ev.PB({i + 1, -1, -i});
}
for(int i = 0; i < m; i++) {
int L = get_first(l[i] + 1);
int R = get_first(r[i] + 2);
// [L, R)
upd(L, R, 0);
upd(L, L + 1, 1);
ev.PB({L, 1, -(i + n)});
ev.PB({R, -1, -(i + n)});
}
sort(ALL(ev));
p[-ev[0][2]][0] = -1;
for(int i = 1; i < SZ(ev); i++) {
if(ev[i][1] == 1) { // L
if(ev[i - 1][1] == 1) {
p[-ev[i][2]][0] = -ev[i - 1][2];
} else {
p[-ev[i][2]][0] = p[-ev[i - 1][2]][0];
}
}
}
for(int j = 1; j < 20; j++)
for(int i = 0; i < n + m; i++) {
p[i][j] = -1;
if(p[i][j - 1] != -1)
p[i][j] = p[p[i][j - 1]][j - 1];
}
int best_pos = -1, mx = -1;
for(int i = 0; i < n; i++) {
int cur = 0, u = i;
for(int j = 19; j >= 0; j--) {
if(p[u][j] != -1 && qrt_mx(l[p[u][j] - n], r[p[u][j] - n] - 1) < k) {
u = p[u][j];
cur += 1 << j;
}
}
if(cur > mx) {
mx = cur;
best_pos = i;
}
}
cerr << "best pos = " << best_pos << endl;
return best_pos;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |