#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 100000
#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);
}
};
long long st[4*N];
void update(int node,int l,int r,int qx,int t){
if(r<qx || qx<l) return;
if(l==r){
st[node]+=t;
return;
}
int mid=(l+r)/2;
update(2*node,l,mid,qx,t);
update(2*node+1,mid+1,r,qx,t);
st[node]=max(st[2*node],st[2*node+1]);
}
void solve(){
int n,q;
cin >> n >> q;
map<int,int> mp;
vector<Item> query;
int a[n+1],ind[n+1],cnt=0;
for(int i=1;i<=n;i++){
cin >> a[i];
if(!mp[a[i]])
mp[a[i]]=++cnt;
}
for(int i=1;i<=n;i++)
ind[i]=mp[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];
for(auto [ql,qr,i]:query){
while(r<qr){
++r;update(1,1,cnt,ind[r],a[r]);
}
while(r>qr){
update(1,1,cnt,ind[r],-a[r]);--r;
}
while(l<ql){
update(1,1,cnt,ind[l],-a[l]);++l;
}
while(l>ql){
--l;update(1,1,cnt,ind[l],a[l]);
}
ans[i]=st[1];
}
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... |