This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
template <class X, class Y> bool minimize(X &a, Y b) {
if (a > b) return a = b, true;
return false;
}
template <class X, class Y> bool maximize(X &a, Y b) {
if (a < b) return a = b, true;
return false;
}
long long target;
const int offset = (1 << 17) + 7;
struct segment_tree {
struct node {
pair<long long, int> pref[50], suff[50];
int best;
int len;
int p_sz, s_sz;
node() {
len = p_sz = s_sz = 0;
best = (int) 1e9;
}
node(long long v) {
len = p_sz = s_sz = 1;
best = (v == target ? 1 : (int) 1e9);
pref[0] = suff[0] = mp(v, 1);
}
void add_pref(pair<long long, int> x) {
pref[p_sz ++] = x;
}
void add_suff(pair<long long, int> x) {
suff[s_sz ++] = x;
}
#define new_bit(s1, s2) ((s1 | s2) != s1)
friend node operator + (node &a, node &b) {
node res;
for (int i = 0; i < a.p_sz; i ++) {
res.add_pref(a.pref[i]);
}
for (int i = 0; i < b.s_sz; i ++) {
res.add_suff(b.suff[i]);
}
long long mask = a.pref[a.p_sz - 1].fi;
for (int i = 0; i < b.p_sz; i ++) {
if (!new_bit(mask, b.pref[i].fi)) {
continue;
}
mask |= b.pref[i].fi;
res.add_pref(mp(mask, a.len + b.pref[i].se));
}
mask = b.suff[b.s_sz - 1].fi;
for (int i = 0; i < a.s_sz; i ++) {
if (!new_bit(mask, a.suff[i].fi)) {
continue;
}
mask |= a.suff[i].fi;
res.add_suff(mp(mask, b.len + a.suff[i].se));
}
for (int i = a.s_sz - 1, j = 0; ~i; i --) {
while (j < b.p_sz && (a.suff[i].fi | b.pref[j].fi) != target) {
j ++;
}
if (j == b.p_sz || (a.suff[i].fi | b.pref[j].fi) != target) {
break;
}
minimize(res.best, a.suff[i].se + b.pref[j].se);
}
minimize(res.best, min(a.best, b.best));
res.len = a.len + b.len;
return res;
}
#undef new_bit
};
node t[offset << 1];
void modify(int p, long long v) {
p += offset;
t[p] = node(v);
for (int i = p / 2; i; i >>= 1) {
t[i] = t[i << 1] + t[i << 1 | 1];
}
}
int get_ans() {
return t[1].best == (int) 1e9 ? -1 : t[1].best;
}
} st;
const int N = 1e5 + 7;
int n, m, k, q;
int a[N];
signed main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k >> q;
for (int i = 1; i <= n; i ++) {
cin >> a[i];
a[i] --;
}
target = (1LL << k) - 1;
for (int i = 1; i <= n; i ++) {
st.modify(i, 1LL << a[i]);
}
while (q --) {
int t; cin >> t;
if (t == 2) {
cout << st.get_ans() << '\n';
} else {
int p, v; cin >> p >> v;
v --;
st.modify(p, 1LL << v);
}
}
return 0;
}
# | 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... |