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>
using namespace std;
long long int N,D,Q;
long long int lst[300100];
long long int pre[300100];
long long int apre[300100];
long long int modlst[300100];
vector<pair<int,long long int>> v;
vector<pair<pair<int,int>,int>> queries;
long long int ans[300100];
const long long int inf = 1.1e18;
struct node{
int s, e, m;
long long int v;
long long int lazy;
node *l, *r;
node(int S, int E){
s = S;
e = E;
m = (s + e)/2;
v = 0;
lazy = 0;
if(s != e){
l = new node(s,m);
r = new node(m + 1, e);
}
}
void update(int S, int E, long long int k){
if(S <= s && e <= E){
v += (e - s + 1) * k;
lazy += k;
return;
}
v += (min(e,E) - max(s,S) + 1) * k;
if(s != e){
if(S <= m){
l -> update(S,E,k);
}
if(m < E){
r -> update(S,E,k);
}
}
}
long long int query(int S, int E){
if(S <= s && e <= E) return v;
long long int V = 0;
if(S <= m){
V += l -> query(S,E);
}
if(m < E){
V += r -> query(S,E);
}
return V + (min(e,E) - max(s,S) + 1) * lazy;
}
}*root;
int main(){
scanf(" %lld",&N);
scanf(" %lld",&D);
for(int i = 0; i < N; i++){
scanf(" %lld",&lst[i]);
}
modlst[0] = 0;
long long int h = 0;
for(int i = 0; i < N; i++){
if(i == 0){
pre[i] = 0;
continue;
}
if(lst[i] % D >= lst[i - 1] % D){
h += lst[i] % D - lst[i - 1] % D;
pre[i] = h + pre[i - 1];
}
else{
h += lst[i] % D + D - lst[i - 1] % D;
}
modlst[i] = (lst[i] - h - lst[0]) / D;
}
apre[0] = 0;
for(int i = 1; i < N; i++){
apre[i] = apre[i - 1] + modlst[i];
}
root = new node(0,N - 1);
scanf(" %d",&Q);
for(int i = 0; i < Q; i++){
int l, r;
scanf(" %d",&l);
scanf(" %d",&r);
l--;
r--;
queries.push_back({{r,l},i});
}
//for(int i = 0; i < N; i++) printf("%d\n",modlst[i]);
sort(queries.begin(),queries.end());
int it = 0;
for(int i = 0; i < queries.size(); i++){
while(it != N && it <= queries[i].first.first){
while(!v.empty() && v.back().second >= modlst[it]){
pair<int, long long int> ii = v.back();
v.pop_back();
if(v.empty()){
root -> update(0,ii.first, -ii.second);
}
else{
root -> update(v.back().first + 1, ii.first, -ii.second);
}
}
if(v.empty()){
root -> update(0,it,modlst[it]);
}
else{
root -> update(v.back().first + 1, it, modlst[it]);
}
v.push_back({it,modlst[it]});
it++;
}
int l = queries[i].first.second;
int r = queries[i].first.first;
auto it = lower_bound(v.begin(),v.end(),make_pair(l,-inf));
//printf("it: %d %d\n",it -> second,queries[i].second);
if(lst[l]/D - modlst[l] + it -> second < 0){
ans[queries[i].second] = -1;
continue;
}
long long int num = apre[r] - ((l == 0) ? 0 : apre[l - 1]);
//printf("num: %d\n",num);
num -= root -> query(l,r);
//printf("%d\n",root -> query(l,r));
ans[queries[i].second] = num;
}
for(int i = 0; i < Q; i++){
printf("%lld\n",ans[i]);
}
}
Compilation message (stderr)
Main.cpp: In function 'int main()':
Main.cpp:105:14: warning: format '%d' expects argument of type 'int*', but argument 2 has type 'long long int*' [-Wformat=]
105 | scanf(" %d",&Q);
| ~^ ~~
| | |
| | long long int*
| int*
| %lld
Main.cpp:123:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<std::pair<int, int>, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
123 | for(int i = 0; i < queries.size(); i++){
| ~~^~~~~~~~~~~~~~~~
Main.cpp:72:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
72 | scanf(" %lld",&N);
| ~~~~~^~~~~~~~~~~~
Main.cpp:73:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
73 | scanf(" %lld",&D);
| ~~~~~^~~~~~~~~~~~
Main.cpp:76:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
76 | scanf(" %lld",&lst[i]);
| ~~~~~^~~~~~~~~~~~~~~~~
Main.cpp:105:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
105 | scanf(" %d",&Q);
| ~~~~~^~~~~~~~~~
Main.cpp:109:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
109 | scanf(" %d",&l);
| ~~~~~^~~~~~~~~~
Main.cpp:110:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | scanf(" %d",&r);
| ~~~~~^~~~~~~~~~
# | 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... |