이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define FIO ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
using namespace std;
typedef long long ll;
typedef pair <ll, ll> pii;
#define inbuf_len 1 << 16
#define outbuf_len 1 << 16
//-------------------
const int maxn = 1e5 + 7, ofs = (1 << 17), inf = 0x3f3f3f3f;
int t[2*ofs], fen[maxn];
set <int> st[maxn];
vector <int> ad[maxn], re[maxn];
void update(int x, int val) {
x += ofs;
t[x] = val;
while (x /= 2) t[x] = min(t[2*x], t[2*x+1]);
}
int query(int x, int a, int b, int lo = 0, int hi = ofs-1) {
if (b < lo || hi < a) return inf;
if (a <= lo && hi <= b) return t[x];
int mid = (lo + hi) / 2;
return min(query(2*x, a, b, lo, mid), query(2*x+1, a, b, mid+1, hi));
}
void add(int x, int val) {for (x++; x < maxn; x += x & -x) fen[x] += val;}
int suma(int x) {
int out = 0;
for (x++; x; x -= x & -x) out += fen[x];
return out;
}
int get(int x) {
x++;
int sum = 0, y = 0;
for (int i = 16; i >= 0; i--) {
if (y+(1<<i) >= maxn) continue;
if (sum + fen[y+(1<<i)] < x) {
y += (1 << i);
sum += fen[y];
}
}
return y;
}
int GetBestPosition(int n, int c, int R, int *K, int *S, int *E) {
memset(t, inf, sizeof t);
set <pii> s; s.insert({n, n});
for (int i = 0; i < n; i++) {
add(i, 1);
s.insert({i, i});
st[i].insert(inf);
}
vector <int> le(c), ri(c);
for (int i = 0; i < c; i++) {
int l = S[i], r = E[i];
int x = get(l), y = get(r);
auto p = s.lower_bound({x, 0});
while (p -> F <= y) {
add(p -> F, -1);
p = s.erase(p);
}
add(x, 1);
le[i] = x;
ri[i] = p -> F - 1;
s.insert({le[i], ri[i]});
ad[le[i]].pb(i);
re[ri[i]].pb(i);
}
memset(fen, 0, sizeof fen);
vector <int> pre(n-1, -1), suf(n-1, -1);
for (int i = 0; i < n-1; i++) {
if (i) pre[i] = pre[i-1];
if (K[i] > R) pre[i] = i;
}
for (int i = n-2; i >= 0; i--) {
if (i < n-2) suf[i] = suf[i+1];
if (K[i] > R) suf[i] = i;
}
int mx = 0, poz = 0;
for (int i = 0; i < n; i++) {
for (auto x : ad[i]) {
st[le[x]].insert(x);
st[ri[x]].insert(x);
update(le[x], *st[le[x]].begin());
update(ri[x], *st[ri[x]].begin());
add(x, 1);
}
int rj = c;
if (i && pre[i-1] != -1) rj = min(rj, query(1, 0, pre[i-1]));
if (i < n-1 && suf[i] != -1) rj = min(rj, query(1, suf[i]+1, n-1));
rj = suma(rj-1);
if (rj > mx) {
mx = rj;
poz = i;
}
for (auto x : re[i]) {
st[le[x]].erase(x);
st[ri[x]].erase(x);
update(le[x], *st[le[x]].begin());
update(ri[x], *st[ri[x]].begin());
add(x, -1);
}
}
return poz;
}
//-------------------
/*
int main() {
int tmp;
char *inbuf, *outbuf;
inbuf = (char*) malloc(inbuf_len * sizeof(char));
outbuf = (char*) malloc(outbuf_len * sizeof(char));
tmp = setvbuf(stdin, inbuf, _IOFBF, inbuf_len);
assert(tmp == 0);
tmp = setvbuf(stdout, outbuf, _IOFBF, outbuf_len);
assert(tmp == 0);
int N, C, R;
int *K, *S, *E;
tmp = scanf("%d %d %d", &N, &C, &R);
assert(tmp == 3);
K = (int*) malloc((N-1) * sizeof(int));
S = (int*) malloc(C * sizeof(int));
E = (int*) malloc(C * sizeof(int));
int i;
for (i = 0; i < N-1; i++) {
tmp = scanf("%d", &K[i]);
assert(tmp == 1);
}
for (i = 0; i < C; i++) {
tmp = scanf("%d %d", &S[i], &E[i]);
assert(tmp == 2);
}
printf("%d\n", GetBestPosition(N, C, R, K, S, E));
return 0;
}
*/
/*
5 3 3
1
0
2
4
1 3
0 1
0 1
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |