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
| #include <iostream> #include <map> #include <vector> #define maxn 1000010 #define ll long long using namespace std;
int n, m, a[maxn], pre[maxn]; map<int, int> mp;
#define lc i << 1 #define rc i << 1 | 1 struct Seg { ll mx, tmx, add, tadd; } T[maxn * 4]; inline void maintain(int i) { T[i].mx = max(T[lc].mx, T[rc].mx); T[i].tmx = max(T[lc].tmx, T[rc].tmx); }
inline void Update(int i, ll add, ll tadd) { T[i].tmx = max(T[i].tmx, T[i].mx + tadd); T[i].mx += add; T[i].tadd = max(T[i].tadd, T[i].add + tadd); T[i].add += add; }
inline void pushdown(int i) { ll &add = T[i].add, &tadd = T[i].tadd; Update(lc, add, tadd); Update(rc, add, tadd); add = tadd = 0; }
void update(int i, int l, int r, int L, int R, int v) { if (l > R || r < L) return ; if (L <= l && r <= R) return Update(i, v, v); int m = l + r >> 1; pushdown(i); update(lc, l, m, L, R, v); update(rc, m + 1, r, L, R, v); maintain(i); }
ll query(int i, int l, int r, int L, int R) { if (l > R || r < L) return 0; if (L <= l && r <= R) return T[i].tmx; int m = l + r >> 1; pushdown(i); return max(query(lc, l, m, L, R), query(rc, m + 1, r, L, R)); }
vector<pair<int, int>> A[maxn]; ll ans[maxn];
int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
cin >> n; for (int i = 1; i <= n; ++i) { cin >> a[i]; pre[i] = mp[a[i]]; mp[a[i]] = i; } cin >> m; for (int i = 1; i <= m; ++i) { int x, y; cin >> x >> y; A[y].emplace_back(x, i); } for (int i = 1; i <= n; ++i) { update(1, 1, n, pre[i] + 1, i, a[i]); for (auto [k, id] : A[i]) ans[id] = query(1, 1, n, k, i); } for (int i = 1; i <= m; ++i) cout << ans[i] << "\n"; return 0; }
|