| # | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
|---|---|---|---|---|---|---|---|
| 1298217 | nguyn | Fish 3 (JOI24_fish3) | C++20 | 7 ms | 2508 KiB |
#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...)
#endif // LOCAL
#define ll long long
#define F first
#define S second
#define pb push_back
#define pii pair<int, int>
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
const int N = 2e5 + 5;
const int mod = 1e9 + 7;
const ll inf = 1e18;
int n, d, q;
int c[N];
int a[N];
ll suf[N], p[N];
int lef[N][20];
ll val[N][20];
ll rmq[N][20];
ll get_min(int l, int r) {
int lg = 31 - __builtin_clz(r - l + 1);
return min(rmq[l][lg], rmq[r - (1 << lg) + 1][lg]);
}
ll get(int l, int r) {
if (l > r) return 0;
if (get_min(l, r) < - suf[r + 1] * d) return inf;
return (p[r] - p[l - 1]) - suf[r + 1] * (r - l + 1);
}
void solve() {
cin >> n >> d;
for (int i = 1; i <= n; i++) {
cin >> c[i];
}
for (int i = 2; i <= n; i++) {
if (c[i - 1] > c[i]) a[i] = (c[i - 1] - c[i] + d - 1) / d;
else a[i] = - (c[i] - c[i - 1]) / d;
}
for (int i = n; i >= 1; i--) {
suf[i] = suf[i + 1] + a[i];
}
for (int i = 1; i <= n; i++) {
p[i] = p[i - 1] + suf[i];
}
vector<int> vec;
vec.pb(1);
for (int i = 1; i <= n; i++) {
while(!vec.empty() && suf[vec.back()] > suf[i + 1]) vec.pop_back();
if (!vec.empty()) lef[i][0] = vec.back() - 1;
vec.pb(i + 1);
}
for (int i = 1; i <= n; i++) rmq[i][0] = - suf[i] * d + c[i - 1];
for (int i = 1; i < 20; i++) {
for (int j = 1; j <= n - (1 << i) + 1; j++) {
rmq[j][i] = min(rmq[j][i - 1], rmq[j + (1 << (i - 1))][i - 1]);
}
}
for (int i = 1; i <= n; i++) {
val[i][0] = get(lef[i][0] + 2, i);
}
for (int i = 1; i < 20; i++) {
for (int j = 1; j <= n; j++) {
lef[j][i] = lef[lef[j][i - 1]][i - 1];
val[j][i] = val[j][i - 1] + val[lef[j][i - 1]][i - 1];
if (val[j][i] > inf) val[j][i] = inf;
}
}
cin >> q;
while(q--) {
int l, r;
cin >> l >> r;
int cur = r;
ll res = 0;
for (int i = 19; i >= 0; i--) {
if (lef[cur][i] + 1 >= l) {
res += val[cur][i];
if (res > inf) res = inf;
cur = lef[cur][i];
}
}
res += get(l + 1, cur);
if (res > inf) res = inf;
if (res >= inf) cout << -1;
else cout << res;
cout << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "template"
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int T = 1;
// cin >> T;
while (T--) {
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
| # | 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... | ||||
