跳到正文

个人记录

趣味比赛

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

文章信息

发布
2024年11月12日
收录
2025年12月12日
更新
2026年11月14日
篇幅
约 11 分钟

版本

  1. v1当前

问题 1。你需要维护两个长 nn 序列 Ai,BiA_i,B_i,AiA_i 初始值给出,BiB_i 初始为 00,种操作:

  1. 给定 l,r,xl,r,x,对所有 i∈[l,r]i\in[l,r],将 AiA_i 改为 Ai+xA_i+x。
  2. 给定 l,rl,r,查询 ∑i=lrAi\sum_{i=l}^r A_i。
  3. 给定 l,rl,r,查询 ∑i=lrBi\sum_{i=l}^r B_i。

在每次 1 操作后,对于所有 i∈[1,n]i\in[1,n],将 BiB_i 改为 AiA_i。

考虑线段树维护列向量 [∑B∑A∑1]\begin{bmatrix}\sum B\\\sum A\\\sum1\end{bmatrix},其中 ∑1\sum1 表示区间长度。

则 1 操作,需要给 [l,r][l,r] 的列向量左乘 [10001x001]\begin{bmatrix}1&0&0\\0&1&x\\0&0&1\end{bmatrix}。

1 操作后,需要给 [1,n][1,n] 的列向量左乘 [110010001]\begin{bmatrix}1&1&0\\0&1&0\\0&0&1\end{bmatrix}。

所以线段树维护一个 3×33\times3 的矩阵就好。

但是我们真的需要维护 99 个数吗?不需要,注意到这样的矩阵相乘只有 (1,2),(1,3),(2,3)(1,2),(1,3),(2,3) 位置需要记录,所以记 33 个数即可。

问题 2。给定两个长 nn 的序列 a,ba,b,QQ 次查询,每次查询给定一个区间 [l,r][l,r],求 ∑l≤p≤q≤r(max⁡p≤i≤qai)(max⁡p≤i≤qbi)\sum_{l\le p\le q\le r}(\max_{p\le i\le q}a_i)(\max_{p\le i\le q}b_i)。

离线询问,按 rr 排序,用线段树维护 Al=max⁡l≤i≤rai,Bl=max⁡l≤i≤rbiA_l=\max_{l\le i\le r}a_i,B_l=\max_{l\le i\le r}b_i,并维护 ∑1,∑A,∑B,∑AB\sum1,\sum A,\sum B,\sum AB,然后对 ∑AB\sum AB 套一个历史和即可。

现在的操作就是 AA 区间加 xx,BB 区间加 xx,对 ABAB 求历史和的区间和,维护 [∑h∑AB∑AB∑A∑B∑1]\begin{bmatrix}\sum_h\sum AB\\\sum AB\\\sum A\\\sum B\\\sum1\end{bmatrix}。

AA 区间加 xx:

[10000010x00010x0001000001]\begin{bmatrix}1&0&0&0&0\\0&1&0&x&0\\0&0&1&0&x\\0&0&0&1&0\\0&0&0&0&1\end{bmatrix}

BB 区间加 xx:

[1000001x00001000001x00001]\begin{bmatrix}1&0&0&0&0\\0&1&x&0&0\\0&0&1&0&0\\0&0&0&1&x\\0&0&0&0&1\end{bmatrix}

求历史和:

[1100001000001000001000001]\begin{bmatrix}1&1&0&0&0\\0&1&0&0&0\\0&0&1&0&0\\0&0&0&1&0\\0&0&0&0&1\end{bmatrix}

按照上面的想法,我们需要维护 1010 个数,但是并非如此。

手玩可以发现,(3,4)(3,4) 一定是 00,(2,3)(2,3) 和 (4,5)(4,5) 一定相等,(2,4)(2,4) 和 (3,5)(3,5) 一定相等。

好吧不是手玩的:

Python
from sympy import *
def f(x):
    return Matrix([[1, 0, 0, 0, 0],
                   [0, 1, 0, x, 0],
                   [0, 0, 1, 0, x],
                   [0, 0, 0, 1, 0],
                   [0, 0, 0, 0, 1]])
def g(x):
    return Matrix([[1, 0, 0, 0, 0],
                   [0, 1, x, 0, 0],
                   [0, 0, 1, 0, 0],
                   [0, 0, 0, 1, x],
                   [0, 0, 0, 0, 1]])
def h():
    return Matrix([[1, 1, 0, 0, 0],
                   [0, 1, 0, 0, 0],
                   [0, 0, 1, 0, 0],
                   [0, 0, 0, 1, 0],
                   [0, 0, 0, 0, 1]])
x1 = Symbol('x1')
x2 = Symbol('x2')
x3 = Symbol('x3')
x4 = Symbol('x4')
x5 = Symbol('x5')
x6 = Symbol('x6')
print(f(x1) * g(x2) * h() * f(x3) * g(x4) * h() * f(x5) * g(x6) * h())

所以矩阵一定长这个样:

