Language: C++

#ifndef __UNION_FIND_H__0004__
#define __UNION_FIND_H__0004__

class UnionFindTree {
	int whole_size_m;
	int* parent_p_m;	// 根である場合は自分自身の添え字を入れる
	int* rank_p_m;
	int* size_p_m;		// 集合のサイズ(根に大きさを書く)

	bool Is_valid_index(int index);
	int Root_of_rec(int index);
public:
	UnionFindTree(int whole_size);
	~UnionFindTree();

	int Root_of(int index);
	void Unite(int index_l, int index_r);
	bool Are_together(int index_l, int index_r);
	int Size_of_set(int index);
};

#include <cassert>
#include <functional>
#include <algorithm>

template <typename T>
class SegmentTree {
	using FuncType = std::function<T(const T&, const T&)>;

private:
	T* val_p_m;
	const T init_val_m;
	const int size_m;
	const int rank_m;

	const FuncType func_m;

	T Query_rec(int range_left, int range_right, int node_index, int node_range_left, int node_range_right);
	bool Is_valid_index(int index);
public:
	SegmentTree(int size, const T& init_val, const FuncType& func);

	void Update(int pos, const T& val);
	T Query(int range_left, int range_right);
};

template<typename T>
T SegmentTree<T>::Query_rec(int range_left, int range_right, int node_index, int node_range_left, int node_range_right) {
	if (node_range_right <= range_left || range_right <= node_range_left) return init_val_m;
	if (range_left <= node_range_left && node_range_right <= range_right) return val_p_m[node_index];

	int node_range_mid = (node_range_left + node_range_right) / 2;
	const T val_left = Query_rec(range_left, range_right, node_index * 2, node_range_left, node_range_mid);
	const T val_right = Query_rec(range_left, range_right, node_index * 2 + 1, node_range_mid, node_range_right);
	return func_m(val_left, val_right);
}

template<typename T>
inline bool SegmentTree<T>::Is_valid_index(int index) {
	return index >= 0 && index < size_m;
}

template<typename T>
SegmentTree<T>::SegmentTree(int size, const T& init_val, const FuncType& func)
	: init_val_m(init_val), size_m(size), rank_m((int)std::log2(size) + 1), func_m(func) {

	val_p_m = new T[1 << rank_m];
	fill(val_p_m + (1 << (rank_m - 1)), val_p_m + (1 << rank_m), init_val_m);

	for (int i = (1 << (rank_m - 1)) - 1; i >= 1; --i) {
		val_p_m[i] = func_m(val_p_m[i * 2], val_p_m[i * 2 + 1]);
	}
}

template<typename T>
void SegmentTree<T>::Update(int pos, const T& val) {
	assert(Is_valid_index(pos));

	int i = pos + (1 << (rank_m - 1));
	val_p_m[i] = val;
	while (i > 1) {
		i /= 2;
		val_p_m[i] = func_m(val_p_m[i * 2], val_p_m[i * 2 + 1]);
	}
}

template<typename T>
T SegmentTree<T>::Query(int range_left, int range_right) {
	assert(Is_valid_index(range_left));
	assert(Is_valid_index(range_right - 1));

	return Query_rec(range_left, range_right, 1, 0, 1 << (rank_m - 1));
}

template<typename T>
class Max {
public:
	const T& operator()(const T& a, const T& b) const {
		return max<T>(a, b);
	}
};

template<typename T>
class Min {
public:
	const T& operator()(const T& a, const T& b) const {
		return min<T>(a, b);
	}
};

#endif

//#include "IntMod.h"
//typedef IntMod<1000000007> MInt;

#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
#include <list>
#include <utility>
#include <algorithm>
#include <functional>
#include <cmath>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <iomanip>
#include <sstream>
#include <numeric>
#include <array>
#include <bitset>

using namespace std;
#define REP(i,a,n) for(int i = (a); i < (int)(n); ++i)
#define REPM(i,n,a) for(int i = ((n) - 1); i >= (a); --i)
#define EPS 0.0001
#define INF 0x3FFFFFFF
#define INFLL 0x3FFFFFFF3FFFFFFF
#define INFD 1.0e+308
#define FLOAT setprecision(16)
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<LL, LL> PP;

template <class T, class U>
istream& operator>>(istream& ist, pair<T, U>& right) { return ist >> right.first >> right.second; }
template <class T, class U>
ostream& operator<<(ostream& ost, pair<T, U>& right) { return ost << right.first << ' ' << right.second; }
template <class T, class TCompatible, size_t N>
void Fill(T(&dest)[N], const TCompatible& val) { fill(begin(dest), end(dest), val); }
template <class T, class TCompatible, size_t M, size_t N>
void Fill(T(&dest)[M][N], const TCompatible& val) { for (int i = 0; i < M; ++i) Fill(dest[i], val); }
// all_of

#if 1
#include <unordered_set>
#include <unordered_map>
template<typename T>
using PriorityQ = priority_queue<T, vector<T>, greater<T> >;	// コスト小を優先
#endif

int N, M, Q;
int A[100000];
vector<PP> seq;
int idxs[100000];
set<int> Set;

int main() {
	cin >> N >> M >> Q;
	REP(i, 0, Q) {
		cin >> A[i];
		--A[i];
	}
	Fill(idxs, -1);

	SegmentTree<int> S(M, INF, Min<int>());
	REPM(i, Q, 0) {
		int idx = idxs[A[i]];
		if (idx == -1) {
			seq.push_back(PP(A[i], 1));
			idxs[A[i]] = seq.size() - 1;
			S.Update(idxs[A[i]], 1);
		} else {
			int mn = idx == 0 ? INF : S.Query(0, idx);
			if (seq[idx].second != mn && seq[idx].second != N) {
				++seq[idx].second;
				S.Update(idx, seq[idx].second);
			}
		}
	}

	REP(i, 0, M) {
		Set.insert(i);
	}
	bool ok = true;
	for (PP p : seq) {
		if (p.second == N) {
			Set.erase(p.first);
		} else {
			if (*Set.begin() != p.first) {
				ok = false;
				break;
			}
			Set.erase(p.first);
		}
	}
	cout << (ok ? "Yes" : "No") << endl;
	return 0;
}



Compiler output:
a.cc: In constructor 'SegmentTree<T>::SegmentTree(int, const T&, const FuncType&)':
a.cc:65:64: error: 'log2' is not a member of 'std'
   65 |         : init_val_m(init_val), size_m(size), rank_m((int)std::log2(size) + 1), func_m(func) {
      |                                                                ^~~~
a.cc: In member function 'const T& Max<T>::operator()(const T&, const T&) const':
a.cc:99:24: error: 'max' was not declared in this scope; did you mean 'std::max'?
   99 |                 return max<T>(a, b);
      |                        ^~~
      |                        std::max
In file included from /usr/include/c++/14/algorithm:61,
                 from a.cc:24:
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: 'std::max' declared here
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
a.cc:99:29: error: expected primary-expression before '>' token
   99 |                 return max<T>(a, b);
      |                             ^
a.cc: In member function 'const T& Min<T>::operator()(const T&, const T&) const':
a.cc:107:24: error: 'min' was not declared in this scope; did you mean 'std::min'?
  107 |                 return min<T>(a, b);
      |                        ^~~
      |                        std::min
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: 'std::min' declared here
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
a.cc:107:29: error: expected primary-expression before '>' token
  107 |                 return min<T>(a, b);
      |                             ^
a.cc: In instantiation of 'SegmentTree<T>::SegmentTree(int, const T&, const FuncType&) [with T = int; FuncType = std::function<int(const int&, const int&)>]':
a.cc:180:39:   required from here
  180 |         SegmentTree<int> S(M, INF, Min<int>());
      |                                              ^
a.cc:68:13: error: 'fill' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation
   68 |         fill(val_p_m + (1 << (rank_m - 1)), val_p_m + (1 << rank_m), init_val_m);
      |         ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:86:
/usr/include/c++/14/pstl/glue_algorithm_defs.h:191:1: note: 'template<class _ExecutionPolicy, class _ForwardIterator, class _Tp> __pstl::__internal::__enable_if_execution_policy<_ExecutionPolicy, void> std::fill(_ExecutionPolicy&&, _ForwardIterator, _ForwardIterator, const _Tp&)' declared here, later in the translation unit
  191 | fill(_ExecutionPolicy&& __exec, _ForwardIterator __first, _ForwardIterator __last, const _Tp& __value);
      | ^~~~