趣味比赛
cancan123456
·
·
个人记录
问题 1。你需要维护两个长 n 序列 A_i,B_i,A_i 初始值给出,B_i 初始为 0,种操作:
- 给定 l,r,x,对所有 i\in[l,r],将 A_i 改为 A_i+x。
- 给定 l,r,查询 \sum_{i=l}^r A_i。
- 给定 l,r,查询 \sum_{i=l}^r B_i。
在每次 1 操作后,对于所有 i\in[1,n],将 B_i 改为 A_i。
考虑线段树维护列向量 \begin{bmatrix}\sum B\\\sum A\\\sum1\end{bmatrix},其中 \sum1 表示区间长度。
则 1 操作,需要给 [l,r] 的列向量左乘 \begin{bmatrix}1&0&0\\0&1&x\\0&0&1\end{bmatrix}。
1 操作后,需要给 [1,n] 的列向量左乘 \begin{bmatrix}1&1&0\\0&1&0\\0&0&1\end{bmatrix}。
所以线段树维护一个 3\times3 的矩阵就好。
但是我们真的需要维护 9 个数吗?不需要,注意到这样的矩阵相乘只有 (1,2),(1,3),(2,3) 位置需要记录,所以记 3 个数即可。
问题 2。给定两个长 n 的序列 a,b,Q 次查询,每次查询给定一个区间 [l,r],求 \sum_{l\le p\le q\le r}(\max_{p\le i\le q}a_i)(\max_{p\le i\le q}b_i)。
离线询问,按 r 排序,用线段树维护 A_l=\max_{l\le i\le r}a_i,B_l=\max_{l\le i\le r}b_i,并维护 \sum1,\sum A,\sum B,\sum AB,然后对 \sum AB 套一个历史和即可。
现在的操作就是 A 区间加 x,B 区间加 x,对 AB 求历史和的区间和,维护 \begin{bmatrix}\sum_h\sum AB\\\sum AB\\\sum A\\\sum B\\\sum1\end{bmatrix}。
$$\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}$$
$B$ 区间加 $x$:
$$\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}$$
求历史和:
$$\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}$$
按照上面的想法,我们需要维护 $10$ 个数,但是并非如此。
手玩可以发现,$(3,4)$ 一定是 $0$,$(2,3)$ 和 $(4,5)$ 一定相等,$(2,4)$ 和 $(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())
```
所以矩阵一定长这个样:
$$\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+Q\log n)$。
```cpp
#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;
}
```