제출 #1328008

#제출 시각아이디문제언어결과실행 시간메모리
1328008joacruSnowball (JOI21_ho_t2)C++20
100 / 100
104 ms12992 KiB
#include <iostream>
#include <assert.h>
#include <algorithm>
#include <vector>
#include <queue>

#define forn(i,n) for(int i=0;i<(int)n;++i)
#define fort(i,n) for(int i=0;i<=(int)n;++i)
#define fori(i,a,n) for(int i=a;i<(int)n;++i)
#define forit(i,a,n) for(int i=a;i<=(int)n;++i)

#define DBG(a) cerr<<#a<<" = "<<(a)<<endl
#define DBGA(a) cerr<<#a<<" = "<<(a)<<", ";
#define DBG2(a,b) do{DBGA(a)DBG(b);}while(0)

#define ALL(v) v.begin(),v.end()
#define SZ(v) (int)v.size()

using namespace std;

template<typename T>
ostream &operator<<(ostream &os, vector<T> v){
	os<<"[";
	forn(i,SZ(v)){
		if(i) os<<", ";
		os<<v[i];
	}
	os<<"]";
	return os;
}

template<typename T>
using PQ = priority_queue<T,vector<T>,greater<T>>;

typedef long long ll;

const ll INF = 1000000000000000000;

void solve(){
	
	int n, q;
	cin>>n>>q;
	
	vector<ll> a(n);
	for(ll &x: a) cin>>x;
	
	PQ<pair<ll,int>> toLeft, toRight; // no se si es necesario el index
	
	toLeft.push({INF, 0});
	fori(i,1,n) toLeft.push({a[i]-a[i-1], i});
	
	toRight.push({INF,n-1});
	for(int i=n-2;i>=0;--i) toRight.push({a[i+1]-a[i], i});
	
	ll l = 0, r = 0, x = 0; // el rango en el que ya me movi, el punto en el que estoy
	vector<ll> ansL(n,-1), ansR(n,-1);
	ll aL = 0, aR = 0; // redundantes
	
	while(q--){
		ll v;
		cin>>v;
		ll nx = x + v;
		//~ DBG2(v, nx);
		if(v < 0){ // hacia la izquierda
			if(nx < l){ // hay novedades
				while(SZ(toLeft) && toLeft.top().first - r < abs(nx)){
					int i = toLeft.top().second;
					ll d = toLeft.top().first;
					ansL[i] = max(0ll, d + l - r) + aL;
					//~ DBG2(i, ansL[i]);
					toLeft.pop();
				}
				aL += l - nx;
				l = nx;
			}
		} else{ // hacia la derecha
			if(nx > r){
				while(SZ(toRight) && toRight.top().first + l < abs(nx)){
					int i = toRight.top().second;
					ll d = toRight.top().first;
					ansR[i] = max(0ll, d - r + l) + aR;
					//~ DBG2(i, ansR[i]);
					toRight.pop();
				}
				aR += nx - r;
				r = nx;
			}
		}
		x = nx;
	}
	
	forn(i,n){
		if(ansL[i] == -1) ansL[i] = -l;
		if(ansR[i] == -1) ansR[i] = r;
		//~ DBG2(ansL[i], ansR[i]);
		ll ans = ansL[i] + ansR[i];
		cout<<ans<<"\n";
	}
	
}

int main(){
	
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	
	#ifdef LOCAL
	freopen("B.in", "r", stdin);
	int tcs;
	cin>>tcs;
	while(tcs--)
	#endif
	solve();
	
	return 0;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...