제출 #401010

#제출 시각아이디문제언어결과실행 시간메모리
401010errorgornThe short shank; Redemption (BOI21_prison)C++17
100 / 100
1390 ms460556 KiB
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define ii pair<int,int>
#define fi first
#define se second

#define puf push_front
#define pof pop_front
#define pub push_back
#define pob pop_back

#define rep(x,s,e) for (auto x=s-(s>e);x!=e-(s>e);(s<e?x++:x--))
#define all(x) (x).begin(),(x).end()
#define sz(x) (int) (x).size()

struct node{
	int s,e,m;
	ii val;
	int lazy=0;
	node *l,*r;
	
	node (int _s,int _e){
		s=_s,e=_e,m=s+e>>1;
		val=ii(0,s);
		
		if (s!=e){
			l=new node(s,m);
			r=new node(m+1,e);
		}
	}
	
	void propo(){
		if (lazy){
			val.fi+=lazy;
			if (s!=e){
				l->lazy+=lazy;
				r->lazy+=lazy;
			}
			lazy=0;
		}
	}
	
	void update(int i,int j,int k){
		if (s==i && e==j) lazy+=k;
		else{
			if (j<=m) l->update(i,j,k);
			else if (m<i) r->update(i,j,k);
			else l->update(i,m,k),r->update(m+1,j,k);
			
			l->propo(),r->propo();
			val=max(l->val,r->val);
		}
	}
	
	ii query(int i,int j){
		propo();
		
		if (s==i && e==j) return val;
		else if (j<=m) return l->query(i,j);
		else if (m<i) return r->query(i,j);
		else return max(l->query(i,m),r->query(m+1,j));
	}
} *root=new node(0,4000005);

int n,d,t;
int arr[2000005];
int nxt[2000005];

vector<ii> brac;

int dist[4000005];
int ptr[4000005];
int range[4000005];

inline void read(int& x) {
    x = 0;
    char ch = getchar_unlocked();
    while (ch&16){ 
		//this will break when ‘\n’ or ‘ ‘ is encountered
		x = (x << 3) + (x << 1) + (ch&15);
		ch = getchar_unlocked();
	}
}

main(){
	cin.tie(0);
	cout.tie(0);
	cin.sync_with_stdio(false);
	
	read(n),read(d),read(t);
	rep(x,0,n) read(arr[x]);
	
	arr[n]=-1;
	vector<int> stk={n};
	
	rep(x,n,0){
		while (arr[x]-x<arr[stk.back()]-stk.back()) stk.pob();
		nxt[x]=min(stk.back()-x,max(t-arr[x]+1,0));
		stk.pub(x);
	}
	
	//rep(x,0,n) cout<<nxt[x]<<" "; cout<<endl;
	
	rep(x,0,n) if (nxt[x]){
		brac.pub(ii(x,1));
		brac.pub(ii(x+nxt[x],0));
	}
	
	sort(all(brac));
	
	/*
	for (auto &it:brac){
		cout<<it.fi<<" "<<it.se<<endl;
	}
	*/
	
	stk.clear();
	int p=0;
	int IDX=0;
	bool pc=false;
	int s=0;
	
	memset(ptr,-1,sizeof(ptr));
	
	int ans=0;
	for (auto &it:brac){
		range[IDX]=IDX;
		
		if (pc && !stk.empty()){
			ptr[stk.back()]=IDX;
			stk.pob();
			range[IDX]=range[stk.back()];
			ptr[stk.back()]=IDX;
			stk.pob();
		}
	
		if (it.se==1){
			ans++;
			
			if (s){
				dist[IDX]=it.fi-p;
				stk.pub(IDX);
				IDX++;
			}
			p=it.fi+1;
			pc=false;
			
			s++;
		}
		else{
			dist[IDX]=it.fi-p;
			p=it.fi;
			pc=true;
			
			stk.pub(IDX);
			IDX++;
			
			s--;
		}
		if (s==0) stk.pob();
		
		//cout<<it.fi<<" "<<it.se<<" "<<IDX<<endl;
	}
	
	//rep(x,0,IDX) cout<<dist[x]<<" "; cout<<endl;
	//rep(x,0,IDX) cout<<ptr[x]<<" "; cout<<endl;
	//rep(x,0,IDX) cout<<range[x]<<" "; cout<<endl;
	
	rep(x,0,IDX) ans+=dist[x];
	rep(x,0,IDX) root->update(range[x],x,dist[x]);
	
	
	rep(zzz,0,d){
		auto temp=root->query(0,4000005);
		if (temp.fi==0) break;
		
		ans-=temp.fi;
		while (temp.se!=-1){
			if (dist[temp.se]==-1) break;
			root->update(range[temp.se],temp.se,-dist[temp.se]);
			dist[temp.se]=-1;
			
			temp.se=ptr[temp.se];
		}
	}
	
	cout<<ans<<endl;
}

컴파일 시 표준 에러 (stderr) 메시지

prison.cpp: In constructor 'node::node(int, int)':
prison.cpp:29:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
   29 |   s=_s,e=_e,m=s+e>>1;
      |               ~^~
prison.cpp: At global scope:
prison.cpp:91:6: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
   91 | main(){
      |      ^
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...