제출 #1138464

#제출 시각아이디문제언어결과실행 시간메모리
1138464PwoFish 3 (JOI24_fish3)C++20
100 / 100
540 ms106620 KiB
#include <bits/stdc++.h>
using namespace std;
#define int long long

int n, d, q, a[300005], b[300005], c[300005], pre[300005], ans[300005];
pair<int, int> up[300005][19];

int32_t main() {
	ios::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
	cin >> n >> d;
	for (int i = 1; i <= n; i++) {
		cin >> a[i]; c[i] = a[i];
		b[i] = a[i] / d;
		c[i] %= d;
		if (c[i] < c[i - 1]) {
			int tmp = (c[i - 1] - c[i] + d - 1) / d;
			b[i] -= tmp;
			c[i] += tmp * d;
		}
		pre[i] = pre[i - 1] + b[i];
	}

	vector<int> st;
	for (int i = n; i >= 1; i--) {
		while (!st.empty() && b[st.back()] > b[i]) {
			up[st.back()][0] = {i, pre[st.back()] - pre[i] - b[st.back()] * (st.back() - i)};
			st.pop_back();
		}
		st.push_back(i);
	}
	
	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= 18; j++)
			up[i][j] = {up[up[i][j - 1].first][j - 1].first,
				max(up[i][j - 1].second + up[up[i][j - 1].first][j - 1].second, (int) -4e18)};
	cin >> q;
	for (int i = 0; i < q; i++) {
		int l, r; cin >> l >> r; int ans = 0;
		for (int i = 18; i >= 0; i--)
			if (up[r][i].first >= l)
				ans += up[r][i].second, r = up[r][i].first;
		ans += pre[r] - pre[l - 1] + - b[r] * (r - l + 1);
		if (a[l] < (b[l] - b[r]) * d) cout << -1 << '\n';
		else cout << ans << '\n';
	}	
}
#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...