Submission #444369

# Submission time Handle Problem Language Result Execution time Memory
444369 2021-07-13T19:14:09 Z aryan12 Snowball (JOI21_ho_t2) C++17
100 / 100
159 ms 13748 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());
int n, q;
vector<int> a, query;
vector<array<int, 2> > newQueries;

int CalcPre(int i) {
	int dist_pre = a[i] - a[i - 1];
	int cur_ans = 0;
	if(newQueries[q][0] - newQueries[q][1] < dist_pre) {
		return -newQueries[q][1];
	}
	int l = 0, r = q;
	int mid, ans = 0;
	while(l <= r) {
		mid = (l + r) >> 1;
		if(newQueries[mid][0] - newQueries[mid][1] < dist_pre) {
			ans = mid;
			l = mid + 1;
		}
		else {
			r = mid - 1;
		}
	}
	mid = ans;
	if(newQueries[mid + 1][1] < newQueries[mid][1]) {
		cur_ans += dist_pre - newQueries[mid + 1][0];
	}
	else {
		cur_ans += -newQueries[mid][1];
	}
	return cur_ans;
}

int CalcSuf(int i) {
	int dist_suf = a[i + 1] - a[i];
	int cur_ans = 0;
	if(newQueries[q][0] - newQueries[q][1] < dist_suf) {
		return newQueries[q][0];
	}
	int l = 0, r = q;
	int mid, ans = 0;
	//cout << "dist_suf = " << dist_suf << "\n";
	while(l <= r) {
		mid = (l + r) >> 1;
		if(newQueries[mid][0] - newQueries[mid][1] < dist_suf) {
			ans = mid;
			l = mid + 1;
		}
		else {
			r = mid - 1;
		}
	}
	mid = ans;
	//cout << "cur_ans = " << cur_ans << "\n";
	//cout << "mid = " << mid << "\n";
	if(newQueries[mid + 1][0] > newQueries[mid][0]) {
		cur_ans += dist_suf + newQueries[mid + 1][1];
	}
	else {
		cur_ans += newQueries[mid][0];
	}
	return cur_ans;
}

void Solve() {
	cin >> n >> q;
	a.resize(n + 1);
	a[0] = -1e18;
	for(int i = 1; i <= n; i++) {
		cin >> a[i];
	}
	a.push_back(1e18);
	query.resize(q + 1);
	for(int i = 1; i <= q; i++) {
		cin >> query[i];
	}
	int sum = 0, maxx = 0, minx = 0;
	newQueries.resize(q + 1);
	newQueries[0] = {0, 0};
	for(int i = 1; i <= q; i++) {
		sum += query[i];
		maxx = max(maxx, sum);
		minx = min(minx, sum);
		newQueries[i] = {maxx, minx};
		//cout << newQueries[i][0] << " " << newQueries[i][1] << "\n";
	}
	for(int i = 1; i <= n; i++) {
		//int ans1 = CalcPre(i), ans2 = CalcSuf(i);
		//cout << "ans1[" << i << "] = " << ans1 << ", ans2[" << i << "] = " << ans2 << "\n";
		int ans = CalcPre(i) + CalcSuf(i);
		cout << ans << "\n";
	}
}

int32_t main() {
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) {
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 332 KB Output is correct
2 Correct 1 ms 332 KB Output is correct
3 Correct 1 ms 332 KB Output is correct
4 Correct 2 ms 332 KB Output is correct
5 Correct 2 ms 332 KB Output is correct
6 Correct 1 ms 332 KB Output is correct
7 Correct 2 ms 460 KB Output is correct
8 Correct 2 ms 332 KB Output is correct
9 Correct 1 ms 332 KB Output is correct
10 Correct 1 ms 332 KB Output is correct
11 Correct 1 ms 316 KB Output is correct
12 Correct 1 ms 204 KB Output is correct
13 Correct 1 ms 204 KB Output is correct
14 Correct 1 ms 332 KB Output is correct
15 Correct 2 ms 332 KB Output is correct
16 Correct 2 ms 332 KB Output is correct
17 Correct 1 ms 332 KB Output is correct
18 Correct 0 ms 204 KB Output is correct
19 Correct 1 ms 332 KB Output is correct
20 Correct 32 ms 4956 KB Output is correct
21 Correct 32 ms 4964 KB Output is correct
22 Correct 35 ms 4944 KB Output is correct
23 Correct 30 ms 4984 KB Output is correct
24 Correct 37 ms 5244 KB Output is correct
25 Correct 141 ms 8180 KB Output is correct
26 Correct 143 ms 8064 KB Output is correct
27 Correct 138 ms 7884 KB Output is correct
28 Correct 157 ms 7904 KB Output is correct
29 Correct 138 ms 7796 KB Output is correct
30 Correct 132 ms 7264 KB Output is correct
31 Correct 89 ms 7000 KB Output is correct
32 Correct 75 ms 6900 KB Output is correct
33 Correct 13 ms 1068 KB Output is correct
34 Correct 144 ms 8172 KB Output is correct
35 Correct 141 ms 7876 KB Output is correct
36 Correct 159 ms 8168 KB Output is correct
37 Correct 155 ms 7976 KB Output is correct
38 Correct 140 ms 7972 KB Output is correct
39 Correct 135 ms 7872 KB Output is correct
40 Correct 96 ms 8016 KB Output is correct
41 Correct 35 ms 4960 KB Output is correct
42 Correct 83 ms 10032 KB Output is correct
43 Correct 113 ms 13344 KB Output is correct
44 Correct 36 ms 7620 KB Output is correct
45 Correct 75 ms 11560 KB Output is correct
46 Correct 101 ms 13472 KB Output is correct
47 Correct 104 ms 13748 KB Output is correct
48 Correct 100 ms 13736 KB Output is correct