#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
#define pii pair<int, int>
#define piii pair<pii, int>
#define vi vector<int>
#define vvi vector<vi>
#define vpii vector<pii>
#define vvpii vector<vpii>
#define f first
#define s second
#define oo 1000000001
#define eb emplace_back
#define pb push_back
#define mpr make_pair
#define size(v) (int)v.size()
#define ln '\n'
#define ull unsigned long long
#define ll long long
#define all(v) v.begin(), v.end()
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
#define pli pair<ll, int>
struct node {
vector<pli> pref, suff;
int ans;
node() {
ans = oo;
}
node(int x, int pos) {
pref.eb(mpr((1ll << x), pos));
suff.eb(mpr((1ll << x), pos));
ans = oo;
}
};
const int up = 1e5 + 5;
node tree[up << 2];
int arr[up];
int k;
node Merge(node &L, node &R) {
node res;
res.ans = min(L.ans, R.ans);
int posr = 0;
for (int i = size(L.suff) - 1; i >= 0 && posr < size(R.pref); --i) {
while (posr < size(R.pref) && ((L.suff[i]).f | (R.pref[posr]).f) != (1ll << k) - 1) {
++posr;
}
if (posr < size(R.pref)) {
res.ans = min(res.ans, (R.pref[posr]).s - (L.suff[i]).s + 1);
}
}
res.pref = L.pref;
for (pli p : R.pref) {
if ((((res.pref).back()).f | p.f) > ((res.pref).back()).f) {
(res.pref).eb(mpr(((res.pref).back()).f | p.f, p.s));
}
}
res.suff = R.suff;
for (pli p : L.suff) {
if ((((res.suff).back()).f | p.f) > ((res.suff).back()).f) {
(res.suff).eb(mpr(((res.suff).back()).f | p.f, p.s));
}
}
return res;
}
void build(int v, int l, int r) {
if (l == r) {
tree[v] = node(arr[l], l);
if (k == 1) tree[v].ans = 1;
} else {
int mid = (l + r) >> 1;
build(v * 2, l, mid);
build(v * 2 + 1, mid + 1, r);
tree[v] = Merge(tree[v * 2], tree[v * 2 + 1]);
}
}
void update(int v, int l, int r, int pos) {
if (l == r) {
tree[v] = node(arr[l], l);
if (k == 1) tree[v].ans = 1;
} else {
int mid = (l + r) >> 1;
if (pos <= mid) update(v * 2, l, mid, pos);
else update(v * 2 + 1, mid + 1, r, pos);
tree[v] = Merge(tree[v * 2], tree[v * 2 + 1]);
}
}
void solve() {
int n, m;
cin >> n >> k >> m;
for (int i = 1; i <= n; ++i) {
cin >> arr[i];
--arr[i];
}
build(1, 1, n);
int type, p, v;
while (m--) {
cin >> type;
if (type == 1) {
cin >> p >> v;
--v;
arr[p] = v;
update(1, 1, n, p);
} else {
int ans = tree[1].ans;
if (ans == oo) ans = -1;
cout << ans << ln;
}
}
}
int main() {
ios_base::sync_with_stdio(false);
int t = 1;
//cin >> t;
while (t--) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
23124 KB |
Output is correct |
2 |
Correct |
21 ms |
22868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
23508 KB |
Output is correct |
2 |
Correct |
27 ms |
23484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
24028 KB |
Output is correct |
2 |
Correct |
43 ms |
24008 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
338 ms |
29220 KB |
Output is correct |
2 |
Correct |
930 ms |
44672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
531 ms |
40472 KB |
Output is correct |
2 |
Correct |
1002 ms |
53888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
641 ms |
36660 KB |
Output is correct |
2 |
Correct |
1000 ms |
49856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
851 ms |
43600 KB |
Output is correct |
2 |
Correct |
1048 ms |
51188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
807 ms |
42524 KB |
Output is correct |
2 |
Correct |
1036 ms |
52812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1014 ms |
55660 KB |
Output is correct |
2 |
Correct |
1115 ms |
55580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1077 ms |
55644 KB |
Output is correct |
2 |
Correct |
1270 ms |
55412 KB |
Output is correct |