#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define eb emplace_back
#define pu push
#define ins insert
#define fi first
#define se second
#define all(a) a.begin(),a.end()
#define bruh ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define fu(x,a,b) for (auto x=a;x<=b;x++)
#define fd(x,a,b) for (auto x=a;x>=b;x--)
#define int ll
using namespace std;
//mt19937 mt(chrono::steady_clock::now().time_since_epoch().count());
/*
Competitive Programming notes that I need to study & fix my dumbass self:
1. Coding:
- Always be sure to check the memory of arrays (maybe use vectors), for loops
- Always try to maximize the memory if possible, even if you are going for subtasks
- Do not exploit #define int long long, it will kill you
2. Stress:
- Always try generating big testcases and try if they run
3. Time management:
- Don't overcommit or undercommit, always spend a certain amount of time to think a problem, don't just look at it and say I'm fucked
- Do not spend too much time coding brute-force solutions, they should be easily-codable solutions that don't take up too much time
Time management schedule:
Offline / LAH days (4 problems - 3h):
15' thinking of solution / idea
1. no idea: skip
2. yes idea: continue thinking for <= 15'
+ implementing: <= 20'
+ brute-force: <= 5'
+ test generator: <= 5'
I hate offline because I am dumb
*/
typedef pair<int, int> ii;
const int N = 3e5+5;
const int B = 750;
const int mod = 1e9+7;
const int inf = 1e18;
using cd = complex<double>;
const long double PI = acos(-1);
int power(int a,int b) {ll x = 1;if (a >= mod) a%=mod; while (b) {if (b & 1) x = x*a % mod;a = a*a % mod;b>>=1;}return x;}
int n,q,d;
int c[N], ans[N];
vector<ii> qu[N];
int st[4*N], lazy[4*N], mn[4*N];
void build(int id, int l, int r)
{
if (l == r)
{
mn[id] = c[l];
return;
}
int mid = l+r>>1;
build(id*2, l, mid); build(id*2+1, mid+1, r);
mn[id] = min(mn[id*2], mn[id*2+1]);
}
void push(int id, int l, int r)
{
int mid = l+r>>1;
st[id*2] += (mid-l+1) * lazy[id]; st[id*2+1] += (r-mid) * lazy[id];
mn[id*2] -= lazy[id] * d; mn[id*2+1] -= lazy[id] * d;
lazy[id*2] += lazy[id], lazy[id*2+1] += lazy[id];
lazy[id] = 0;
}
void update(int id, int l, int r, int u, int v, int val)
{
if (l > v || r < u) return;
if (u <= l && r <= v)
{
st[id] += (r-l+1) * val;
mn[id] -= val * d;
lazy[id] += val;
return;
}
int mid = l+r>>1;
push(id, l, r);
update(id*2, l, mid, u, v, val); update(id*2+1, mid+1, r, u, v, val);
st[id] = st[id*2] + st[id*2+1];
mn[id] = min(mn[id*2], mn[id*2+1]);
}
int get(int id, int l, int r, int u, int v)
{
if (l > v || r < u) return 0;
if (u <= l && r <= v) return st[id];
int mid = l+r>>1;
push(id, l, r);
return get(id*2, l, mid, u, v) + get(id*2+1, mid+1, r, u, v);
}
int query(int id, int l, int r, int u, int v)
{
if (l > v || r < u) return inf;
if (u <= l && r <= v) return mn[id];
int mid = l+r>>1;
push(id, l, r);
return min(query(id*2, l, mid, u, v), query(id*2+1, mid+1, r, u, v));
}
void solve()
{
cin>>n>>d;
for (int i = 1; i <= n; i++) cin>>c[i];
build(1,1,n);
cin>>q;
for (int i = 0; i < q; i++)
{
int l,r; cin>>l>>r;
qu[r].pb({l, i});
}
stack<ii> block;
for (int i = 1; i <= n; i++)
{
int l = i, r = i;
while (!block.empty())
{
auto p = block.top();
int l1 = p.fi, r1 = p.se;
int v1 = c[r1] - d * get(1,1,n,r1,r1), v = c[l] - d * get(1,1,n,l,l);
if (v >= v1)
{
if (v - v1 >= d) break;
else
{
l = l1;
block.pop();
break;
}
} else
{
int add = (v1 - v) / d + ((v1-v) % d == 0 ? 0 : 1);
update(1,1,n,l1,r1,add);
l = l1;
block.pop();
}
}
block.push({l,r});
for (auto j : qu[i])
{
int rmn = query(1,1,n,j.fi,i);
if (rmn < 0) ans[j.se] = -1;
else ans[j.se] = get(1,1,n,j.fi,i);
}
}
for (int i = 0; i < q; i++) cout<<ans[i]<<endl;
}
/*
Go through the mistakes you usually make and revise your code, for god's sake...
*/
signed main()
{
bruh
//freopen("input.inp","r",stdin);
//freopen("output.inp","w",stdout);
int t = 1;
// cin>>t;
while (t--)
{
solve();
cout<<"\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... |