从来木有想过update儿子呀,除了lazy~~
#define fi freopen("in.txt","r",stdin)
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <cmath>
#include <vector>
#include <algorithm>
using namespace std;
#define sfint(x) scanf("%d",&x)
#define sfint2(x,y) scanf("%d%d",&x,&y)
#define sfint3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define sfstr(c) scanf("%s",c)
#define pfint(x) printf("%d\n",x)
#define fr(i,s,n) for(i=s;i<n;++i)
#define _fr(i,n,s) for(i=n-1;i>=s;--i)
#define cl(a) memset(a,0,sizeof(a))
#define md ((troot.l+troot.r)>>1)
#define lc (root<<1)
#define rc (root<<1|1)
#define troot tree[root]
typedef long long ll;
const int N = 100010;
struct Seg{
int nad,pad,nmx,pmx; //pad:preadd,pmx:premax;
int l,r;
void up(Seg *l,Seg *r){
nmx = max(l->nmx+l->nad, r->nmx+r->nad);
pmx = max(max(l->pmx,l->nmx+l->pad),
max(r->pmx,r->nmx+r->pad) );
}
void down(Seg *l,Seg *r){
l->pad = max(l->pad,l->nad + pad);
r->pad = max(r->pad,r->nad + pad);
l->nad += nad;r->nad += nad;
nad = pad = 0;
}
}tree[N<<2];
void build(int root,int l,int r){
troot.l = l;troot.r = r;
if (l == r) return ;
int m = md;
build(lc,l,m);
build(rc,m+1,r);
}
void update(int root,int l,int r,int del){
if (l <= troot.l&&troot.r <= r){
troot.nad += del;
troot.pad = max(troot.pad,troot.nad);
return;
}
troot.down(&tree[lc],&tree[rc]);
int m = md;
if (l<=m) update(lc,l,r,del);
if (r>m) update(rc,l,r,del);
troot.up(&tree[lc],&tree[rc]);
}
int query(int root,int l,int r){
if (l <= troot.l &&troot.r <= r) return max(troot.pmx,troot.nmx+troot.pad);
troot.down(&tree[lc],&tree[rc]);
int m = md;int ret = 0;
if (l<=m) ret = max(ret , query(lc,l,r));
if (r>m) ret = max(ret,query(rc,l,r));
troot.up(&tree[lc],&tree[rc]);
return ret;
}
int n,num[N],q,pos[N<<1];
int ans[N];
struct Que{
int l,r;
int id;
}que[N];
bool cmp(Que a,Que b){
return a.r<b.r;
}
void solve(){
int i,j =1;
sort(que+1,que+q+1,cmp);
build(1,1,n);
fr(i , 1,n+1){
update(1,pos[num[i]+N]+1,i,num[i]);
pos[num[i]+N] = i;
while(que[j].r == i){
ans[que[j++].id] = query(1,que[j].l,que[j].r);
if (j>q) return;
}
}
}
void inp(){
int i;
sfint(n);
fr(i,1,n+1) sfint(num[i]);
sfint(q);
fr(i,1,q+1) sfint2(que[i].l,que[i].r),que[i].id = i;
}
void dsp(){
for(int i = 1;i<q+1;i++) {
printf("%d\n",ans[i]);
}
}
int main(){
#ifdef local
fi;
#endif
inp();
solve();
dsp();
return 0;
}