#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5, kx=18;
int n, c[2*nx], rt, nd[nx], cur, pa[2*nx][kx], lvl[2*nx], in[2*nx], out[2*nx], t, res;
vector<int> d[2*nx];
void dfspa(int u, int p)
{
lvl[u]=lvl[p]+1;
pa[u][0]=p;
for (int i=1; i<kx; i++) pa[u][i]=pa[pa[u][i-1]][i-1];
for (auto v:d[u]) dfspa(v, u);
}
void dfs(int u)
{
if (u<=n) return;
c[u]=0;
for (auto v:d[u]) dfs(v), c[u]=c[u]|c[v];
}
struct segtree
{
int d[4*nx];
void update(int l, int r, int i, int idx, int vl)
{
if (idx<l||r<idx) return;
if (l==r) return d[i]+=vl, void();
int md=(l+r)/2;
update(l, md, 2*i, idx, vl);
update(md+1, r, 2*i+1, idx, vl);
d[i]=d[2*i]+d[2*i+1];
}
int query(int l, int r, int i, int key)
{
if (l==r) return l;
int md=(l+r)/2;
if (d[2*i]>=key) return query(l, md, 2*i, key);
else return query(md+1, r, 2*i+1, key-d[2*i]);
}
} s;
int GetBestPosition(int N, int C, int R, int *K, int *S, int *E) {
n=cur=N;
for (int i=1; i<=N; i++) nd[i]=i, s.update(1, N, 1, i, 1);
for (int i=0; i<C; i++)
{
cur++;
for (int j=S[i]+1; j<=E[i]+1; j++)
{
int idx=s.query(1, N, 1, S[i]+1);
d[cur].push_back(nd[idx]);
if (j==E[i]+1) nd[idx]=cur;
else s.update(1, N, 1, idx, -1);
}
}
for (int i=2; i<=N; i++) c[i]=K[i-2]>R;
rt=cur;
dfspa(rt, rt);
pair<int, int> mx={-1, -1};
for (int i=1; i<=N; i++)
{
if (i!=1) swap(c[i], c[i-1]);
dfs(rt);
if (!c[rt]) mx=max(mx, {lvl[i]-1, -i});
else
{
int tmp=i, cnt=0;
while (!c[pa[tmp][0]]) cnt++, tmp=pa[tmp][0];
mx=max(mx, {cnt, -i});
}
}
return -mx.second-1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
4952 KB |
Output is correct |
2 |
Correct |
2 ms |
4956 KB |
Output is correct |
3 |
Correct |
4 ms |
5212 KB |
Output is correct |
4 |
Correct |
4 ms |
5212 KB |
Output is correct |
5 |
Correct |
3 ms |
5212 KB |
Output is correct |
6 |
Correct |
5 ms |
5292 KB |
Output is correct |
7 |
Correct |
4 ms |
5080 KB |
Output is correct |
8 |
Correct |
4 ms |
5292 KB |
Output is correct |
9 |
Correct |
2 ms |
5212 KB |
Output is correct |
10 |
Correct |
3 ms |
5088 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
5212 KB |
Output is correct |
2 |
Correct |
205 ms |
6492 KB |
Output is correct |
3 |
Correct |
35 ms |
5724 KB |
Output is correct |
4 |
Correct |
208 ms |
6488 KB |
Output is correct |
5 |
Correct |
173 ms |
6236 KB |
Output is correct |
6 |
Correct |
122 ms |
5980 KB |
Output is correct |
7 |
Correct |
210 ms |
6236 KB |
Output is correct |
8 |
Correct |
206 ms |
6232 KB |
Output is correct |
9 |
Correct |
30 ms |
5720 KB |
Output is correct |
10 |
Correct |
265 ms |
6236 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1028 ms |
14168 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |