1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
|
#define NDEBUG
#include <iostream> #include <algorithm> #include <cassert> #include <cstring> #include <iomanip> #include <cstdio> #include <cmath> using namespace std; inline void pass(){return;} inline void hold(){while(1);} #define endl "\n" #define mp(a,b) make_pair(a,b) #define ct(a) while(!a.empty()) a.pop() #define cmax(a,b) a=max(a,b) #define cmin(a,b) a=min(a,b) #define cdb cerr<<"Debug: " #define cwn cerr<<"Warn: " #define debug(x) cerr<<"In Line "<<__LINE__<<": "<<#x<<" = "<<x<<"\n" #define sp(a) fixed<<setprecision(a) #define likely(x) __builtin_expect(!!(x), 1) #define unlikely(x) __builtin_expect(!!(x), 0) #define ll long long #define ull unsigned long long #define ld long double
struct query{ int l,r,id; }qs[100005];
namespace ds{ struct seg{ struct segNode{ int sum,hxsum,lzy,lzymax; }tree[400005]; inline void pushup(int u){ tree[u].sum = max(tree[u*2].sum, tree[u*2+1].sum); tree[u].hxsum = max(tree[u*2].hxsum, tree[u*2+1].hxsum); } inline bool inrange(int l,int r,int ql,int qr){return (ql<=l)&&(r<=qr);} inline bool outofrange(int l,int r,int ql,int qr){return (l>qr)||(r<ql);} inline void maketag(int u,int ul,int ur,int uk){ tree[u].lzy += uk; cmax(tree[u].lzymax, tree[u].lzy); tree[u].sum += uk; cmax(tree[u].hxsum, tree[u].sum); } inline void pushdown(int u){ cmax(tree[u*2].hxsum, tree[u*2].sum+tree[u].lzymax); cmax(tree[u*2+1].hxsum, tree[u*2+1].sum+tree[u].lzymax); tree[u*2].sum += tree[u].lzy; tree[u*2+1].sum += tree[u].lzy; cmax(tree[u*2].lzymax, tree[u*2].lzy+tree[u].lzymax); cmax(tree[u*2+1].lzymax, tree[u*2+1].lzy+tree[u].lzymax); tree[u*2].lzy += tree[u].lzy; tree[u*2+1].lzy += tree[u].lzy; tree[u].lzy = 0; tree[u].lzymax = 0; } int query(int u,int l,int r,int ql,int qr){ if(inrange(l,r,ql,qr)) return tree[u].hxsum; else if(!outofrange(l,r,ql,qr)){ int mid=(l+r)>>1; pushdown(u); return max(query(u*2,l,mid,ql,qr),query(u*2+1,mid+1,r,ql,qr)); } else return 0; } void update(int u,int l,int r,int ul,int ur,int uk){ if(inrange(l,r,ul,ur)) maketag(u,l,r,uk); else if(!outofrange(l,r,ul,ur)){ int mid=(l+r)>>1; pushdown(u); update(u*2,l,mid,ul,ur,uk); update(u*2+1,mid+1,r,ul,ur,uk); pushup(u); } } #ifndef NDEBUG inline void output(int u,int l,int r){ cerr << "On node " << u << ", left " << l << ", right " << r << ", sum=" << tree[u].sum << ", hxsum=" << tree[u].hxsum << ", lzy=" << tree[u].lzy << ", lzymax=" << tree[u].lzymax << ";\n"; if(l==r) return; int mid=(l+r)>>1; output(u*2,l,mid); output(u*2+1,mid+1,r); } #else inline void output(int u,int l,int r){} #endif }; }using ds::seg;
bool cmp(query x,query y){ return x.r<y.r; }
#define OFFSET 100000 int _t,n,q,a[100005],pre[100005],curr[200005],ans[100005]; seg segobj;
void solve(int testID){ cin >> n; for(int i=1;i<=n;i++){ cin >> a[i]; pre[i] = curr[a[i]+OFFSET]; curr[a[i]+OFFSET] = i; } cin >> q; for(int i=1;i<=q;i++){ cin >> qs[i].l >> qs[i].r; qs[i].id = i; } sort(qs+1,qs+q+1,cmp); int j=1; for(int i=1;i<=n;i++){ segobj.update(1,1,n,pre[i]+1,i,a[i]); for(;j<=q&&qs[j].r<=i;j++){ ans[qs[j].id] = segobj.query(1,1,n,qs[j].l,qs[j].r); } } segobj.output(1,1,n); for(int i=1;i<=q;i++) cout<<ans[i]<<"\n"; }
signed main(){ ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
_t = 1;
for(int ran=0;ran<_t;ran++) solve(ran);
fflush(stdout); return 0; }
|