#include "september.h"
#include <bits/stdc++.h>
using namespace std;
const int nx=1e5+5;
int in[nx], out[nx], t, n, st, rv[nx], cnt;
vector<int> d[nx], k;
struct segtree
{
int d[4*nx];
void build(int l, int r, int i)
{
d[i]=0;
if (l==r) return;
int md=(l+r)/2;
build(l, md, 2*i);
build(md+1, r, 2*i+1);
}
void update(int l, int r, int i, int idx, int vl)
{
if (idx<l||r<idx) return;
if (l==r) return d[i]=max(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]=max(d[2*i], d[2*i+1]);
}
int query(int l, int r, int i, int ql, int qr)
{
if (qr<l||r<ql) return 0;
if (ql<=l&&r<=qr) return d[i];
int md=(l+r)/2;
return max(query(l, md, 2*i, ql, qr), query(md+1, r, 2*i+1, ql, qr));
}
} s, mxseg;
struct mnsegtree
{
int d[4*nx];
void build(int l, int r, int i)
{
d[i]=1e9;
if (l==r) return;
int md=(l+r)/2;
build(l, md, 2*i);
build(md+1, r, 2*i+1);
}
void update(int l, int r, int i, int idx, int vl)
{
if (idx<l||r<idx) return;
if (l==r) return d[i]=min(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]=min(d[2*i], d[2*i+1]);
}
int query(int l, int r, int i, int ql, int qr)
{
if (qr<l||r<ql) return 0;
if (ql<=l&&r<=qr) return d[i];
int md=(l+r)/2;
return min(query(l, md, 2*i, ql, qr), query(md+1, r, 2*i+1, ql, qr));
}
} mnseg;
void dfs(int u, int p)
{
in[u]=++t;
//cout<<"update "<<u<<' '<<t<<' '<<rv[u]<<'\n';
if (u!=0) s.update(1, n, 1, t, rv[u]);
for (auto v:d[u]) dfs(v, u);
out[u]=t;
}
int solve(int N, int M, std::vector<int> F, std::vector<std::vector<int>> S) {
for (int i=0; i<N-1; i++) rv[S[0][i]]=i;
n=N, t=0, st=0, cnt=0;
int res=0;
for (int i=0; i<N; i++) in[i]=out[i]=0, d[i].clear();
for (int i=1; i<N; i++) d[F[i]].push_back(i);
s.build(1, N, 1);
mxseg.build(0, N-2, 1);
mnseg.build(0, N-2, 1);
for (int i=0; i<N-1; i++) mxseg.update(0, N-2, 1, i, i);
for (int i=0; i<N-1; i++) mnseg.update(0, N-2, 1, i, i);
for (int i=1; i<M; i++)
{
for (int j=0; j<N-1; j++)
{
mxseg.update(0, N-2, 1, rv[S[i][j]], j);
mnseg.update(0, N-2, 1, rv[S[i][j]], j);
}
}
dfs(0, 0);
while (st<N-1)
{
int mx=0;
for (int lst=st; lst<N-1; lst++)
{
mx=max(mx, s.query(1, n, 1, in[S[0][lst]], out[S[0][lst]]));
//cout<<"debug "<<st<<' '<<lst<<' '<<mx<<' '<<S[0][lst]<<'\n';
if (mxseg.query(0, N-2, 1, st, lst)>lst) continue;
if (mx==lst)
{
res++;
st=lst+1;
break;
}
}
}
return res;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7256 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7260 KB |
Output is correct |
5 |
Correct |
1 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7256 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7260 KB |
Output is correct |
5 |
Correct |
1 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7256 KB |
Output is correct |
9 |
Correct |
1 ms |
7260 KB |
Output is correct |
10 |
Correct |
1 ms |
7512 KB |
Output is correct |
11 |
Correct |
1 ms |
7260 KB |
Output is correct |
12 |
Correct |
1 ms |
7260 KB |
Output is correct |
13 |
Correct |
1 ms |
7260 KB |
Output is correct |
14 |
Correct |
1 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7256 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7260 KB |
Output is correct |
5 |
Correct |
1 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
2 ms |
7260 KB |
Output is correct |
13 |
Correct |
2 ms |
7260 KB |
Output is correct |
14 |
Correct |
2 ms |
7260 KB |
Output is correct |
15 |
Correct |
2 ms |
7256 KB |
Output is correct |
16 |
Correct |
2 ms |
7260 KB |
Output is correct |
17 |
Correct |
2 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7256 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7260 KB |
Output is correct |
5 |
Correct |
1 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7256 KB |
Output is correct |
9 |
Correct |
1 ms |
7260 KB |
Output is correct |
10 |
Correct |
1 ms |
7512 KB |
Output is correct |
11 |
Correct |
1 ms |
7260 KB |
Output is correct |
12 |
Correct |
1 ms |
7260 KB |
Output is correct |
13 |
Correct |
1 ms |
7260 KB |
Output is correct |
14 |
Correct |
1 ms |
7260 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7516 KB |
Output is correct |
17 |
Correct |
2 ms |
7516 KB |
Output is correct |
18 |
Correct |
2 ms |
7260 KB |
Output is correct |
19 |
Correct |
2 ms |
7260 KB |
Output is correct |
20 |
Correct |
2 ms |
7260 KB |
Output is correct |
21 |
Correct |
2 ms |
7260 KB |
Output is correct |
22 |
Correct |
2 ms |
7256 KB |
Output is correct |
23 |
Correct |
2 ms |
7260 KB |
Output is correct |
24 |
Correct |
2 ms |
7260 KB |
Output is correct |
25 |
Correct |
3 ms |
7516 KB |
Output is correct |
26 |
Correct |
3 ms |
7516 KB |
Output is correct |
27 |
Correct |
2 ms |
7516 KB |
Output is correct |
28 |
Correct |
3 ms |
7260 KB |
Output is correct |
29 |
Correct |
3 ms |
7260 KB |
Output is correct |
30 |
Correct |
3 ms |
7260 KB |
Output is correct |
31 |
Correct |
3 ms |
7260 KB |
Output is correct |
32 |
Correct |
3 ms |
7348 KB |
Output is correct |
33 |
Correct |
3 ms |
7256 KB |
Output is correct |
34 |
Correct |
3 ms |
7260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
311 ms |
22736 KB |
Output is correct |
5 |
Correct |
315 ms |
22728 KB |
Output is correct |
6 |
Correct |
313 ms |
22732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7256 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7260 KB |
Output is correct |
5 |
Correct |
1 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7516 KB |
Output is correct |
9 |
Correct |
3 ms |
7516 KB |
Output is correct |
10 |
Correct |
2 ms |
7516 KB |
Output is correct |
11 |
Correct |
2 ms |
7260 KB |
Output is correct |
12 |
Correct |
2 ms |
7260 KB |
Output is correct |
13 |
Correct |
2 ms |
7260 KB |
Output is correct |
14 |
Correct |
2 ms |
7260 KB |
Output is correct |
15 |
Correct |
2 ms |
7256 KB |
Output is correct |
16 |
Correct |
2 ms |
7260 KB |
Output is correct |
17 |
Correct |
2 ms |
7260 KB |
Output is correct |
18 |
Correct |
311 ms |
22736 KB |
Output is correct |
19 |
Correct |
315 ms |
22728 KB |
Output is correct |
20 |
Correct |
313 ms |
22732 KB |
Output is correct |
21 |
Correct |
388 ms |
12976 KB |
Output is correct |
22 |
Correct |
396 ms |
12972 KB |
Output is correct |
23 |
Correct |
397 ms |
14292 KB |
Output is correct |
24 |
Correct |
371 ms |
14224 KB |
Output is correct |
25 |
Correct |
372 ms |
12968 KB |
Output is correct |
26 |
Correct |
377 ms |
12972 KB |
Output is correct |
27 |
Correct |
364 ms |
14252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
7516 KB |
Output is correct |
2 |
Correct |
3 ms |
7516 KB |
Output is correct |
3 |
Correct |
2 ms |
7516 KB |
Output is correct |
4 |
Correct |
3 ms |
7516 KB |
Output is correct |
5 |
Correct |
3 ms |
7516 KB |
Output is correct |
6 |
Correct |
2 ms |
7516 KB |
Output is correct |
7 |
Correct |
311 ms |
22736 KB |
Output is correct |
8 |
Correct |
315 ms |
22728 KB |
Output is correct |
9 |
Correct |
313 ms |
22732 KB |
Output is correct |
10 |
Correct |
180 ms |
25752 KB |
Output is correct |
11 |
Correct |
180 ms |
25800 KB |
Output is correct |
12 |
Correct |
181 ms |
25820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
7256 KB |
Output is correct |
2 |
Correct |
1 ms |
7260 KB |
Output is correct |
3 |
Correct |
1 ms |
7260 KB |
Output is correct |
4 |
Correct |
1 ms |
7260 KB |
Output is correct |
5 |
Correct |
1 ms |
7260 KB |
Output is correct |
6 |
Correct |
1 ms |
7260 KB |
Output is correct |
7 |
Correct |
1 ms |
7260 KB |
Output is correct |
8 |
Correct |
2 ms |
7256 KB |
Output is correct |
9 |
Correct |
1 ms |
7260 KB |
Output is correct |
10 |
Correct |
1 ms |
7512 KB |
Output is correct |
11 |
Correct |
1 ms |
7260 KB |
Output is correct |
12 |
Correct |
1 ms |
7260 KB |
Output is correct |
13 |
Correct |
1 ms |
7260 KB |
Output is correct |
14 |
Correct |
1 ms |
7260 KB |
Output is correct |
15 |
Correct |
2 ms |
7516 KB |
Output is correct |
16 |
Correct |
3 ms |
7516 KB |
Output is correct |
17 |
Correct |
2 ms |
7516 KB |
Output is correct |
18 |
Correct |
2 ms |
7260 KB |
Output is correct |
19 |
Correct |
2 ms |
7260 KB |
Output is correct |
20 |
Correct |
2 ms |
7260 KB |
Output is correct |
21 |
Correct |
2 ms |
7260 KB |
Output is correct |
22 |
Correct |
2 ms |
7256 KB |
Output is correct |
23 |
Correct |
2 ms |
7260 KB |
Output is correct |
24 |
Correct |
2 ms |
7260 KB |
Output is correct |
25 |
Correct |
3 ms |
7516 KB |
Output is correct |
26 |
Correct |
3 ms |
7516 KB |
Output is correct |
27 |
Correct |
2 ms |
7516 KB |
Output is correct |
28 |
Correct |
3 ms |
7260 KB |
Output is correct |
29 |
Correct |
3 ms |
7260 KB |
Output is correct |
30 |
Correct |
3 ms |
7260 KB |
Output is correct |
31 |
Correct |
3 ms |
7260 KB |
Output is correct |
32 |
Correct |
3 ms |
7348 KB |
Output is correct |
33 |
Correct |
3 ms |
7256 KB |
Output is correct |
34 |
Correct |
3 ms |
7260 KB |
Output is correct |
35 |
Correct |
311 ms |
22736 KB |
Output is correct |
36 |
Correct |
315 ms |
22728 KB |
Output is correct |
37 |
Correct |
313 ms |
22732 KB |
Output is correct |
38 |
Correct |
388 ms |
12976 KB |
Output is correct |
39 |
Correct |
396 ms |
12972 KB |
Output is correct |
40 |
Correct |
397 ms |
14292 KB |
Output is correct |
41 |
Correct |
371 ms |
14224 KB |
Output is correct |
42 |
Correct |
372 ms |
12968 KB |
Output is correct |
43 |
Correct |
377 ms |
12972 KB |
Output is correct |
44 |
Correct |
364 ms |
14252 KB |
Output is correct |
45 |
Correct |
180 ms |
25752 KB |
Output is correct |
46 |
Correct |
180 ms |
25800 KB |
Output is correct |
47 |
Correct |
181 ms |
25820 KB |
Output is correct |
48 |
Correct |
194 ms |
15000 KB |
Output is correct |
49 |
Correct |
187 ms |
14796 KB |
Output is correct |
50 |
Correct |
188 ms |
16328 KB |
Output is correct |
51 |
Correct |
185 ms |
16328 KB |
Output is correct |
52 |
Correct |
194 ms |
14792 KB |
Output is correct |
53 |
Correct |
186 ms |
14792 KB |
Output is correct |
54 |
Correct |
185 ms |
16296 KB |
Output is correct |