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;
typedef long long ll;
int bucket = 316;
struct query
{
int s, e, i;
query() : query(0, 0, 0) {}
query(int _s, int _e, int _i) : s(_s), e(_e), i(_i) {}
bool operator < (const query &O)
{
if(s/bucket == O.s/bucket) return e < O.e;
return s/bucket < O.s/bucket;
}
};
ll X[100002], Y[100002], I[100002];
query Q[100002];
ll tree[400002];
void update(int i, int l, int r, int id, ll v)
{
if(l == r) { tree[i] += v; return; }
int m = (l + r)/2;
if(id <= m) update(i*2, l, m, id, v);
else update(i*2+1, m+1, r, id, v);
tree[i] = max(tree[i*2], tree[i*2+1]);
}
ll ans[100002];
int main()
{
ios::sync_with_stdio(false); cin.tie(NULL);
int n, q; cin >> n >> q;
for(int i=1;i<=n;i++) cin >> X[i];
for(int i=1;i<=q;i++)
{
int l, r; cin >> l >> r;
Q[i] = query(l, r, i);
}
sort(Q + 1, Q + q + 1);
for(int i=1;i<=n;i++) Y[i] = X[i];
sort(Y + 1, Y + n + 1);
for(int i=1;i<=n;i++) I[i] = lower_bound(Y + 1, Y + n + 1, X[i]) - Y;
for(int i=Q[1].s;i<=Q[1].e;i++)
update(1, 1, n, I[i], X[i]);
ans[Q[1].i] = tree[1];
int s = Q[1].s, e = Q[1].e;
for(int i=2;i<=q;i++)
{
while(s < Q[i].s)
{
update(1, 1, n, I[s], -X[s]);
s++;
}
while(s > Q[i].s)
{
s--;
update(1, 1, n, I[s], X[s]);
}
while(e < Q[i].e)
{
e++;
update(1, 1, n, I[e], X[e]);
}
while(e > Q[i].e)
{
update(1, 1, n, I[e], -X[e]);
e--;
}
ans[Q[i].i] = tree[1];
}
for(int i=1;i<=q;i++) cout << ans[i] << "\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... |