This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/** - dwuy -
/> フ
| _ _|
/`ミ _x ノ
/ |
/ ヽ ?
/ ̄| | | |
| ( ̄ヽ__ヽ_)_)
\二つ
**/
#include "september.h"
#include <bits/stdc++.h>
#define fastIO ios_base::sync_with_stdio(false); cin.tie(NULL)
#define file(a) freopen(a".inp","r",stdin); freopen(a".out", "w",stdout)
#define fi first
#define se second
#define endl "\n"
#define len(s) (int)((s).size())
#define MASK(k)(1LL<<(k))
#define TASK "test"
using namespace std;
typedef tuple<int, int, int> tpiii;
typedef pair<double, double> pdd;
typedef pair<int, int> pii;
typedef long long ll;
const long long OO = 1e18;
const int MOD = 1e9 + 7;
const int INF = 1e9;
struct SMT{
int n;
vector<int> tree;
SMT(int n=0){
this->n = n;
this->tree.assign(n<<2|3, 0);
}
void update(int pos, int val){
int id = 1;
for(int lo=1, hi=n; lo<hi;){
int mid = (lo + hi)>>1;
if(pos <= mid) hi = mid, id = id<<1;
else lo = mid + 1, id = id<<1 | 1;
}
tree[id] = max(tree[id], val);
for(id>>=1; id; id>>=1) tree[id] = max(tree[id<<1], tree[id<<1|1]);
}
int get(int l, int r, int id, const int &u, const int &v){
if(l>v || r<u) return 0;
if(l>=u && r<=v) return tree[id];
int mid = (l + r)>>1;
return max(get(l, mid, id<<1, u, v), get(mid + 1, r, id<<1|1, u, v));
}
int get(int l, int r){
return get(1, n, 1, l, r);
}
};
int solve(int N, int M, std::vector<int> F, std::vector<std::vector<int>> S) {
int ID = 0;
vector<int> num(N + 5, 0);
vector<int> clo(N + 5, 0);
vector<vector<int>> mx(M + 1, vector<int>(N + 5, 0));
vector<vector<int>> G(N + 5, vector<int>());
for(int i=1; i<N; i++) G[F[i]].push_back(i);
function<void(int)> dfs = [&](int u){
num[u] = ++ID;
for(int v: G[u]){
dfs(v);
}
clo[u] = ID;
}; dfs(0);
for(int t=0; t<M; t++){
SMT smt(N);
for(int i=N-2; i>=0; i--){
mx[t][i] = smt.get(num[S[t][i]], clo[S[t][i]]);
smt.update(num[S[t][i]], i);
}
}
int res = 0;
int cmx = 0;
int cur = 0;
vector<int> cnt(N + 5, 0);
for(int i=0; i<N-1; i++){
for(int j=0; j<M; j++){
cmx = max(cmx, mx[j][i]);
cnt[S[j][i]]++;
if(cnt[S[j][i]] == M) cur++;
}
if(cmx <= i && cur == i + 1) res++;
}
return res;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |