// #include "atcoder/convolution" #include "atcoder/dsu" #include "atcoder/fenwicktree" #include "atcoder/lazysegtree" #include "atcoder/math" #include "atcoder/maxflow" #include "atcoder/mincostflow" #include "atcoder/modint" #include "atcoder/scc" #include "atcoder/segtree" #include "atcoder/string" #include "atcoder/twosat" using namespace atcoder; #include using namespace std; // #include #pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #define rep(i, n) for (int i = 0; i < (int)(n); i++) #define rep2(i,a,b) for (int i = (int)(a); i < (int)(b); i++) #define all(v) v.begin(),v.end() #define inc(x,l,r) ((l)<=(x)&&(x)<(r)) #define Unique(x) sort(all(x)), x.erase(unique(all(x)), x.end()) #define pcnt __builtin_popcountll typedef long long ll; #define int ll using ld = long double; using vi = vector; using vs = vector; using P = pair; using vp = vector

; // using Bint = boost::multiprecision::cpp_int; template bool chmax(T1 &a, const T2 b) {if (a < b) {a = b; return true;} else return false; } template bool chmin(T1 &a, const T2 b) {if (a > b) {a = b; return true;} else return false; } template using priority_queue_greater = priority_queue, greater>; template istream &operator>>(istream& is,vector &v){for(T &in:v)is>>in;return is;} template ostream &operator<< (ostream &os, const pair &p){os << p.first <<" "<> (istream &is, modint1000000007 &m){ll in;is>>in;m=in;return is;} ostream &operator<< (ostream &os, const modint998244353 &m){os << m.val();return os;} istream &operator>> (istream &is, modint998244353 &m){ll in;is>>in;m=in;return is;} template void input(T&... a){(cin>> ... >> a);} #ifdef LOCAL template ostream &operator<<(ostream &os,const vector &v){os<<"\x1b[32m";rep(i,v.size())os< void print(T& a){cout << "\x1b[32m"<< a<< '\n' << "\x1b[0m";} template void print(const T&a, const Ts&... b){cout << "\x1b[32m" << a;(cout<<...<<(cout<<' ',b));cout<<'\n' << "\x1b[0m";} #else template ostream &operator<<(ostream &os,const vector &v){rep(i,v.size())os< void print(T& a){cout << a<< '\n';} template void print(const T&a, const Ts&... b){cout << a;(cout<<...<<(cout<<' ',b));cout<<'\n';} #endif #define VI(v,n) vi v(n); input(v) #define INT(...) int __VA_ARGS__; input(__VA_ARGS__) #define STR(...) string __VA_ARGS__; input(__VA_ARGS__) #define CHAR(...) char __VA_ARGS__; input(__VA_ARGS__) int sign(ll x){return x>0?1:x<0?-1:0;} ll ceil(ll x,ll y){assert(y!=0);if(sign(x)==sign(y))return (x+y-1)/y;return -((-x/y));} ll floor(ll x,ll y){assert(y!=0);if(sign(x)==sign(y))return x/y;if(y<0)x*=-1,y*=-1;return x/y-(x%y<0);} ll abs(ll x,ll y){return abs(x-y);} ll bit(int n){return 1ll< bit, sum; BitVector(){} BitVector(int len) : len(len), blocks((len+w-1)/w){ bit.assign(blocks, 0); sum.assign(blocks, 0); } void set(const int k, const int b = 1){ if(b) bit[k / w] |= 1U << rem(k); else bit[k / w] &= ~(1U << rem(k)); } void build(){ sum[0] = 0; for(int i = 1; i < blocks; i++){ sum[i] = sum[i - 1] + __builtin_popcount(bit[i - 1]); } } int operator[](const int k) const{ return bit[k / w] >> rem(k) & 1; } inline int rank(const int k) const{ return sum[k / w] + __builtin_popcount(bit[k / w] & ((1U << rem(k)) - 1)); } inline int rank(const int val, const int k) const{ return val ? rank(k) : k - rank(k); } #undef rem }; template struct WaveletMatrix { int len; BitVector matrix[maxlog]; int mid[maxlog]; vector sum[maxlog]; WaveletMatrix(){} WaveletMatrix(const vector &v) : len(v.size()){ build(v); } void build(vector v){ len = v.size(); vector l(len), r(len); fill(matrix, matrix + maxlog, BitVector(len + 1)); if(USE_SUM) fill(sum, sum + maxlog, vector(len + 1)); for(int level = maxlog - 1; level >= 0; level--){ int left = 0, right = 0; for(int i = 0; i < len; i++){ if(v[i] >> level & 1){ matrix[level].set(i); r[right++] = v[i]; }else{ l[left++] = v[i]; } } mid[level] = left; matrix[level].build(); v.swap(l); copy(r.begin(), r.begin() + right, v.begin() + left); if(USE_SUM){ sum[level][0] = 0; for(int i = 0; i < len; i++){ sum[level][i + 1] = sum[level][i] + v[i]; } } } } inline int succ(const int f, const int l, const int level) const{ return matrix[level].rank(f, l) + mid[level] * f; } //find v[k] T access(int k) const{ assert(0 <= k && k < len); T res = 0; for(int level = maxlog - 1; level >= 0; level--){ const bool f = matrix[level][k]; if(f) res |= T(1) << level; k = matrix[level].rank(f, k) + mid[level]*f; } return res; } T operator[](const int k) const{ return access(k); } // count i (0 <= i < r) && v[i] == x int rank(const T x, int r) const{ assert(0 <= r && r <= len); int l = 0; for(int level = maxlog - 1; level >= 0; level--){ l = succ(x >> level & 1, l, level); r = succ(x >> level & 1, r, level); } return r - l; } //k-th value v[l,r) T kth_smallest(int l, int r, int k) const{ assert(0 <= l && l <= r && r <= len); assert(0 <= k && k < r - l); T res = 0; for(int level = maxlog - 1; level >= 0; level--){ int cnt = matrix[level].rank(0, r) - matrix[level].rank(0, l); bool f = cnt <= k; if(f){ res |= T(1) << level; k -= cnt; } l = succ(f, l, level); r = succ(f, r, level); } return res; } T kth_largest(int l, int r, int k) const{ return kth_smallest(l, r, r-l-k-1); } // sum of [0,k)th smallest elements([l, r)) T kth_smallest_sum(int l, int r, int k) const{ assert(0 <= l && l <= r && r <= len); assert(0 <= k && k <= r - l); assert(USE_SUM); T res = 0; for(int level = maxlog - 1; level >= 0; level--){ const int l0 = matrix[level].rank(0, l); const int r0 = matrix[level].rank(0, r); if(k < r0 - l0){ l = l0, r = r0; }else{ k -= r0 - l0; res += sum[level][r0] - sum[level][l0]; l += mid[level] - l0; r += mid[level] - r0; } } if(k) res += sum[0][l + k] - sum[0][l]; return res; } // sum of [0,k)th largest elements([l, r)) T kth_largest_sum(int l, int r, int k) const{ assert(0 <= l && l <= r && r <= len); assert(0 <= k && k <= r - l); assert(USE_SUM); return kth_smallest_sum(l, r, r - l) - kth_smallest_sum(l, r, r - l - k); } // count i (l <= i < r), (v[i] < upper) int range_freq(int l, int r, const T upper) const{ int res = 0; for(int level = maxlog - 1; level >= 0; level--){ bool f = upper >> level & 1; if(f) res += matrix[level].rank(0, r) - matrix[level].rank(0, l); l = succ(f, l, level); r = succ(f, r, level); } return res; } // count i (l <= i < r), (lower <= v[i] < upper) int range_freq(int l, int r, const T lower, const T upper) const{ assert(0 <= l && l <= r && r <= len); return range_freq(l, r, upper) - range_freq(l, r, lower); } // max v[i] (l <= i < r), (v[i] < upper) T prev_value(int l, int r, const T upper) const{ assert(0 <= l && l <= r && r <= len); int cnt = range_freq(l, r, upper); return cnt == 0 ? -1 : kth_smallest(l, r, cnt - 1); } // min v[i] (l <= i < r), (lower <= v[i]) T next_value(int l, int r, const T lower) const{ assert(0 <= l && l <= r && r <= len); int cnt = range_freq(l, r, lower); return cnt == r - l ? -1 : kth_smallest(l, r, cnt); } }; struct RangeSetQuery{ WaveletMatrix mat; int n; RangeSetQuery(vector &a):n(a.size()){ vector next(n,n); map mp; for(int i=n-1;i>=0;i--){ if(mp.count(a[i]))next[i] = mp[a[i]]; mp[a[i]] = i; } mat.build(next); } int query(int l,int r){ assert(0<=l && l>a; sort(all(a)); RangeSetQuery s(a); int ans = INF; rep(l,n){ int r = l+k; if(r>n)break; chmin(ans,s.query(l,r)); } print(ans); return 0; }