This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// author : thembululquaUwU
// 3.9.2024
#include <bits/stdc++.h>
#define pb push_back
#define fi first
#define se second
#define endl '\n'
using namespace std;
using ll = long long;
using ii = pair <int, int>;
using vi = vector <int>;
const int N = 3e5 + 5;
const int mod = 1e9 + 7;
void maxl(auto &a, auto b) {a = max(a, b);}
void minl(auto &a, auto b) {a = min(a, b);}
int n, q, lq[N], rq[N], sz[4 * N];
ll d, c[N], rs[N];
vector <int> query[N];
ll sum[4 * N], lz[4 * N];
void build(int s = 1, int l = 1, int r = n){
sz[s] = r - l + 1, sum[s] = 0, lz[s] = 0;
if (l == r) return; int mid = l + r >> 1;
build(s << 1, l, mid); build(s << 1 | 1, mid + 1, r);
}
void add(int s, ll val){
lz[s] += val; sum[s] += val * sz[s];
}
void down(int s){
if (lz[s] == 0) return;
add(s << 1, lz[s]); add(s << 1 | 1, lz[s]); lz[s] = 0;
}
void upd(int u, int v, ll val, int s = 1, int l = 1, int r = n){
if (u <= l && r <= v){
add(s, val);
return;
}
int mid = l + r >> 1; down(s);
if (mid >= u) upd(u, v, val, s << 1, l, mid);
if (mid + 1 <= v) upd(u, v, val, s << 1 | 1, mid + 1, r);
sum[s] = sum[s << 1] + sum[s << 1 | 1];
}
ll get(int u, int v, int s = 1, int l = 1, int r = n){
if (u <= l && r <= v) return sum[s];
int mid = l + r >> 1; down(s); ll ans = 0;
if (mid >= u) ans += get(u, v, s << 1, l, mid);
if (mid + 1 <= v) ans += get(u, v, s << 1 | 1, mid + 1, r);
return ans;
}
ll up(ll a, ll b){return (a + b - 1) / b;}
struct DSU{
vi par;
DSU(int n){
par.resize(n);
for (int i = 0; i < n; ++ i) par[i] = i;
}
int find(int u){
if (u == par[u]) return u;
return par[u] = find(par[u]);
}
bool check(int u, int v){
return find(u) == find(v);
}
void joint(int u, int v){
u = find(u); v = find(v);
par[u] = v;
}
};
void solve(){
cin >> n >> d;
for (int i = 1; i <= n; ++ i) cin >> c[i];
cin >> q;
for (int i = 1; i <= q; ++ i) {
cin >> lq[i] >> rq[i];
query[rq[i]].pb(i);
}
DSU pqh(n + 1); build();
for (int i = 1; i <= n; ++ i){
int u = i;
while (u > 1){
int r = u - 1, l = pqh.find(r);
ll nu = d * get(u, u), nr = d * get(r, r);
ll cost = up((c[r] - nr) - (c[u] - nu), d);
if (cost <= 0) break; pqh.joint(u, l); upd(l, r, cost); u = l;
}
for (int id : query[i]) {
if (c[lq[id]] - d * get(lq[id], lq[id]) < 0) rs[id] = -1;
else rs[id] = get(lq[id], rq[id]);
}
}
for (int i = 1; i <= q; ++ i) cout << rs[i] << endl;
}
int main(){
if (fopen("code.inp", "r")){
freopen("code.inp", "r", stdin);
freopen("code.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1; // cin >> t;
while (t --) solve();
return 0;
}
Compilation message (stderr)
Main.cpp:18:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
18 | void maxl(auto &a, auto b) {a = max(a, b);}
| ^~~~
Main.cpp:18:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
18 | void maxl(auto &a, auto b) {a = max(a, b);}
| ^~~~
Main.cpp:19:11: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
19 | void minl(auto &a, auto b) {a = min(a, b);}
| ^~~~
Main.cpp:19:20: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
19 | void minl(auto &a, auto b) {a = min(a, b);}
| ^~~~
Main.cpp: In function 'void build(int, int, int)':
Main.cpp:29:5: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
29 | if (l == r) return; int mid = l + r >> 1;
| ^~
Main.cpp:29:25: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
29 | if (l == r) return; int mid = l + r >> 1;
| ^~~
Main.cpp:29:37: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
29 | if (l == r) return; int mid = l + r >> 1;
| ~~^~~
Main.cpp: In function 'void upd(int, int, ll, int, int, int)':
Main.cpp:48:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
48 | int mid = l + r >> 1; down(s);
| ~~^~~
Main.cpp: In function 'll get(int, int, int, int, int)':
Main.cpp:56:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int mid = l + r >> 1; down(s); ll ans = 0;
| ~~^~~
Main.cpp: In function 'void solve()':
Main.cpp:103:13: warning: this 'if' clause does not guard... [-Wmisleading-indentation]
103 | if (cost <= 0) break; pqh.joint(u, l); upd(l, r, cost); u = l;
| ^~
Main.cpp:103:35: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'if'
103 | if (cost <= 0) break; pqh.joint(u, l); upd(l, r, cost); u = l;
| ^~~
Main.cpp: In function 'int main()':
Main.cpp:115:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
115 | freopen("code.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
Main.cpp:116:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
116 | freopen("code.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |