이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <vector>
#include <set>
#include <cassert>
#define rep(i, n) for (int i=0; i<(n); i++)
#define all(xs) xs.begin(), xs.end()
#define pb push_back
#define _1 first
#define _2 second
using namespace std;
typedef pair<int, int> P;
#define MAX_N (1<<18)
struct SegTree {
int seg[MAX_N*2-1];
SegTree() {
rep(i, MAX_N*2-1) seg[i] = -2;
}
void update(int k, int v) {
k += MAX_N-1;
seg[k] = v;
while (k) {
k = (k-1)/2;
seg[k] = max(seg[k*2+1], seg[k*2+2]);
}
}
int query(int a, int b, int k=0, int l=0, int r=MAX_N) {
if (b <= l || r <= a) return -2;
if (a <= l && r <= b) return seg[k];
return max(
query(a, b, k*2+1, l, (l+r)/2),
query(a, b, k*2+2, (l+r)/2, r)
);
}
};
struct BIT {
int n;
vector<int> xs;
BIT(int n) : n(n) {
xs.resize(n+1);
}
void add(int i, int v) {
for (int x=i+1; x<=n; x+=x&-x) xs[x] += v;
}
int sum(int i) {
int s = 0;
for (int x=i+1; x>0; x-=x&-x) s += xs[x];
return s;
}
};
SegTree seg;
vector<int> G[200000];
int R[200000];
int U[18][200000];
vector<int> tour;
int IN[200000], OUT[200000];
int A[100000];
void dfs(int x, int p, int r) {
U[0][x] = p;
R[x] = r;
IN[x] = tour.size();
tour.pb(x);
for (int t : G[x]) dfs(t, x, r+1);
OUT[x] = (int)tour.size()-1;
}
int f(int s) {
int x = s;
for (int k=17; k>=0; k--) {
if (U[k][x] != -1 && seg.query(IN[U[k][x]], OUT[U[k][x]]+1) <= 0) x = U[k][x];
}
return R[s]-R[x];
}
int GetBestPosition(int N, int C, int Rpower, int *power, int *S, int *E) {
BIT pre(N);
rep(i, N) pre.add(i, 1);
set<P> vs;
rep(i, N) vs.insert(P(i, i));
int V = N+C;
rep(i, C) {
int lo = -1, hi = N;
while (hi-lo > 1) {
int mid = (lo + hi) / 2;
if (pre.sum(mid) >= S[i]+1) hi = mid;
else lo = mid;
}
int lpos = hi;
auto it = vs.lower_bound(P(lpos, -1));
int v = N+i;
rep(_, E[i]-S[i]+1) {
assert(it != vs.end());
int t = it->_2;
pre.add(it->_1, -1);
G[v].pb(t);
it = vs.erase(it);
}
pre.add(lpos, 1);
vs.insert(P(lpos, v));
}
if (vs.size() > 1) {
for (P p : vs) G[V].pb(p._2);
V++;
}
dfs(V-1, -1, 0);
for (int k=1; k<18; k++) {
rep(x, V) {
if (U[k-1][x] == -1) U[k][x] = -1;
else U[k][x] = U[k-1][U[k-1][x]];
}
}
A[0] = 0;
rep(i, N-1) A[i+1] = (power[i] > Rpower) ? 1 : -1;
rep(i, N) seg.update(IN[i], A[i]);
P m = P(f(0), -0);
rep(i, N-1) {
swap(A[i], A[i+1]);
seg.update(IN[i], A[i]);
seg.update(IN[i+1], A[i+1]);
m = max(m, P(f(i+1), -(i+1)));
}
return -m._2;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |