답안 #605329

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
605329 2022-07-25T15:39:29 Z inksamurai Nekameleoni (COCI15_nekameleoni) C++17
56 / 140
3000 ms 106540 KB
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3Pxrs5V ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
//e

const int inf=200011;

const int _n=340002;
const int _k=51;

struct node{
	int res;
	int l[_k],r[_k];
};

int id[_n];
node seg[_n];

int k; // initial k

void build(int node,int l,int r){
	seg[node].res=inf;
	rep(i,k){
		seg[node].l[i]=-1;
		seg[node].r[i]=-1;
	}
	if(l==r-1){
		id[l]=node;
		return;
	}
	int m=(l+r)>>1;
	build(node*2,l,m);
	build(node*2+1,m,r);
}

void update_node(int &node,int &val,int t=0){
	if(val==-1){
		return;
	}
	if(node==-1){
		node=val;
	}else if(t==0){
		node=min(node,val);
	}else{
		node=max(node,val);
	}
}

vec(pii) evs;

void merge(int node){
	seg[node].res=inf;
	seg[node].res=min(seg[node*2].res,seg[node*2+1].res);
	evs={};
	bool flag=0;
	rep(i,k){
		int l=seg[node*2].r[i];
		int r=seg[node*2+1].l[i];
		l=(l!=-1?l:-inf);
		r=(r!=-1?r:inf);
		if(l==-inf and r==inf){
			flag=1;
		}
		evs.pb(pii(r,l));
		int v0l=seg[node*2].l[i];
		int v0r=seg[node*2].r[i];
		int v1l=seg[node*2+1].l[i];
		int v1r=seg[node*2+1].r[i];
		seg[node].l[i]=seg[node].r[i]=-1;
		update_node(seg[node].l[i],v0l);
		update_node(seg[node].l[i],v1l);
		update_node(seg[node].r[i],v0r,1);
		update_node(seg[node].r[i],v1r,1);
	}
	// if(flag){
	// 	return;
	// }
	sort(evs.begin(), evs.end());
	int mi=inf;
	per(i,sz(evs)){
		if(mi==-inf){
			break;
		}
		if(evs[i].fi!=inf and mi!=inf){
			seg[node].res=min(seg[node].res,evs[i].fi-mi+1);
		}
		mi=min(mi,evs[i].se);
	}
}

void update(int pos,int v){
	int node=id[pos];
	rep(i,k){
		seg[node].l[i]=seg[node].r[i]=-1;
	}
	seg[node].res=inf;
	seg[node].l[v]=seg[node].r[v]=pos;
	node>>=1;
	while(node){
		merge(node);
		node>>=1;
	}
}

signed main(){
_3Pxrs5V;
	int n,q;
	cin>>n>>k>>q;
	build(1,0,n);
	rep(i,n){
		int v;
		cin>>v;
		v-=1;
		update(i,v);
	}
	rep(_,q){
		int t;
		cin>>t;
		if(t==1){
			int i,v;
			cin>>i>>v;
			i-=1,v-=1;
			update(i,v);
		}else{
			if(k==1){
				cout<<"1\n";
			}else{
				int res=seg[1].res;
				cout<<(res>n?-1:res)<<"\n";
			}
		}
	}
}

Compilation message

nekameleoni.cpp: In function 'void merge(int)':
nekameleoni.cpp:70:7: warning: variable 'flag' set but not used [-Wunused-but-set-variable]
   70 |  bool flag=0;
      |       ^~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 21 ms 3540 KB Output is correct
2 Correct 16 ms 3660 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 61 ms 4564 KB Output is correct
2 Correct 66 ms 4672 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 151 ms 6756 KB Output is correct
2 Correct 143 ms 6756 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 942 ms 26780 KB Output is correct
2 Correct 2984 ms 76988 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1777 ms 53528 KB Output is correct
2 Execution timed out 3076 ms 106380 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 1909 ms 53580 KB Output is correct
2 Execution timed out 3070 ms 105544 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2523 ms 53732 KB Output is correct
2 Execution timed out 3073 ms 106472 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Correct 2507 ms 53548 KB Output is correct
2 Execution timed out 3074 ms 106356 KB Time limit exceeded
3 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3066 ms 106540 KB Time limit exceeded
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Execution timed out 3083 ms 106444 KB Time limit exceeded
2 Halted 0 ms 0 KB -