#include <iostream>
#include <bits/stdc++.h>
#define INF 1e9
using namespace std;
const int MAXN = 1e5;
int n,k,q;
int v[MAXN+2];
struct node{
int len;
pair<long long,int> suff[52],pref[52];
int ans;
};
node seg[3*MAXN + 2];
bool check(int a,int b){
return (a&b) == a;
}
void upd(int a,int b){
a+=(1<<17);
seg[a].len = 1;
seg[a].pref[0] = make_pair(1LL<<b,a-(1<<17));
seg[a].suff[0] = seg[a].pref[0];
seg[a].ans = INF;
for(a/=2;a;a>>=1){
int lp,r1;
lp = 0;
//setting prefixes to child prefixes
for(int i=0;i<seg[a*2].len;i++){
seg[a].pref[lp++] = seg[a*2].pref[i];
}
for(int i=0;i<seg[a*2+1].len;i++){
if(lp==0||!check(seg[a*2+1].pref[i].first,seg[a].pref[lp-1].first)){
seg[a].pref[lp] =seg[a*2 + 1].pref[i];
if(lp>0){
seg[a].pref[lp].first|=seg[a].pref[lp-1].first;
}
lp++;
}
}
//setting suffixes to child suffixes
r1 = 0;
for(int i=0;i<seg[a*2+1].len;i++){
seg[a].suff[r1++] = seg[a*2 + 1].suff[i];
}
for(int i=0;i<seg[a*2].len;i++){
if(r1 == 0||!check(seg[a*2].suff[i].first,seg[a].suff[r1-1].first)){
seg[a].suff[r1] = seg[a*2].suff[i];
if(r1){
seg[a].suff[r1].first|=seg[a].suff[r1-1].first;
}
r1++;
}
}
seg[a].len = lp;
seg[a].ans = INF;
int prefpos = 0;
//set the whole suffix to be the answer at the start, then decrease the suffix and accordingly increase the prefix
for(int suffpos = seg[a*2].len-1;suffpos>=0;suffpos--){
while(prefpos<seg[a*2+1].len && (seg[a*2].suff[suffpos].first|seg[a*2+1].pref[prefpos].first)!=(1LL<<k)-1){
prefpos++;
}
if(prefpos<seg[a*2 + 1].len){
long long currmask = (seg[a*2].suff[suffpos].first|seg[a*2+1].pref[prefpos].first);
if(currmask==(1LL<<k)-1){
seg[a].ans = min(seg[a].ans,seg[a*2+1].pref[prefpos].second -seg[a*2].suff[suffpos].second+1);
}
}
seg[a].ans = min(seg[a].ans,min(seg[2*a].ans,seg[a*2+1].ans));
}
}
}
int main() {
cin>>n>>k>>q;
for(int i=0;i<=280000;i++){
seg[i].ans = INF;
}
for(int i=0;i<n;i++){
cin>>v[i];
upd(i,v[i]-1);
}
while(q--){
int type;
cin>>type;
if(type == 2){
if(seg[1].ans == INF){
cout<<-1<<endl;
}else{
cout<<seg[1].ans<<endl;
}
}else{
int a,b;
cin>>a>>b;
a--;
b--;
upd(a,b);
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
386 ms |
493512 KB |
Output is correct |
2 |
Correct |
307 ms |
493428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
347 ms |
493508 KB |
Output is correct |
2 |
Correct |
328 ms |
493660 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
335 ms |
493432 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
624 ms |
493560 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
846 ms |
493988 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
955 ms |
493848 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1128 ms |
494072 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1101 ms |
494000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1376 ms |
494200 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1361 ms |
494056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |