跳到正文

个人记录

SPOJ GSS 题解

cancan123456约 46 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年7月24日
篇幅
约 46 分钟

版本

  1. v1当前

GSS1

题意:

给你一个长为 nn 的序列,qq 次询问,询问区间 [l,r][l,r] 中的最大子段和,不可以不选。

题解:

用线段树解决,每个节点保存四个信息:lmax,rmax,tmax,sumlmax, rmax, tmax, sum,分别表示该区间的最大前缀和,最大后缀和,最大子段和,以及区间和。

合并:

首先考虑 lmaxlmax,有两种情况:

第一,完全位于左子区间内;

第二,延伸到了右子区间。

那么对这两种情况取个 max⁡\max,我们可以得到:

lmax=max⁡(lc.lmax,lc.sum+rc.lmax)lmax=\max(lc.lmax,lc.sum+rc.lmax)

注释:lclc 为左子节点,rcrc 为右子节点。

同理,我们可以得到:

rmax=max⁡(rc.rmax,rc.sum+lc.rmax)rmax=\max(rc.rmax,rc.sum+lc.rmax)

tmax=max⁡(lc.tmax,rc.tmax,lc.rmax+rc.lmax)tmax=\max(lc.tmax,rc.tmax,lc.rmax+rc.lmax)

sum=lc.sum+rc.sumsum=lc.sum+rc.sum

如果区间只有一个数,那么:

lmax=rmax=tmax=sum=xlmax=rmax=tmax=sum=x

然后就可以愉快的用线段树解决了!

GSS3

和 GSS1 类似,不过多了个单点修改。

就是小白逛公园了,只需要递归修改,然后交给 push_up 即可。

GSS7

把 GSS3 搬到了树上,把单点修改变成了链上覆盖,并且可以不选数。

保证不炸 long long,但可能炸 int。

首先看到这种数树上链查链改的问题,先打个树剖出来。

然后对于链覆盖,我们用一个懒标记存下来就可以了。

对于链查询,有一些小细节,我们先扔一下代码:

C++
Result chain_query(int u, int v) {
	Result L, R;
	while (top[u] != top[v]) {
		if (dep[top[u]] > dep[top[v]]) {
			L = query(1, dfn[top[u]], dfn[u]) + L;
			u = fa[top[u]];
		} else {
			R = query(1, dfn[top[v]], dfn[v]) + R;
			v = fa[top[v]];
		}
	}
	if (dep[u] < dep[v]) {
		R = query(1, dfn[u], dfn[v]) + R;
	} else {
		L = query(1, dfn[v], dfn[u]) + L;
	}
	swap(L.lmax, L.rmax);
	return L + R;
}

这里我们不能像普通的树剖那样,必须要用两个变量维护从 u, v 跳上来的路径的信息。

然后我们需要 swap(L.lmax, L.rmax),这是因为我们 L 的左端在 u,R 的左端在 v,要是直接拼起来的话就是右端对右端,所以我们把 L 翻转一下就可以了。

GSS2

和 GSS1 一样,不过……

相同的数只算一次。

题目瞬间就变难了。

我们考虑如何做区间去重,预处理 preipre_i 表示前一个与 aia_i 相同的数的下标,把询问离线,按照右端点排序。

然后如果处理到了右端点为 ii 的询问,我们把区间 [prei,i][pre_i,i] 都加上 aia_i,这样就实现了自动去重。

线段树每个节点维护四个值:sum,max_sum,sum_lazy_tag,max_sum_lazy_tagsum,max\_sum,sum\_lazy\_tag,max\_sum\_lazy\_tag:

对于叶子结点:

  • sumsum 表示从当前节点到当前询问右端点的和。

  • max_summax\_sum 表示 sum 的历史最大值,初始为 0。

  • sum_lazy_tagsum\_lazy\_tag 和 max_sum_lazy_tagmax\_sum\_lazy\_tag 是懒标记,实际无意义(都是叶子结点了)。

对于中层节点:

  • sumsum 表示左子节点的 sumsum 和右子节点的 sumsum 的最大值。

  • max_summax\_sum 和 sumsum 类似。

  • sum_lazy_tagsum\_lazy\_tag 和 max_sum_lazy_tagmax\_sum\_lazy\_tag 是懒标记。

那么很明显的,这个历史最大值就是我们要求的最大子段和。

然后下传标记的时候注意顺序。

GSS5

给定一个序列,问左端点在 [l1,r1][l_1,r_1] 中,右端点在 [l2,r2][l_2,r_2] 中的子序列的最大和,[l1,r1][l_1,r_1] 与 [l2,r2][l_2,r_2] 可能重合,不允许不取。

多组数据,最多 55 组。

首先我们考虑不重合的情况:

代码
---+----+-----------+-----+-----
   l1   r1          l2    r2

那么我们发现:(r1,l2)(r_1,l_2) 这段区间是必须选的,然后我们加上 [l1,r1][l_1,r_1] 的 rmaxrmax 和 [l2,r2][l_2,r_2] 的 lmaxlmax 就可以得到答案了。

然后我们考虑重合的情况:

代码
---+-------+--------+-----+-----
   l1      l2       r1    r2

那么这时有 44 种情况:

  1. 左右端点都在 [l2,r1][l_2,r_1] 中:返回 [l2,r1][l_2,r_1] 的 tmaxtmax 即可。

  2. 左端点在 [l1,l2)[l_1,l_2) 中,右端点在 [l2,r1] [l_2,r_1] 中:返回 [l1,l2)[l_1,l_2) 的 rmaxrmax 加 [l2,r1][l_2,r_1] 的 lmaxlmax 即可。

  3. 左端点在 [l1,r1][l_1,r_1] 中,右端点在 (r1,r2](r_1,r_2] 中:返回 [l1,r1][l_1,r_1] 的 rmaxrmax 加 (r1,r2](r_1,r_2] 的 lmaxlmax 即可。

  4. 左端点在 [l1,l2)[l_1,l_2) 中,右端点在 (r1,r2](r_1,r_2] 中:返回 [l1,l2)[l_1,l_2) 的 rmaxrmax 加 [l2,r1][l_2,r_1] 的区间和加 (r1,r2](r_1,r_2] 的 lmaxlmax 即可。

然后我们可以发现:其实情况 2,3 包含了情况 4,自己画画图就知道了。

另外注意特判边界情况,比如当 l1=l2l_1=l_2 时查询区间 [l1,l2)[l_1,l_2) 的时候可能会炸掉,在线段树的 query 函数判断即可。

GSS6

维护一个序列,支持:插入元素,删除元素,修改元素,区间最大子段和。

平衡树维护即可,这里我用的是 fhqTreap。

注意平衡树并不是 leafy 的,也就是合并信息的时候要考虑此节点的信息,就像这样(valval 是当前节点的权值):

lmax=max⁡(lc.lmax,lc.sum+val+rc.lmax)lmax=\max(lc.lmax,lc.sum+val+rc.lmax)

rmax=max⁡(rc.rmax,lc.rmax+val+rc.sum)rmax=\max(rc.rmax,lc.rmax+val+rc.sum)

tmax=max⁡(lc.tmax,rc.tmax,lc.rmax+val+rc.lmax)tmax=\max(lc.tmax,rc.tmax,lc.rmax+val+rc.lmax)

sum=lc.sum+val+rc.sumsum=lc.sum+val+rc.sum

注意这里 lmax,rmaxlmax,rmax 可以不选,就是必须大于等于 0,但是 tmaxtmax 必须要选。

还有注意平衡树的节点有可能达到 2×1052\times10^5,空间要开够了。

然后为了方便实现、我们把 split 函数写成按照大小分裂的。

剩下的 GSS4 和 GSS8 就和最大子段和没什么关系了。

GSS4

给你一个序列,都是正整数,要求维护区间开根和区间求和。多组数据,读到 EOF 结束。

首先按几下计算器可以发现:

1018→109→31622→177→13→3→1→1→⋯10^{18}\rightarrow10^9\rightarrow31622\rightarrow177\rightarrow13\rightarrow3\rightarrow1\rightarrow1\rightarrow\cdots

那么我们发现 101810^{18} 开根 66 次就变成 11 了,那么我们考虑用线段树维护这个序列,区间开根的时候递归,如果遇到一个区间已经全变成 11 了就不递归,又因为这个题只有整数,所以如果区间和等于区间数的个数我们就可以判断区间内只有 11 了。

还有就是注意……

不保证给出的区间 [x,y][x,y] 有 x≤yx\le y,如果 x>yx>y 请交换 x,yx,y。

双倍经验:P4145。

GSS8

维护一个序列 aa,支持插入、删除、替换,以及查询:

∑i=lrai(i−l+1)k\sum\limits_{i=l}^ra_i(i-l+1)^k

模 2322^{32} 的值,其中 k≤10k\le10。

考虑使用平衡树维护。

发现 k≤10k\le10,可以存下所有的 kk 对应的答案。

我们推一下式子:

∑i=lrai(i−l+1)k\displaystyle\sum\limits_{i=l}^ra_i(i-l+1)^k 为我们需要求出的东西(当前节点的答案)。

∑i=lmid−1ai(i−l+1)k\displaystyle\sum\limits_{i=l}^{mid-1}a_i(i-l+1)^k 为左子节点的答案。

