이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int M = 1 << 19, N = 2 * M, INF = 1e15 + 42;
struct Query {
int l, r, val = 0;
};
int sum[N], tag[N];
void applyOp(int i, int len, int add) {
sum[i] += len * add;
tag[i] += add;
}
void propage(int i, int len) {
applyOp(i*2, len, tag[i]);
applyOp(i*2+1, len, tag[i]);
tag[i] = 0;
}
int update(int i, int deb, int fin, int l, int r, int add) {
if(fin <= l || r <= deb) return 0;
if(l <= deb && fin <= r) {
applyOp(i, fin - deb, add);
return sum[i];
}
int mid = (deb + fin) >> 1;
propage(i, mid - deb);
int ans = update(i*2, deb, mid, l, r, add) + update(i*2+1, mid, fin, l, r, add);
sum[i] = sum[i*2] + sum[i*2+1];
return ans;
}
Query req[M];
vector<int> id[M];
int n, q, d, c[M], prefD[M], zero[M], nbD[M];
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
cin >> n >> d;
for(int i = 1; i <= n; i++)
cin >> c[i];
cin >> q;
for(int i = 0; i < q; i++) {
cin >> req[i].l >> req[i].r;
id[req[i].r].push_back(i);
req[i].r++;
}
for(int i = n; i > 0; i--) {
zero[i] = zero[i+1];
if(c[i] % d > c[i+1] % d)
zero[i]++;
nbD[i] = c[i] / d + zero[i];
prefD[i] = nbD[i];
}
for(int i = 1; i <= n; i++)
prefD[i] += prefD[i-1];
deque<int> imin;
nbD[0] = -INF;
imin.push_back(0);
for(int i = 1; i <= n; i++) {
while(nbD[imin[0]] > nbD[i]) {
update(1, 0, M, imin[1]+1, imin[0]+1, - nbD[imin[0]]);
imin.pop_front();
}
update(1, 0, M, imin[0]+1, i+1, nbD[i]);
imin.push_front(i);
for(int iQ : id[i]) {
if(update(1, 0, M, req[iQ].l, req[iQ].l+1, 0) < zero[req[iQ].l]) {
req[iQ].val = -1;
} else {
req[iQ].val = prefD[req[iQ].r-1] - prefD[req[iQ].l-1] - update(1, 0, M, req[iQ].l, req[iQ].r, 0);
}
}
}
for(int i = 0; i < q; i++)
cout << req[i].val << '\n';
}
# | 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... |