#include "paint.h"
#include <bits/stdc++.h>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N = 2e5 + 7;
const int Mod = 998244353;
const int szBL = 916;
const int INF = 1e9;
const int BASE = 137;
int n, m, K;
vector<int> A, C;
vector<vector<int>> B;
namespace sub4 {
const int N2 = 20000 + 2;
const int M2 = 2000 + 2;
int dp[N2][M2 * 2], f[N];
bool ok[N2];
struct Segment_Tree {
int Range;
int st[N << 2];
void init (int n) {
Range = n;
rep (i, 1, n << 2) {
st[i] = INF;
}
}
void update (int id, int l, int r, int pos, int val) {
if (l > pos || r < pos) return;
if (l == r) {
st[id] = val;
return;
}
int mid = l + r >> 1;
update (id << 1, l, mid, pos, val);
update (id << 1 | 1, mid + 1, r, pos, val);
st[id] = min(st[id << 1], st[id << 1 | 1]);
}
int get (int id, int l, int r, int u, int v) {
if (l > v || r < u) return INF;
if (l >= u && r <= v) return st[id];
int mid = l + r >> 1;
return min (get (id << 1, l, mid, u, v), get (id << 1 | 1, mid + 1, r, u, v));
}
void update (int pos, int val ) {
update (1, 1, Range, pos, val);
}
int get (int u, int v) {
return get (1, 1, Range, u, v);
}
}ST;
int solution() {
A.resize (2 * m);
B.resize (2 * m);
rep (i, 0, m - 1) sort (ALL(B[i]));
rep (i, m, 2 * m - 1) {
A[i] = A[i - m];
B[i] = B[i - m];
}
rep (i, 0, n - 1) {
rep (j, 0, 2 * m - 1) {
int pos = lower_bound(ALL(B[j]), C[i]) - B[j].begin();
if (pos == SZ(B[j]) || B[j][pos] != C[i]) dp[i][j] = 0;
else {
dp[i][j] = 1;
}
}
}
reb (i, n - 1, 0) {
reb (j, 2 * m - 1, 0) {
if (dp[i][j] != 0) dp[i][j] = dp[i + 1][j + 1] + 1;
if (dp[i][j] >= m) ok[i + 1] = 1;
}
}
ST.init(n);
f[m] = ok[1] ? 1 : INF;
ST.update (m, f[m]);
rep (i, 2, n - m + 1) {
if (ok[i]) {
f[i + m - 1] = ST.get (i - 1, i + m - 1) + 1;
ST.update (i + m - 1, f[i + m - 1]);
}
}
if (f[n] == 0) return -1;
else return f[n];
}
}
int minimumInstructions (int _n, int _m, int _K, vector<int> _C, vector<int> _A, vector<vector<int>> _B)
//void solution() {
// cin >> n >> m >> K;
n = _n;
m = _m;
K = _K;
C = _C;
A = _A;
B = _B;
// C.resize(n);
// rep (i, 0, n - 1) {
// cin >> C[i];
//
// }
// A.resize (m);
// B.resize (m);
// rep (i, 0, m - 1) {
// cin >> A[i];
// B[i].resize (A[i]);
// rep (j, 0, A[i] - 1) {
// cin >> B[i][j];
// }
// }
// if (n <= 20000 && m <= 2000)
return sub4 :: solution();
// cout << sub4 :: solution() <<"\n";
// else sub5 :: solution();
}
//#define file(name) freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout);
//int main () {
// ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//// file ("c");
// int num_Test = 1;
//// cin >> num_Test;
// while (num_Test--)
// solution();
//}
/*
8 3 5
3 3 1 3 4 4 2 2
3 0 1 2
2 2 3
2 3 4
*/
Compilation message
paint.cpp: In member function 'void sub4::Segment_Tree::update(int, int, int, int, int)':
paint.cpp:55:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | int mid = l + r >> 1;
| ~~^~~
paint.cpp: In member function 'int sub4::Segment_Tree::get(int, int, int, int, int)':
paint.cpp:63:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
63 | int mid = l + r >> 1;
| ~~^~~
paint.cpp: At global scope:
paint.cpp:116:5: error: expected initializer before 'n'
116 | n = _n;
| ^
paint.cpp:117:5: error: 'm' does not name a type
117 | m = _m;
| ^
paint.cpp:118:5: error: 'K' does not name a type
118 | K = _K;
| ^
paint.cpp:119:5: error: 'C' does not name a type
119 | C = _C;
| ^
paint.cpp:120:5: error: 'A' does not name a type
120 | A = _A;
| ^
paint.cpp:121:5: error: 'B' does not name a type
121 | B = _B;
| ^
paint.cpp:137:5: error: expected unqualified-id before 'return'
137 | return sub4 :: solution();
| ^~~~~~
paint.cpp:140:1: error: expected declaration before '}' token
140 | }
| ^