∑i=mid+1rai[i−(mid+1)+1]k=∑i=mid+1rai(i−mid)k\displaystyle\sum\limits_{i=mid+1}^ra_i[i-(mid+1)+1]^k=\sum\limits_{i=mid+1}^ra_i(i-mid)^k 为右子节点的答案。

∑i=lrai(i−l+1)k=∑i=mid−1rai(i−l+1)k+amid(mid−l+1)k+∑i=mid+1rai(i−l+1)k\sum_{i=l}^ra_i(i-l+1)^k=\sum_{i=mid-1}^ra_i(i-l+1)^k+a_{mid}(mid-l+1)^k+\sum_{i=mid+1}^ra_i(i-l+1)^k

前面两项倒还好说,关键是这个 ∑i=mid+1rai(i−l+1)k\displaystyle\sum_{i=mid+1}^ra_i(i-l+1)^k 怎么办。

我们观察右子节点的答案 ∑i=mid+1rai(i−mid)k\displaystyle\sum\limits_{i=mid+1}^ra_i(i-mid)^k,发现只有 (i−mid)(i-mid) 和 (mid−l+1)(mid-l+1) 不一样,考虑处理:

∑i=mid+1rai(i−l+1)k=∑i=mid+1rai[(i−mid)+(mid−l+1)]k=∑i=mid+1rai∑j=0k(jk)(i−mid)j(mid−l+1)j−k=∑j=0k(jk)(mid−l+1)j−k∑i=mid+1rai(i−mid)j\begin{aligned}\sum\limits_{i=mid+1}^ra_i(i-l+1)^k&=\sum\limits_{i=mid+1}^ra_i[(i-mid)+(mid-l+1)]^k\\&=\sum_{i=mid+1}^ra_i\sum_{j=0}^k\binom jk(i-mid)^j(mid-l+1)^{j-k}\\&=\sum_{j=0}^k\binom jk(mid-l+1)^{j-k}\sum_{i=mid+1}^ra_i(i-mid)^j\end{aligned}

我们发现后面的部分就是右子节点的信息,然后预处理组合数,把前面的部分计算了即可,这样就可以做到 O(K2)O(K^2) 转移了。

最后注意下标从 00 开始,这样我们取出区间的时候应该 split(r + 1) 再 split(l)。

所有题的代码

