#include using namespace std; const int N = 2e5 + 10, M = 20, INF = 1e9; struct Node { int ans, pre[M], suf[M], sum, pre_p[M], suf_p[M], len; } tr[4 * N], Empty; int n, k, a[N], q; Node Merge(Node i, Node j) { Node ret = Empty; ret.ans = min(i.ans, j.ans); for (int p = 0; p < k; p++) { for (int q = 0; q < k; q++) { if (i.suf[p] >= 0 && j.pre[q] >= 0 && (i.suf[p] | j.pre[q]) == (1 << k) - 1) { ret.ans = min(ret.ans, i.suf_p[p] + j.pre_p[q]); //cout << p << ' ' << q << ' ' << i.pre_p[p] << ' ' << j.suf_p[q] << '\n'; } } } /*cout << i.ans << ' ' << j.ans << ' ' << i.sum << ' ' << j.sum << ' ' << ret.sum << '\n'; cout << "i : "; for (int p = 0; p < k; p++) cout << i.suf[p] << ' '; cout << "\nj : "; for (int p = 0; p < k; p++) cout << j.pre[p] << ' '; cout << '\n';*/ int t = 0; for (int p = 0; p < k; p++) { if (i.pre[p] >= 0) ret.pre[t++] = i.pre[p], ret.pre_p[t - 1] = i.pre_p[p]; else { for (int q = 0; q < k; q++) { if (j.pre[q] >= 0 && (i.sum | j.pre[q]) > ret.pre[t - 1]) { ret.pre[t++] = i.sum | j.pre[q]; ret.pre_p[t - 1] = i.len + j.pre_p[q]; } } } } t = 0; for (int p = 0; p < k; p++) { if (j.suf[p] >= 0) ret.suf[t++] = j.suf[p], ret.suf_p[t - 1] = j.suf_p[p]; else { for (int q = 0; q < k; q++) { if (i.suf[q] >= 0 && (j.sum | i.suf[q]) > ret.suf[t - 1]) { ret.suf[t++] = j.sum | i.suf[q]; ret.suf_p[t - 1] = j.len + i.suf_p[q]; } } } } ret.sum = i.sum | j.sum, ret.len = i.len + j.len; return ret; } Node build(int i, int l, int r) { if (l == r) { tr[i].ans = (a[l] == (1 << k) - 1 ? 1 : INF); tr[i].pre[0] = tr[i].suf[0] = tr[i].sum = a[l]; tr[i].pre_p[0] = tr[i].suf_p[0] = tr[i].len = 1; return tr[i]; } int mid = (l + r) >> 1; tr[i] = Merge(build(i * 2, l, mid), build(i * 2 + 1, mid + 1, r)); //cout << "build : " << l << ' ' << r << ' ' << tr[i].ans << ' ' << tr[i].sum << '\n'; return tr[i]; } void modify(int i, int l, int r, int pos, int x) { if (l == r) { tr[i] = Empty; tr[i].ans = (x == (1 << k) - 1 ? 1 : INF); tr[i].pre[0] = tr[i].suf[0] = tr[i].sum = x; tr[i].pre_p[0] = tr[i].suf_p[0] = tr[i].len = 1; return ; } int mid = (l + r) >> 1; pos <= mid ? modify(i * 2, l, mid, pos, x) : modify(i * 2 + 1, mid + 1, r, pos, x); tr[i] = Merge(tr[i * 2], tr[i * 2 + 1]); } Node query(int i, int l, int r, int ql, int qr) { if (qr < l || ql > r) return Empty; if (ql <= l && r <= qr) return tr[i]; int mid = (l + r) >> 1; return Merge(query(i * 2, l, mid, ql, qr), query(i * 2 + 1, mid + 1, r, ql, qr)); } int main() { ios::sync_with_stdio(0), cin.tie(0); // freopen("test.in", "r", stdin); // freopen("1.out", "w", stdout); Empty.ans = INF, Empty.sum = Empty.len = 0; fill(Empty.pre, Empty.pre + 20, -1); fill(Empty.suf, Empty.suf + 20, -1); fill(Empty.pre_p, Empty.pre_p + 20, INF); fill(Empty.suf_p, Empty.suf_p + 20, INF); cin >> n >> k; for (int i = 1; i <= n; i++) cin >> a[i]; fill(tr + 1, tr + 4 * n + 1, Empty); cin >> q, build(1, 1, n); while (q--) { int op; cin >> op; if (op == 1) { int i, x; cin >> i >> x; modify(1, 1, n, i, x); } else { int l, r; cin >> l >> r; Node t = query(1, 1, n, l, r); cout << (t.ans == INF ? -1 : t.ans) << '\n'; } } return 0; } /* 15 14 4099 8602 13653 2718 5587 10360 12181 9352 2451 14529 7600 16196 5078 15897 3144 7 1 1 828 2 6 9 2 8 14 2 1 12 2 5 6 2 7 8 1 7 10808 5 3 7 5 1 7 0 18 2 1 2 1 3 6 2 3 5 2 1 2 1 1 1 2 4 4 1 1 6 2 4 5 2 2 5 2 2 3 2 2 2 1 4 6 1 5 7 2 3 5 1 3 4 2 4 5 2 5 5 2 2 4 6 5 4 6 7 110 101 100 110 111 18 4 8 10 1 7 6 2 2 7 10 13 1 1 14 6 12 7 0 5 16 1 9 7 2 10 15 1 7 7 2 14 18 1 9 2 2 12 13 1 15 10 1 10 0 2 12 14 1 8 2 2 2 15 1 7 14 2 9 10 2 2 6 2 6 11 2 7 10 8 10 1 7 6 2 14 2 2 0 1 1 14 6 10 7 0 5 */