답안 #535387

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
535387 2022-03-10T06:52:15 Z PikaQ Snowball (JOI21_ho_t2) C++17
100 / 100
157 ms 24788 KB
#include<bits/stdc++.h>
#define int long long
#define forn(i,n) for(int i = 0;i < (n);i++)
#define Forn(i,n) for(int i = 1;i <= (n);i++)
#define all(p) p.begin(),p.end()
#define pb push_back
#define vi vector<int>
#define pii pair<int,int>
#define F first
#define S second
#define debug(x) cout << #x << ' ' << x << '\n';
using namespace std;
const int INF = (int) 1e18 + 9;
int n,q;

void solve(){
	cin >> n >> q;
	vi x(n+1);
	vi w(q);
	Forn(i,n) cin >> x[i];
	forn(i,q) cin >> w[i];
	vi lt(n+1),rt(n+1),ans(n+1);
	Forn(i,n) lt[i] = q-1;
	Forn(i,n) rt[i] = q-1;
	priority_queue<pii,vector<pii>,greater<pii>> pq;
	Forn(i,n){
		if(i == 1) {
			pq.push({INF,-1});
		}else {
			pq.push({x[i] - x[i-1],-i});
		}
	}
	Forn(i,n){
		if(i == n){
			pq.push({INF,1});
		}else{
			pq.push({x[i+1] - x[i],i});
		}
	}
	int mx = 0,mn = 0,cur = 0;
	forn(i,q){
		cur += w[i];
		if(cur > mx){
			w[i] = cur - mx;
			mx = cur;
		}
		else if(cur < mn){
			w[i] = cur - mn;
			mn = cur;
		}
		else {
			w[i] = 0;
		}
	}
	//forn(i,q) debug(w[i]);
	int nn = 0,nm = 0;
	forn(i,q){
		if(w[i] > 0) nm += w[i];
		if(w[i] < 0) nn -= w[i];
		while(pq.top().F <= (nn + nm)){
			pii cur = pq.top();pq.pop();
			if(cur.S > 0){
				rt[cur.S] = i;
				if(w[i] > 0 && cur.F < (nn + nm)) ans[cur.S] -= (nn + nm) - cur.F;
				//debug(rt[cur.S]);
				//debug(cur.S);
			}else{
				cur.S *= -1;
				lt[cur.S] = i;
				if(w[i] < 0 && cur.F < (nn + nm)) ans[cur.S] -= (nn + nm) - cur.F;
			//	debug(lt[cur.S]);
			//	debug(cur.S);
			}
		}
	}
	vi sp(q),sn(q);
	forn(i,q){
		if(w[i] > 0) sp[i] += w[i];
		if(w[i] < 0) sn[i] -= w[i];
		if(i){
			sp[i] += sp[i-1];
			sn[i] += sn[i-1];
		}
	}
	Forn(i,n){
		ans[i] += sp[rt[i]];
		ans[i] += sn[lt[i]];
	}
	Forn(i,n) cout << ans[i] << ' ';
	cout << '\n';
}

signed main(){
	cin.tie(NULL);
	cout.tie(NULL);
	ios_base::sync_with_stdio(0);
	solve();
	return 0;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 516 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 532 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 340 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 2 ms 596 KB Output is correct
5 Correct 1 ms 468 KB Output is correct
6 Correct 2 ms 468 KB Output is correct
7 Correct 2 ms 468 KB Output is correct
8 Correct 2 ms 468 KB Output is correct
9 Correct 2 ms 468 KB Output is correct
10 Correct 2 ms 516 KB Output is correct
11 Correct 1 ms 468 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 328 KB Output is correct
14 Correct 1 ms 340 KB Output is correct
15 Correct 2 ms 532 KB Output is correct
16 Correct 2 ms 468 KB Output is correct
17 Correct 2 ms 596 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 1 ms 468 KB Output is correct
20 Correct 32 ms 7132 KB Output is correct
21 Correct 28 ms 6856 KB Output is correct
22 Correct 27 ms 6740 KB Output is correct
23 Correct 26 ms 6756 KB Output is correct
24 Correct 34 ms 8136 KB Output is correct
25 Correct 148 ms 22776 KB Output is correct
26 Correct 153 ms 22704 KB Output is correct
27 Correct 144 ms 22488 KB Output is correct
28 Correct 147 ms 22736 KB Output is correct
29 Correct 146 ms 22108 KB Output is correct
30 Correct 129 ms 21464 KB Output is correct
31 Correct 122 ms 20944 KB Output is correct
32 Correct 103 ms 21084 KB Output is correct
33 Correct 14 ms 2644 KB Output is correct
34 Correct 145 ms 23132 KB Output is correct
35 Correct 142 ms 22616 KB Output is correct
36 Correct 143 ms 22864 KB Output is correct
37 Correct 150 ms 22704 KB Output is correct
38 Correct 157 ms 22484 KB Output is correct
39 Correct 148 ms 22628 KB Output is correct
40 Correct 130 ms 22608 KB Output is correct
41 Correct 29 ms 7636 KB Output is correct
42 Correct 64 ms 21092 KB Output is correct
43 Correct 119 ms 24404 KB Output is correct
44 Correct 31 ms 7628 KB Output is correct
45 Correct 76 ms 22620 KB Output is correct
46 Correct 114 ms 24404 KB Output is correct
47 Correct 130 ms 24752 KB Output is correct
48 Correct 118 ms 24788 KB Output is correct