#include <bits/stdc++.h>
using namespace std;
//~ #define int int64_t
#define pb push_back
#define emb emplace_back
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define sp << " " <<
#define N 300000
#define inf (int)1e9
typedef pair<int,int> ii;
typedef tuple<int,int,int> iii;
const int kok=320;
struct Item{
int l,r,i;
Item(int ll,int rr,int ii){
l=ll;r=rr;i=ii;
}
bool operator <(Item other){
return make_pair(l/kok,r)<make_pair(other.l/kok,other.r);
}
};
struct Node{
int l,r;
long long val;
Node(){l=r=val=0;}
};
vector<Node> st;
int cnt=0;
int addNode(){
st.pb(Node());
return cnt++;
}
int update(int node,int l,int r,int qx,int t){
if(r<qx || qx<l) return node;
if(!node) node=addNode();
if(l==r){
st[node].val+=t;
return node;
}
int mid=(l+r)/2;
st[node].l=update(st[node].l,l,mid,qx,t);
st[node].r=update(st[node].r,mid+1,r,qx,t);
st[node].val=max(st[st[node].l].val,st[st[node].r].val);
return node;
}
void solve(){
int n,q;
cin >> n >> q;
int a[n+1];
vector<Item> query;
for(int i=1;i<=n;i++)
cin >> a[i];
for(int i=0;i<q;i++){
int l,r;
cin >> l >> r;
query.emb(l,r,i);
}
sort(all(query));
int l=1,r=0;
long long ans[q];
addNode();addNode();
for(auto [ql,qr,i]:query){
while(r<qr){
++r;update(1,1,1e9,a[r],a[r]);
}
while(r>qr){
update(1,1,1e9,a[r],-a[r]);--r;
}
while(l<ql){
update(1,1,1e9,a[l],-a[l]);++l;
}
while(l>ql){
--l;update(1,1,1e9,a[l],a[l]);
}
ans[i]=st[1].val;
}
for(int i=0;i<q;i++)
cout << ans[i] << "\n";
}
int32_t main(){
//~ freopen("hopscotch.in","r",stdin);
//~ freopen("hopscotch.out","w",stdout);
cout << fixed << setprecision(0);
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
int test=1;
//~ cin >> test;
while(test--) solve();
}
| # | 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... |