This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ldb long double
#define db double
#define fin(x) freopen(x,"r",stdin)
#define fout(x) freopen(x,"w",stdout)
#define fo(i,l,r) for(int i=(l);i<=(r);i++)
#define foi(i,l,r) for(int i=(l);i>=(r);i--)
#define el cout<<'\n';
#define cel cerr<<'\n';
#define all(x) x.begin(),x.end()
#define ii pair<int,int>
#define iii pair<int,ii>
#define gb(x,i) (((x)>>(i))&1)
#define mask(i) (1LL<<(i))
#define TIME (1.0 * clock()/CLOCKS_PER_SEC)
using namespace std;
const int N=3e5+5;
const int bl=60;
const ll base=1e9+7;
const ll inf=1e9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll Rand(ll l,ll r)
{
return l+(1ll*rng()*rng()%(r-l+1)+(r-l+1))%(r-l+1);
}
template<class X,class Y>bool maximize(X &a,Y b)
{
if(a<b) return a=b,true;
return false;
}
template<class X,class Y>bool minimize(X &a, Y b)
{
if(a>b) return a=b,true;
return false;
}
void add(auto &a,auto b)
{
a+=b;
if(a>=base) a-=base;
if(a<0) a+=base;
}
int n,q;
ll d,c[N],a[N],sum[N],ans[N];
stack<int>cc;
vector<ii>g[N];
ll st[4*N],lazy[4*N];
void push(int id,int l,int r,int mid)
{
ll &x=lazy[id];
if(!~x) return;
st[id<<1]=1LL*x*(mid-l+1);
st[id<<1|1]=1LL*x*(r-mid);
lazy[id<<1]=lazy[id<<1|1]=x;
x=-1;
}
void update(int id,int l,int r,int u,int v,ll val)
{
if(r<u or l>v) return;
if(u<=l and r<=v)
{
st[id]=1LL*(r-l+1)*val;
lazy[id]=val;
return;
}
int mid=(l+r)>>1;
push(id,l,r,mid);
update(id<<1,l,mid,u,v,val),update(id<<1|1,mid+1,r,u,v,val);
st[id]=st[id<<1]+st[id<<1|1];
}
ll get(int id,int l,int r,int u,int v)
{
if(r<u or l>v) return 0;
if(u<=l and r<=v) return st[id];
int mid=(l+r)>>1;
push(id,l,r,mid);
return get(id<<1,l,mid,u,v)+get(id<<1|1,mid+1,r,u,v);
}
int main()
{
#define task "o"
if(fopen(task".inp","r"))
{
fin(task".inp");
//fout(task".out");
}
srand(time(NULL));
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>d;
fo(i,1,n)
{
cin>>c[i];
a[i]=a[i-1]+((c[i]-c[i-1]%d)%d+d)%d;
sum[i]=sum[i-1]+c[i]-a[i];
}
cin>>q;
fo(i,1,q)
{
int l,r;
cin>>l>>r,g[r].pb({l,i});
}
memset(lazy,-1,sizeof(lazy));
fo(i,1,n)
{
while(cc.size() and c[cc.top()]-a[cc.top()]>=c[i]-a[i]) cc.pop();
int x=(cc.size()?cc.top()+1:1);
update(1,1,n,x,i,c[i]-a[i]),cc.push(i);
for(auto [l,id]:g[i])
{
if(get(1,1,n,l,l)+a[l]-c[l]%d<0) ans[id]=-1;
else ans[id]=(sum[i]-sum[l-1]-get(1,1,n,l,i))/d;
}
}
fo(i,1,q) cout<<ans[i],el
cerr<<"Time elapsed: "<<TIME<<" s.\n";
}
Compilation message (stderr)
Main.cpp:41:10: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
41 | void add(auto &a,auto b)
| ^~~~
Main.cpp:41:18: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
41 | void add(auto &a,auto b)
| ^~~~
Main.cpp: In function 'int main()':
Main.cpp:112:18: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
112 | for(auto [l,id]:g[i])
| ^
Main.cpp:9:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | #define fin(x) freopen(x,"r",stdin)
| ~~~~~~~^~~~~~~~~~~~~
Main.cpp:88:9: note: in expansion of macro 'fin'
88 | fin(task".inp");
| ^~~
# | 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... |