CF1093G - Multidimensional Queries

这题需要用到一个惯用的套路。

$$a - b \leq |a - b|$$

显然如果是 $a - b$ 非负的,直接取等号;如果 $ a - b $ 负数,那么绝对值就是正数,负数一定小于正数。

考虑到 $ 2 ^ 5 ​$ 并不是很大,我们可以暴力枚举每个数的符号(用状态压缩的方式)。然后维护一棵线段树进行修改和查询即可。

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
// =================================
// author: memset0
// date: 2019.01.06 18:16:41
// website: https://memset0.cn/
// =================================
#include <bits/stdc++.h>
#define ll long long
namespace ringo {
template <class T> inline void read(T &x) {
x = 0; register char c = getchar(); register bool f = 0;
while (!isdigit(c)) f ^= c == '-', c = getchar();
while (isdigit(c)) x = x * 10 + c - '0', c = getchar();
if (f) x = -x;
}
template <class T> inline void print(T x) {
if (x < 0) putchar('-'), x = -x;
if (x > 9) print(x / 10);
putchar('0' + x % 10);
}
template <class T> inline void print(T x, char c) { print(x), putchar(c); }

const int N = 2e5 + 10, M = 5;
int n, m, t, ans;

struct node {
int b[M];
} u, a[N];

struct status {
int ans[1 << M];
inline status () {}
inline status (const node &a) {
memset(ans, 0, sizeof(ans));
for (int x = 0; x < (1 << m); x++) {
for (int i = 0; i < m; i++)
ans[x] += (x & (1 << i) ? 1 : -1) * a.b[i];
}
}
} ret;
inline status operator ^ (const status &x, const status &y) {
status ans;
for (int i = 0; i < (1 << m); i++)
ans.ans[i] = std::max(x.ans[i], y.ans[i]);
return ans;
}

struct seg_node {
int l, r, mid;
status x;
} p[N << 2];

void build(int u, int l, int r) {
p[u].l = l, p[u].r = r, p[u].mid = (l + r) >> 1;
if (l == r) return (void)(p[u].x = status(a[l]));
build(u << 1, l, p[u].mid);
build(u << 1 | 1, p[u].mid + 1, r);
p[u].x = p[u << 1].x ^ p[u << 1 | 1].x;
}

void modify(int u, int k, const node &x) {
if (p[u].l == p[u].r) return (void)(p[u].x = status(x));
if (k <= p[u].mid) modify(u << 1, k, x);
else modify(u << 1 | 1, k, x);
p[u].x = p[u << 1].x ^ p[u << 1 | 1].x;
}

status query(int u, int l, int r) {
if (p[u].l == l && p[u].r == r) return p[u].x;
if (r <= p[u].mid) return query(u << 1, l, r);
if (l > p[u].mid) return query(u << 1 | 1, l, r);
return query(u << 1, l, p[u].mid) ^ query(u << 1 | 1, p[u].mid + 1, r);
}

void main() {
read(n), read(m);
for (int i = 1; i <= n; i++)
for (int j = 0; j < m; j++)
read(a[i].b[j]);
build(1, 1, n), read(t);
for (int i = 1, k, l, r, op; i <= t; i++)
if (read(op), op == 1) {
read(k);
for (int i = 0; i < m; i++)
read(u.b[i]);
modify(1, k, u);
} else {
read(l), read(r), ans = 0;
ret = query(1, l, r);
for (int x = 0; x < (1 << m); x++)
ans = std::max(ans, ret.ans[x] + ret.ans[((1 << m) - 1) ^ x]);
print(ans, '\n');
}
}

} signed main() { return ringo::main(), 0; }
Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×