C++
// GSS1
#include <cstdio>
using namespace std;
const int N = 50005;
int a[N];
int max(int a, int b) {
	return a > b ? a : b;
}
struct Result {
	int lmax, rmax, tmax, sum;
	Result(int l = 0, int r = 0, int t = 0, int s = 0) {
		lmax = l;
		rmax = r;
		tmax = t;
		sum = s;
	}
};
Result operator + (const Result & lc, const Result & rc) {
	return Result(max(lc.lmax, lc.sum + rc.lmax), max(rc.rmax, rc.sum + lc.rmax), max(max(lc.tmax, rc.tmax), lc.rmax + rc.lmax), lc.sum + rc.sum);
}
struct Node {
	int l, r;
	Result res;
} node[4 * N];
void push_up(int p) {
	node[p].ans = node[2 * p].res + node[2 * p + 1].res;
}
void build(int p, int l, int r) {
	node[p].l = l;
	node[p].r = r;
	if (l == r) {
		node[p].ans = Result(a[l], a[l], a[l], a[l]);
	} else {
		int mid = (l + r) / 2;
		build(2 * p, l, mid);
		build(2 * p + 1, mid + 1, r);
		push_up(p);
	}
}
Result Query(int p, int l, int r) {
	if (l <= node[p].l && node[p].r <= r) {
		return node[p].res;
	} else {
		int mid = (node[p].l + node[p].r) / 2;
		if (r <= mid) {
			return Query(2 * p, l, r);
		} else if (mid + 1 <= l) {
			return Query(2 * p + 1, l, r);
		} else {
			return Query(2 * p, l, r) + Query(2 * p + 1, l, r);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	build(1, 1, n);
	int m;
	scanf("%d", &m);
	for (int l, r, i = 1; i <= m; i++) {
		scanf("%d %d", &l, &r);
		printf("%d\n", Query(1, l, r).tmax);
	}
	return 0;
}
// GSS3
#include <cstdio>
using namespace std;
const int N = 50005;
int a[N];
int max(int a, int b) {
	return a > b ? a : b;
}
struct Result {
	int lmax, rmax, tmax, sum;
	Result(int l = 0, int r = 0, int t = 0, int s = 0) {
		lmax = l;
		rmax = r;
		tmax = t;
		sum = s;
	}
};
Result operator + (const Result & lc, const Result & rc) {
	return Result(max(lc.lmax, lc.sum + rc.lmax), max(rc.rmax, rc.sum + lc.rmax), max(max(lc.tmax, rc.tmax), lc.rmax + rc.lmax), lc.sum + rc.sum);
}
struct Node {
	int l, r;
	Result ans;
} node[4 * N];
void push_up(int p) {
	node[p].ans = node[2 * p].ans + node[2 * p + 1].ans;
}
void build(int p, int l, int r) {
	node[p].l = l;
	node[p].r = r;
	if (l == r) {
		node[p].ans = Result(a[l], a[l], a[l], a[l]);
	} else {
		int mid = (l + r) / 2;
		build(2 * p, l, mid);
		build(2 * p + 1, mid + 1, r);
		push_up(p);
	}
}
void Modify(int p, int x, int y) {
	if (node[p].l == node[p].r) {
		node[p].ans = Answer(y, y, y, y);
	} else {
		int mid = (node[p].l + node[p].r) / 2;
		if (x <= mid) {
			Modify(2 * p, x, y);
		} else {
			Modify(2 * p + 1, x, y);
		}
		push_up(p);
	}
}
Result Query(int p, int l, int r) {
	if (l <= node[p].l && node[p].r <= r) {
		return node[p].ans;
	} else {
		int mid = (node[p].l + node[p].r) / 2;
		if (r <= mid) {
			return Query(2 * p, l, r);
		} else if (mid + 1 <= l) {
			return Query(2 * p + 1, l, r);
		} else {
			return Query(2 * p, l, r) + Query(2 * p + 1, l, r);
		}
	}
}
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	build(1, 1, n);
	int m;
	scanf("%d", &m);
	for (int op, x, y, i = 1; i <= m; i++) {
		scanf("%d %d %d", &op, &x, &y);
		if (op == 0) {
			Modify(1, x, y);
		} else {
			printf("%d\n", Query(1, x, y).tmax);
		}
	}
	return 0;
}
// GSS7
#include <cstdio>
using namespace std;
const int N = 100005;
int a[N];
struct Edge {
	int v, next;
} edge[2 * N];
int head[N];
int cnt;
void add_edge(int u, int v) {
	cnt++;
	edge[cnt].v = v;
	edge[cnt].next = head[u];
	head[u] = cnt;
}
int size[N], son[N], dep[N], fa[N];
void dfs1(int u, int f) {
	fa[u] = f;
	dep[u] = dep[f] + 1;
	size[u] = 1;
	for (int v, i = head[u]; i != 0; i = edge[i].next) {
		v = edge[i].v;
		if (v != f) {
			dfs1(v, u);
			size[u] += size[v];
			if (size[v] > size[son[u]]) {
				son[u] = v;
			}
		}
	}
}
int top[N], dfn[N], dfncnt, b[N];
void dfs2(int u, int toplink) {
	top[u] = toplink;
	dfn[u] = ++dfncnt;
	b[dfn[u]] = a[u];
	if (son[u] != 0) {
		dfs2(son[u], toplink);
		for (int v, i = head[u]; i != 0; i = edge[i].next) {
			v = edge[i].v;
			if (v != son[u] && v != fa[u]) {
				dfs2(v, v);
			}
		}
	}
}
int max(int a, int b) {
	return a > b ? a : b;
}
struct Result {
	int lmax, rmax, tmax, sum;
	Result(int l = 0, int r = 0, int t = 0, int s = 0) {
		lmax = l;
		rmax = r;
		tmax = t;
		sum = s;
	}
};
Result operator + (const Result & a, const Result & b) {
	return Result(max(a.lmax, a.sum + b.lmax), max(b.rmax, b.sum + a.rmax), max(max(a.tmax, b.tmax), a.rmax + b.lmax), a.sum + b.sum);
}
struct Node {
	int l, r;
	int cover;
	bool have_covered;
	Result res;
} node[4 * N];
void push_up(int p) {
	node[p].res = node[2 * p].res + node[2 * p + 1].res;
}
void push_lazy_tag(int p, int tag) {
	node[p].have_covered = true;
	node[p].cover = tag;
	int section_sum = (node[p].r - node[p].l + 1) * tag;
	int max_sum = max(section_sum, 0);
	node[p].res = Result(max_sum, max_sum, max_sum, section_sum);
}
void push_down(int p) {
	if (node[p].have_covered) {
		push_lazy_tag(2 * p, node[p].cover);
		push_lazy_tag(2 * p + 1, node[p].cover);
		node[p].cover = 0;
		node[p].have_covered = false;
	}
}
void build(int p, int l, int r) {
	node[p].l = l;
	node[p].r = r;
	node[p].cover = 0;
	node[p].have_covered = false;
	if (l == r) {
		int best = max(b[l], 0);
		node[p].res = Result(best, best, best, b[l]);
	} else {
		int mid = (l + r) / 2;
		build(2 * p, l, mid);
		build(2 * p + 1, mid + 1, r);
		push_up(p);
	}
}
void cover(int p, int l, int r, int w) {
	if (l <= node[p].l && node[p].r <= r) {
		push_lazy_tag(p, w);
	} else {
		push_down(p);
		int mid = (node[p].l + node[p].r) / 2;
		if (l <= mid) {
			cover(2 * p, l, r, w);
		}
		if (mid + 1 <= r) {
			cover(2 * p + 1, l, r, w);
		}
		push_up(p);
	}
}
Result query(int p, int l, int r) {
	if (l <= node[p].l && node[p].r <= r) {
		return node[p].res;
	} else {
		push_down(p);
		int mid = (node[p].l + node[p].r) / 2;
		Result res;
		if (l <= mid) {
			res = res + query(2 * p, l, r);
		}
		if (mid + 1 <= r) {
			res = res + query(2 * p + 1, l, r);
		}
		return res;
	}
}
void swap(int & a, int & b) {
	a ^= b ^= a ^= b;
}
void chain_cover(int u, int v, int w) {
	while (top[u] != top[v]) {
		if (dep[top[u]] < dep[top[v]]) {
			swap(u, v);
		}
		cover(1, dfn[top[u]], dfn[u], w);
		u = fa[top[u]];
	}
	if (dep[u] > dep[v]) {
		swap(u, v);
	}
	cover(1, dfn[u], dfn[v], w);
}
Result chain_query(int u, int v) {
	Result L, R;
	while (top[u] != top[v]) {
		if (dep[top[u]] > dep[top[v]]) {
			L = query(1, dfn[top[u]], dfn[u]) + L;
			u = fa[top[u]];
		} else {
			R = query(1, dfn[top[v]], dfn[v]) + R;
			v = fa[top[v]];
		}
	}
	if (dep[u] < dep[v]) {
		R = query(1, dfn[u], dfn[v]) + R;
	} else {
		L = query(1, dfn[v], dfn[u]) + L;
	}
	swap(L.lmax, L.rmax);
	return L + R;
}
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", a + i);
	}
	for (int u, v, i = 1; i < n; i++) {
		scanf("%d %d", &u, &v);
		add_edge(u, v);
		add_edge(v, u);
	}
	dfs1(1, 0);
	dfs2(1, 1);
	build(1, 1, n);
	int m;
	scanf("%d", &m);
	for (int op, u, v, w, i = 1; i <= m; i++) {
		scanf("%d %d %d", &op, &u, &v);
		if (op == 1) {
			printf("%d\n", chain_query(u, v).tmax);
		} else {
			scanf("%d", &w);
			chain_cover(u, v, w);
		}
	}
	return 0;
}
// GSS2
#include <cstdio>
#include <algorithm>
#include <map>
using namespace std;
typedef long long ll;
const int N = 100005;
ll a[N];
int pre[N];
map < ll , int > pos;
ll max(ll a, ll b) {
	return a > b ? a : b;
}
struct Result {
	ll sum, max_sum, sum_lazy_tag, max_sum_lazy_tag;
	Result() {
		sum = max_sum = sum_lazy_tag = max_sum_lazy_tag = 0;
	}
};
Result operator + (const Result & a, const Result & b) {
	Result res;
	res.sum = max(a.sum, b.sum);
	res.max_sum = max(a.max_sum, b.max_sum);
	return res;
}
struct Node {
	int l, r;
	Result res;
} node[4 * N];
void push_up(int p) {
	node[p].res = node[2 * p].res + node[2 * p + 1].res;
}
void push_down(int p) {
	node[2 * p].res.max_sum = max(node[2 * p].res.max_sum, node[2 * p].res.sum + node[p].res.max_sum_lazy_tag);
	node[2 * p].res.sum += node[p].res.sum_lazy_tag;
	node[2 * p].res.max_sum_lazy_tag = max(node[2 * p].res.max_sum_lazy_tag, node[2 * p].res.sum_lazy_tag + node[p].res.max_sum_lazy_tag);
	node[2 * p].res.sum_lazy_tag += node[p].res.sum_lazy_tag;
	node[2 * p + 1].res.max_sum = max(node[2 * p + 1].res.max_sum, node[2 * p + 1].res.sum + node[p].res.max_sum_lazy_tag);
	node[2 * p + 1].res.sum += node[p].res.sum_lazy_tag;
	node[2 * p + 1].res.max_sum_lazy_tag = max(node[2 * p + 1].res.max_sum_lazy_tag, node[2 * p + 1].res.sum_lazy_tag + node[p].res.max_sum_lazy_tag);
	node[2 * p + 1].res.sum_lazy_tag += node[p].res.sum_lazy_tag;
	node[p].res.sum_lazy_tag = node[p].res.max_sum_lazy_tag = 0;
}
void build(int p, int l, int r) {
	node[p].l = l;
	node[p].r = r;
	if (l != r) {
		int mid = (l + r) / 2;
		build(2 * p, l, mid);
		build(2 * p + 1, mid + 1, r);
	}
}
void add(int p, int l, int r, ll w) {
	if (l <= node[p].l && node[p].r <= r) {
		node[p].res.sum += w;
		node[p].res.max_sum = max(node[p].res.max_sum, node[p].res.sum);
		node[p].res.sum_lazy_tag += w;
		node[p].res.max_sum_lazy_tag = max(node[p].res.sum_lazy_tag, node[p].res.max_sum_lazy_tag);
	} else {
		push_down(p);
		int mid = (node[p].l + node[p].r) / 2;
		if (l <= mid) {
			add(2 * p, l, r, w);
		}
		if (mid + 1 <= r) {
			add(2 * p + 1, l, r, w);
		}
		push_up(p);
	}
}
Result query(int p, int l, int r) {
	if (l <= node[p].l && node[p].r <= r) {
		return node[p].res;
	} else {
		push_down(p);
		int mid = (node[p].l + node[p].r) / 2;
		Result res;
		if (l <= mid) {
			res = res + query(2 * p, l, r);
		}
		if (mid + 1 <= r) {
			res = res + query(2 * p + 1, l, r);
		}
		return res;
	}
}
struct Query {
	int l, r, id;
} q[N];
ll ans[N];
bool operator < (const Query & a, const Query & b) {
	return a.r < b.r;
}
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) {
		scanf("%lld", a + i);
		if (pos.find(a[i]) != pos.end()) {
			pre[i] = pos[a[i]];
		} else {
			pre[i] = 0;
		}
		pos[a[i]] = i;
	}
	build(1, 1, n);
	int m;
	scanf("%d", &m);
	for (int i = 1; i <= m; i++) {
		scanf("%d %d", &q[i].l, &q[i].r);
		q[i].id = i;
	}
	sort(q + 1, q + m + 1);
	for (int now = 1, i = 1; i <= n; i++) {
		add(1, pre[i] + 1, i, a[i]);
		while (now <= m && q[now].r <= i) {
			ans[q[now].id] = query(1, q[now].l, q[now].r).max_sum;
			now++;
		}
	}
	for (int i = 1; i <= m; i++) {
		printf("%lld\n", ans[i]);
	}
	return 0;
}
// GSS5
#include <cstdio>
using namespace std;
const int N = 10005;
int a[N];
int max(int a, int b) {
	return a > b ? a : b;
}
struct Result {
	int lmax, rmax, tmax, sum;
	Result(int l = 0, int r = 0, int t = 0, int s = 0) {
		lmax = l;
		rmax = r;
		tmax = t;
		sum = s;
	}
};
Result operator + (const Result & a, const Result & b) {
	return Result(max(a.lmax, a.sum + b.lmax), max(b.rmax, a.rmax + b.sum), max(max(a.tmax, b.tmax), a.rmax + b.lmax), a.sum + b.sum);
}
struct Node {
	int l, r;
	Result res;
} node[4 * N];
void build(int p, int l, int r) {
	node[p].l = l;
	node[p].r = r;
	if (l == r) {
		node[p].res = Result(a[l], a[l], a[l], a[l]);
	} else {
		int mid = (l + r) / 2;
		build(2 * p, l, mid);
		build(2 * p + 1, mid + 1, r);
		node[p].res = node[2 * p].res + node[2 * p + 1].res;
	}
}
Result query(int p, int l, int r) {
	if (l > r) {
		return Result(0, 0, 0, 0);
	} else if (l <= node[p].l && node[p].r <= r) {
		return node[p].res;
	} else {
		int mid = (node[p].l + node[p].r) / 2;
		if (mid + 1 <= l) {
			return query(2 * p + 1, l, r);
		} else if (r <= mid) {
			return query(2 * p, l, r);
		} else {
			return query(2 * p, l, r) + query(2 * p + 1, l, r);
		}
	}
}
int get(int l1, int r1, int l2, int r2) {
	if (r1 < l2) {
		int ans = 0;
		ans += query(1, l1, r1).rmax;
		ans += query(1, r1 + 1, l2 - 1).sum;
		ans += query(1, l2, r2).lmax;
		return ans;
	} else {
		int ans = 0;
		ans = query(1, l2, r1).tmax;
		if (l1 < l2) {
			ans = max(ans, query(1, l1, l2 - 1).rmax + query(1, l2, r2).lmax);
		}
		if (r1 < r2) {
			ans = max(ans, query(1, l1, r1).rmax + query(1, r1 + 1, r2).lmax);
		}
		return ans;
	}
}
int main() {
	int t;
	scanf("%d", &t);
	for (; t != 0; t--) {
		int n;
		scanf("%d", &n);
		for (int i = 1; i <= n; i++) {
			scanf("%d", a + i);
		}
		build(1, 1, n);
		int m;
		scanf("%d", &m);
		for (int l1, r1, l2, r2, i = 1; i <= m; i++) {
			scanf("%d %d %d %d", &l1, &r1, &l2, &r2);
			printf("%d\n", get(l1, r1, l2, r2));
		}
	}
	return 0;
}
// GSS6
#include <iostream>
#include <cstdlib>
using namespace std;
const int N = 100005;
const int M = 200005;
int max(int a, int b) {
	return a > b ? a : b;
}
int pri[M], val[M], size[M], ch[M][2], cnt;
int lmax[M], rmax[M], tmax[M], sum[M];
void push_up(int p) {
	int lc = ch[p][0];
	int rc = ch[p][1];
	lmax[p] = max(lmax[lc], sum[lc] + val[p] + lmax[rc]);
	rmax[p] = max(rmax[rc], rmax[lc] + val[p] + sum[rc]);
	tmax[p] = max(max(tmax[lc], tmax[rc]), rmax[lc] + val[p] + lmax[rc]);
	sum[p] = sum[lc] + val[p] + sum[rc];
	size[p] = size[lc] + size[rc] + 1;
}
int new_node(int x) {
	cnt++;
	pri[cnt] = rand();
	val[cnt] = x;
	size[cnt] = 1;
	lmax[cnt] = rmax[cnt] = max(x, 0);
	tmax[cnt] = sum[cnt] = x;
	return cnt;
}
int merge(int x, int y) {
	if (x == 0 || y == 0) {
		return x + y;
	} else {
		if (pri[x] < pri[y]) {
			ch[x][1] = merge(ch[x][1], y);
			push_up(x);
			return x;
		} else {
			ch[y][0] = merge(x, ch[y][0]);
			push_up(y);
			return y;
		}
	}
}
void split(int p, int k, int & u, int & v) {
	if (p == 0) {
		u = v = 0;
	} else {
		if (k <= size[ch[p][0]]) {
			v = p;
			split(ch[p][0], k, u, ch[v][0]);
		} else {
			u = p;
			split(ch[p][1], k - size[ch[p][0]] - 1, ch[u][1], v);
		}
		push_up(p);
	}
}
int a[N];
int main() {
	int n;
	scanf("%d", &n);
	int root = 0;
	tmax[0] = -0x7fffffff - 1;
	for (int i = 1; i <= n; i++) {
		scanf("%d", a + i);
		root = merge(root, new_node(a[i]));
	}
	int m;
	scanf("%d", &m);
	char op;
	int p1, p2, p3;
	for (int x, y, i = 1; i <= m; i++) {
		cin >> op;
		if (op == 'I') {
			scanf("%d %d", &x, &y);
			split(root, x - 1, p1, p2);
			root = merge(p1, merge(new_node(y), p2));
		} else if (op == 'D') {
			scanf("%d", &x);
			split(root, x - 1, p1, p2);
			split(p2, 1, p2, p3);
			root = merge(p1, p3);
		} else if (op == 'R') {
			scanf("%d %d", &x, &y);
			split(root, x - 1, p1, p2);
			split(p2, 1, p2, p3);
			val[p2] = y;
			push_up(p2);
			root = merge(p1, merge(p2, p3));
		} else {
			scanf("%d %d", &x, &y);
			split(root, y, p1, p2);
			split(p1, x - 1, p1, p3);
			printf("%d\n", tmax[p3]);
			root = merge(p1, merge(p3, p2));
		}
	}
	return 0;
}
// GSS4
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
const int N = 100005;
ll a[N];
struct Node {
	int l, r;
	ll sum;
} node[4 * N];
void push_up(int p) {
	node[p].sum = node[2 * p].sum + node[2 * p + 1].sum;
}
void build(int p, int l, int r) {
	node[p].l = l;
	node[p].r = r;
	if (l == r) {
		node[p].sum = a[l];
	} else {
		int mid = (l + r) / 2;
		build(2 * p, l, mid);
		build(2 * p + 1, mid + 1, r);
		push_up(p);
	}
}
void do_sqrt(int p, int l, int r) {
	if (node[p].sum == node[p].r - node[p].l + 1) {
		return;
	} else if (node[p].l == node[p].r) {
		int x = node[p].l;
		a[x] = sqrt(a[x]);
		node[p].sum = a[x];
	} else {
		int mid = (node[p].l + node[p].r) / 2;
		if (l <= mid) {
			do_sqrt(2 * p, l, r);
		}
		if (mid + 1 <= r) {
			do_sqrt(2 * p + 1, l, r);
		}
		push_up(p);
	}
}
ll query(int p, int l, int r) {
	if (l <= node[p].l && node[p].r <= r) {
		return node[p].sum;
	} else {
		int mid = (node[p].l + node[p].r) / 2;
		ll ans = 0;
		if (l <= mid) {
			ans += query(2 * p, l, r);
		}
		if (mid + 1 <= r) {
			ans += query(2 * p + 1, l, r);
		}
		return ans;
	}
}
int main() {
	int number = 1;
	int n;
	while (scanf("%d", &n) != EOF) {
		printf("Case #%d:\n", number);
		for (int i = 1; i <= n; i++) {
			scanf("%lld", a + i);
		}
		build(1, 1, n);
		int m;
		scanf("%d", &m);
		for (int op, l, r, i = 1; i <= m; i++) {
			scanf("%d %d %d", &op, &l, &r);
			if (l > r) {
				l ^= r ^= l ^= r;
			}
			if (op == 0) {
				do_sqrt(1, l, r);
			} else {
				printf("%lld\n", query(1, l, r));
			}
		}
		number++;
		putchar(10);
	}
	return 0;
}
// GSS8
#include <iostream>
#include <cstdlib>
using namespace std;
const int N = 200005;
unsigned C[11][11];
void pre_work() {
	for (int i = 0; i <= 10; i++) {
		C[i][0] = 1;
		for (int j = 1; j <= i; j++) {
			C[i][j] = C[i - 1][j - 1] + C[i - 1][j];
		}
	}
}
int pri[N], val[N], size[N], ch[N][2], cnt;
unsigned res[N][11];
int new_node(unsigned x) {
	cnt++;
	pri[cnt] = rand();
	size[cnt] = 1;
	val[cnt] = x;
	for (int i = 0; i <= 10; i++) {
		res[cnt][i] = x;
	}
	return cnt;
}
unsigned g[11];
// g[i] 表示 (mid - l + 1) ^ i
void push_up(int p) {
	int lc = ch[p][0];
	int rc = ch[p][1];
	size[p] = size[lc] + size[rc] + 1;
	g[0] = 1;
	for (int i = 1; i <= 10; i++) {
		g[i] = g[i - 1] * (size[lc] + 1);
	}
	for (int k = 0; k <= 10; k++) {
		res[p][k] = res[lc][k] + val[p] * g[k];
		for (int j = 0; j <= k; j++) {
			res[p][k] += C[k][j] * g[k - j] * res[rc][j];
		}
	}
}
int merge(int x, int y) {
	if (x == 0 || y == 0) {
		return x + y;
	} else {
		if (pri[x] < pri[y]) {
			ch[x][1] = merge(ch[x][1], y);
			push_up(x);
			return x;
		} else {
			ch[y][0] = merge(x, ch[y][0]);
			push_up(y);
			return y;
		}
	}
}
void split(int p, int k, int & x, int & y) {
	if (p == 0) {
		x = y = 0;
	} else {
		if (k <= size[ch[p][0]]) {
			y = p;
			split(ch[p][0], k, x, ch[y][0]);
		} else {
			x = p;
			split(ch[p][1], k - size[ch[p][0]] - 1, ch[x][1], y);
		}
		push_up(p);
	}
}
unsigned a[N];
int main() {
	pre_work();
	int n;
	scanf("%d", &n);
	int root = 0;
	for (int i = 0; i < n; i++) {
		scanf("%u", a + i);
		root = merge(root, new_node(a[i]));
	}
	int q;
	scanf("%d", &q);
	char op;
	int pos;
	unsigned v;
	int l, r, k;
	int p1, p2, p3;
	for (int i = 1; i <= q; i++) {
		cin >> op;
		if (op == 'I') {
			scanf("%d %u", &pos, &v);
			split(root, pos, p1, p2);
			root = merge(p1, merge(new_node(v), p2));
		} else if (op == 'D') {
			scanf("%d", &pos);
			split(root, pos, p1, p2);
			split(p2, 1, p2, p3);
			root = merge(p1, p3);
		} else if (op == 'R') {
			scanf("%d %u", &pos, &v);
			split(root, pos, p1, p2);
			split(p2, 1, p2, p3);
			val[p2] = v;
			push_up(p2);
			root = merge(p1, merge(p2, p3));
		} else {
			scanf("%d %d %d", &l, &r, &k);
			split(root, r + 1, p1, p3);
			split(p1, l, p1, p2);
			printf("%u\n", res[p2][k]);
			root = merge(p1, merge(p2, p3));
		}
	}
	return 0;
}