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>
typedef long long ll;
using namespace std;
const int BUCKET = 320;
struct Query{
int s, e, x;
Query(){}
Query(int _s, int _e, int _x): s(_s), e(_e), x(_x) {}
bool operator<(const Query &Q) const{
if (s/BUCKET==Q.s/BUCKET) return e<Q.e;
return s/BUCKET<Q.s/BUCKET;
}
}query[100100];
int a[100100], idx[100100];
ll ans[100100];
vector<int> v;
struct Seg{
ll tree[200200];
int sz;
void init(int n) {sz=n;}
void update(int p, int val){
for (tree[p+=sz]+=val;p>1;p>>=1) tree[p>>1] = max(tree[p], tree[p^1]);
}
ll query(int l){
int r = sz;
ll ret = 0;
for (l +=sz, r += sz;l<r;l>>=1, r>>=1){
if (l&1) ret = max(ret, tree[l++]);
if (r&1) ret = max(tree[--r], ret);
}
return ret;
}
}tree;
int main(){
int n, q;
scanf("%d %d", &n, &q);
for (int i=0;i<n;i++){
scanf("%d", a+i);
v.push_back(a[i]);
}
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
for (int i=0;i<n;i++) idx[i] = lower_bound(v.begin(), v.end(), a[i])-v.begin();
for (int i=0;i<q;i++){
scanf("%d %d", &query[i].s, &query[i].e);
query[i].s--;
query[i].x = i;
}
sort(query, query+q);
tree.init((int)v.size());
int s = 0, e = 0;
for (int i=0;i<q;i++){
//printf("%d %d %d\n", query[i].s, query[i].e, query[i].x);
while(e<query[i].e){
tree.update(idx[e], v[idx[e]]);
e++;
}
while(s>query[i].s){
--s;
tree.update(idx[s], v[idx[s]]);
}
while(e>query[i].e){
--e;
tree.update(idx[e], -v[idx[e]]);
}
while(s<query[i].s){
tree.update(idx[s], -v[idx[s]]);
s++;
}
ans[query[i].x] = tree.query(0);
}
for (int i=0;i<q;i++) printf("%lld\n", ans[i]);
return 0;
}
Compilation message (stderr)
historic.cpp: In function 'int main()':
historic.cpp:38:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
38 | scanf("%d %d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~~
historic.cpp:40:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | scanf("%d", a+i);
| ~~~~~^~~~~~~~~~~
historic.cpp:47:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
47 | scanf("%d %d", &query[i].s, &query[i].e);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |