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;
using ll = long long;
#define mid ((l+r)>>1)
const int sq = 400;
inline int64_t hilbertOrder(int x, int y, int pow, int rotate) {
    if (pow == 0) {
        return 0;
    }
    int hpow = 1 << (pow-1);
    int seg = (x < hpow) ? (
        (y < hpow) ? 0 : 3
    ) : (
        (y < hpow) ? 1 : 2
    );
    seg = (seg + rotate) & 3;
    const int rotateDelta[4] = {3, 0, 0, 1};
    int nx = x & (x ^ hpow), ny = y & (y ^ hpow);
    int nrot = (rotate + rotateDelta[seg]) & 3;
    int64_t subSquareSize = int64_t(1) << (2*pow - 2);
    int64_t ans = seg * subSquareSize;
    int64_t add = hilbertOrder(nx, ny, pow-1, nrot);
    ans += (seg == 1 || seg == 2) ? add : (subSquareSize - add - 1);
    return ans;
}
struct query{
    int l, r, id;
    long long order;
    void init(){
        order = hilbertOrder(l, r, 21, 0);
    }
    bool operator < (const query &o) const{
        return order < o.order;
    }
}q[100100];
int n, m, cnt, k[100100], to[100100];
ll x[100100], tree[400400], ans[100100];
map<ll, int> mp;
void init(int node = 1, int l = 1, int r = cnt){
    if(l == r){
        to[l] = node;
        return;
    }
    init(node*2, l, mid), init(node*2+1, mid+1, r);
}
void update(int id, ll val){
    tree[id] += val;
    id /= 2;
    while(id){
        tree[id] = max(tree[id*2], tree[id*2+1]);
        id /= 2;
    }
}
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    cin >> n >> m;
    for(int i=1;i<=n;i++) cin >> x[i], mp[x[i]] = 0;
    for(auto &x : mp) x.second = ++cnt;
    init();
    for(int i=1;i<=n;i++) k[i] = to[mp[x[i]]];
    for(int i=1;i<=m;i++) cin >> q[i].l >> q[i].r, q[i].id = i, q[i].init();
    sort(q+1, q+1+m);
    int l = q[1].l, r = q[1].l-1;
    for(int i=1;i<=m;i++){
        while(l > q[i].l) l--, update(k[l], x[l]);
        while(r < q[i].r) r++, update(k[r], x[r]);
        while(l < q[i].l) update(k[l], -x[l]), l++;
        while(r > q[i].r) update(k[r], -x[r]), r--;
        ans[q[i].id] = tree[1];
    }
    for(int i=1;i<=m;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... |