이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(time(0));
using ll = long long;
#define int long long
const int N = 2e5 + 1;
void solve() {
int n, d; cin >> n >> d;
vector<int> c(n + 1), pref (n + 1);
for (int i = 1; i <= n; ++i) {
cin >> c[i];
pref[i] = pref[i - 1] + c[i];
}
int q; cin >> q;
if (d == 1) {
vector<int> smalL (n + 1, 0);
vector<int>v;
for (int i = 1; i <= n; ++i) {
while ((int)v.size() && c[v.back()] >= c[i]) v.pop_back();
if ((int)v.size()) smalL[i] = v.back();
v.push_back(i);
}
while (q--) {
int l, r;
cin >> l >> r;
int ans = 0;
int v = r;
while (v > l) {
int id = max (smalL[v] + 1, l);
ans += pref[v] - pref[id - 1] - (v - id + 1) * c[v];
// cout << ans << ' ' << pref[v] - pref[id - 1] - (v - id + 1) * c[v] << ' ' << (v - id + 1) * c[v]<< '\n';
v = smalL[v];
}
cout << ans << '\n';
}
return;
}
if (n <= (int)3e3 && q <= (int)3e3) {
while (q--) {
int l, r; cin >> l >> r;
vector<int> a = c;
int res = 0;
bool bad = 0;
for (int i = r - 1; i >= l; --i) {
if (a[i] > a[i + 1]) {
int p = (a[i] - a[i + 1] + d - 1) / d;
res += p;
a[i] -= p * d;
}
if (a[i] < 0) {
bad = 1;
break;
}
}
cout << (!bad ? res : -1) << '\n';
}
return;
}
vector<int> mxl (n + 1);
for (int i = 1; i <= n; ++i) {
if (c[i]) mxl[i] = mxl[i - 1] + 1;
}
while (q--) {
int l, r; cin >> l >> r;
int ans = 0, wr = r;
r -= mxl[r];
if (r >= l) ans = pref[r] - pref[l - 1];
if (ans) {
if (d > 1) {
cout << "-1\n";
continue;
}
}
cout << ans << '\n';
}
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef LOCAL
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // LOCAL
int T = 1;
while (T--) solve();
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
Main.cpp: In function 'void solve()':
Main.cpp:70:22: warning: unused variable 'wr' [-Wunused-variable]
70 | int ans = 0, wr = r;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |