Submission #1329249

#TimeUsernameProblemLanguageResultExecution timeMemory
1329249JuanJLSnowball (JOI21_ho_t2)C++20
100 / 100
1220 ms13052 KiB
#include <bits/stdc++.h>

#define fst first
#define snd second
#define pb push_back
#define SZ(x) (int)x.size()
#define ALL(x) x.begin(),x.end()
#define forn(i,a,b) for(int i = a; i<b; i++)
#define mset(a,v) memset(a,v,sizeof(a))
#define FIN ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);

#ifdef D
	#define debug(x) cout << x
#else
	#define debug(x) // nada
#endif

using namespace std;
typedef long long ll;



int main(){
	ll n,q; cin>>n>>q;
	vector<ll> x(n); forn(i,0,n) cin>>x[i];
	vector<ll> w(q); forn(i,0,q) cin>>w[i];

	vector<pair<ll,ll>> right; right.pb({0,0});
	vector<pair<ll,ll>> left; left.pb({0,0});

	ll aux = 0;
	forn(i,0,q){
		aux+=w[i];
		if(aux>right.back().fst) right.pb({aux,i+1});
		if(aux*-1>left.back().fst) left.pb({aux*-1,i+1});
	}

	vector<pair<ll,ll>> iright = right; forn(i,0,SZ(right)) iright[i].fst=right[i].snd, iright[i].snd=right[i].fst;
	vector<pair<ll,ll>> ileft = left; forn(i,0,SZ(left)) ileft[i].fst=left[i].snd, ileft[i].snd=left[i].fst;

	forn(i,0,SZ(iright)) debug(iright[i].fst<<" "<<iright[i].snd<<" -- "); debug('\n');
	forn(i,0,SZ(ileft)) debug(ileft[i].fst<<" "<<ileft[i].snd<<" -- "); debug('\n');

	auto checklft = [&](ll mid, ll i) -> bool {
		if(i==0) return true;
		ll ind = upper_bound(ALL(ileft) , pair<ll,ll>{mid,900000000000000000})-ileft.begin(); ind--;
		ll ipos = x[i]-ileft[ind].snd;

		ind = upper_bound(ALL(iright) , pair<ll,ll>{mid,900000000000000000})-iright.begin(); ind--;
		ll antipos = x[i-1]+iright[ind].snd;

		if(antipos>ipos) return false;
		return true;
	};

	auto checkrgt = [&](ll mid, ll i) -> bool {
		if(i==n-1) return true;
		ll ind = upper_bound(ALL(iright) , pair<ll,ll>{mid,900000000000000000})-iright.begin(); ind--;
		ll ipos = x[i]+iright[ind].snd;

		ind = upper_bound(ALL(ileft) , pair<ll,ll>{mid,900000000000000000})-ileft.begin(); ind--;
		ll antipos = x[i+1]-ileft[ind].snd;

		if(ipos>antipos) return false;
		return true;
	};

	auto bestlft = [&](ll i, ll r) -> ll{
		ll indl;
		ll indr;
		ll ipos;
		ll antipos;

		ll ret = 0;
		indl = upper_bound(ALL(ileft) , pair<ll,ll>{r,900000000000000000})-ileft.begin(); indl--;
		ret=ileft[indl].snd;

		r++;

		indl = upper_bound(ALL(ileft) , pair<ll,ll>{r,900000000000000000})-ileft.begin(); indl--;
		ipos = x[i]-ileft[indl].snd;
		
		if(i>0){indr = upper_bound(ALL(iright) , pair<ll,ll>{r,900000000000000000})-iright.begin(); indr--;
		antipos = x[i-1]+iright[indr].snd;}

		debug(ret<<" o "<<ileft[indl].snd-(antipos>ipos?antipos-ipos:0)<<" "<<antipos<<" "<<ipos<<'\n');
		ret=max(ret, ileft[indl].snd-(antipos>ipos?antipos-ipos:0));
		return ret;
	};


	auto bestrgt = [&](ll i, ll r) -> ll{
		ll indl;
		ll indr;
		ll ipos;
		ll antipos;
	
		ll ret = 0;
		indr = upper_bound(ALL(iright) , pair<ll,ll>{r,900000000000000000})-iright.begin(); indr--;
		ret=iright[indr].snd;
	
		r++;
	
		indr = upper_bound(ALL(iright) , pair<ll,ll>{r,900000000000000000})-iright.begin(); indr--;
		ipos = x[i]+iright[indr].snd;
			
		if(i+1<n){indl = upper_bound(ALL(ileft) , pair<ll,ll>{r,900000000000000000})-ileft.begin(); indl--;
		antipos = x[i+1]-ileft[indl].snd;}

		debug(ret<<" o "<<iright[indr].snd-(ipos>antipos?ipos-antipos:0)<<" "<<iright[indr].snd<<" "<<ipos<<" "<<antipos<<'\n');
		ret=max(ret, iright[indr].snd-(ipos>antipos?ipos-antipos:0));
		return ret;
	};

	vector<ll> res(n,0);
	forn(i,0,n){

		ll l = 1; ll r=q;
		while(l<=r){
			ll mid = (l+r)/2;

			if(checklft(mid,i)) l = mid+1;
			else r = mid-1;			
		}
		debug(r<<'\n');

		ll indl;
		ll indr;
		ll ipos;
		ll antipos;

		ll blft = bestlft(i,r);
		debug("i "<<i<<" "<<blft<<'\n');
		res[i]+=blft;

		
		l = 1; r = q;
		while(l<=r){
			ll mid = (l+r)/2;

			if(checkrgt(mid,i)) l = mid+1;
			else r = mid-1;
		}
		debug(r<<'\n');

		ll brgt = bestrgt(i,r);
		debug("i "<<i<<" "<<brgt<<'\n');
		res[i]+=brgt;
	}

	forn(i,0,n) cout<<res[i]<<'\n';
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...