[1abcd01efg0010f0001e00001]\begin{bmatrix}1&a&b&c&d\\0&1&e&f&g\\0&0&1&0&f\\0&0&0&1&e\\0&0&0&0&1\end{bmatrix}

然后做完了,时间复杂度为 O(n+Qlog⁡n)O(n+Q\log n)。

C++
#include <cstdio>
#include <vector>
using namespace std;
typedef unsigned long long ull;
const int N = 250005;
struct Tag {
	ull a, b, c, d, e, f, g;
	Tag() {
		a = b = c = d = e = f = g = 0;
	}
	Tag(ull a, ull b, ull c, ull d, ull e, ull f, ull g): a(a), b(b), c(c), d(d), e(e), f(f), g(g) {
	}
};
Tag & operator *= (Tag & a, const Tag & b) {
	a = Tag(a.a + b.a, a.b + a.e * b.a + b.b, a.c + a.f * b.a + b.c, a.d + a.e * b.c + a.f * b.b + a.g * b.a + b.d, a.e + b.e, a.f + b.f, a.e * b.f + a.f * b.e + a.g + b.g);
	return a;
}
struct Info {
	ull a, b, c, d, e;
	Info() {
		a = b = c = d = e = 0;
	}
	Info(ull a, ull b, ull c, ull d, ull e): a(a), b(b), c(c), d(d), e(e) {
	}
};
Info operator + (const Info & a, const Info & b) {
	return Info(a.a + b.a, a.b + b.b, a.c + b.c, a.d + b.d, a.e + b.e);
}
Info & operator *= (Info & a, const Tag & b) {
	a = Info(a.a + a.b * b.a + a.c * b.b + a.d * b.c + a.e * b.d, a.b + a.c * b.e + a.d * b.f + a.e * b.g, a.c + a.e * b.f, a.d + a.e * b.e, a.e);
	return a;
}
struct Node {
	int l, r;
	Info w;
	Tag tag;
} node[4 * N];
void push_up(int p) {
	node[p].w = node[2 * p].w + node[2 * p + 1].w;
}
void push_tag(int p, const Tag & tag) {
	node[p].w *= tag;
	node[p].tag *= tag;
}
void push_down(int p) {
	push_tag(2 * p, node[p].tag);
	push_tag(2 * p + 1, node[p].tag);
	node[p].tag = Tag();
}
void build(int p, int l, int r) {
	node[p].l = l;
	node[p].r = r;
	if (l == r) {
		node[p].w.e = 1;
	} 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 l, int r, const Tag & tag) {
	if (l <= node[p].l && node[p].r <= r) {
		push_tag(p, tag);
	} else {
		push_down(p);
		int mid = (node[p].l + node[p].r) / 2;
		if (l <= mid) {
			modify(2 * p, l, r, tag);
		}
		if (mid + 1 <= r) {
			modify(2 * p + 1, l, r, tag);
		}
		push_up(p);
	}
}
Info query(int p, int l, int r) {
	if (l <= node[p].l && node[p].r <= r) {
		return node[p].w;
	} else {
		push_down(p);
		int mid = (node[p].l + node[p].r) / 2;
		Info 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;
	}
}
ull ans[N];
vector < pair < int, int > > vec[N];
int a[N], stka[N], topa, b[N], stkb[N], topb;
int main() {
	int n;
	scanf("%*d %d", &n);
	build(1, 1, n);
	for (int i = 1; i <= n; i++) {
		scanf("%d", &a[i]);
	}
	for (int i = 1; i <= n; i++) {
		scanf("%d", &b[i]);
	}
	int q;
	scanf("%d", &q);
	for (int l, r, i = 1; i <= q; i++) {
		scanf("%d %d", &l, &r);
		vec[r].push_back(make_pair(l, i));
	}
	for (int i = 1; i <= n; i++) {
		modify(1, i, i, Tag(0, 0, 0, 0, 0, a[i], 0));
		while (topa > 0 && a[stka[topa]] < a[i]) {
			modify(1, stka[topa - 1] + 1, stka[topa], Tag(0, 0, 0, 0, 0, a[i] - a[stka[topa]], 0));
			topa--;
		}
		topa++;
		stka[topa] = i;
		modify(1, i, i, Tag(0, 0, 0, 0, b[i], 0, 0));
		while (topb > 0 && b[stkb[topb]] < b[i]) {
			modify(1, stkb[topb - 1] + 1, stkb[topb], Tag(0, 0, 0, 0, b[i] - b[stkb[topb]], 0, 0));
			topb--;
		}
		topb++;
		stkb[topb] = i;
		modify(1, 1, i, Tag(1, 0, 0, 0, 0, 0, 0));
		for (pair < int, int > que : vec[i]) {
			ans[que.second] = query(1, que.first, i).a;
		}
	}
	for (int i = 1; i <= q; i++) {
		printf("%llu\n", ans[i]);
	}
	return 0;
}
原文
存档评论1 条抓取于