-
Notifications
You must be signed in to change notification settings - Fork 0
/
G.cpp
113 lines (109 loc) · 2.84 KB
/
G.cpp
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <bits/stdc++.h>
using namespace std;
int opt[200010 * 4];
int mn[1 << 4][4 * 200010];
int mx[1 << 4][4 * 200010];
int a[5][200010];
int n, k;
const int inf = 1e9;
void update(int x, int c = 1, int b = 1, int e = n) {
if(b == e) {
for(int i = 0; i < (1 << (k-1)); i++) {
mx[i][c] = a[k-1][b];
mn[i][c] = a[k-1][b];
for(int j = 0; j < k-1; j++) {
if((i >> j) & 1) {
mx[i][c] += a[j][b];
mn[i][c] += a[j][b];
} else {
mx[i][c] -= a[j][b];
mn[i][c] -= a[j][b];
}
}
}
return ;
}
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
if(x <= m) {
update(x, l, b, m);
} else {
update(x, r, m+1, e);
}
opt[c] = max(opt[l], opt[r]);
for(int i = 0; i < (1 << (k-1)); i++) {
mx[i][c] = max(mx[i][l], mx[i][r]);
mn[i][c] = min(mn[i][l], mn[i][r]);
opt[c] = max(opt[c], mx[i][l] - mn[i][r]);
opt[c] = max(opt[c], mx[i][r] - mn[i][l]);
}
}
struct data {
int mx[1 << 4];
int mn[1 << 4];
int opt;
data () {
opt = -inf;
for(int i = 0; i < (1 << (k-1)); i++) {
mx[i] = -inf;
mn[i] = inf;
}
}
};
data query(int x, int y, int c = 1, int b = 1, int e = n) {
if(x <= b && e <= y) {
data ans;
for(int i = 0; i < (1 << (k-1)); i++) {
ans.mx[i] = mx[i][c];
ans.mn[i] = mn[i][c];
ans.opt = opt[c];
}
return ans;
}
if(y < b || e < x) return data();
int m = (b + e) >> 1;
int l = c << 1;
int r = l + 1;
data p = query(x, y, l, b, m);
data q = query(x, y, r, m+1, e);
data ans;
ans.opt = max(p.opt, q.opt);
for(int i = 0; i < (1 << (k-1)); i++) {
ans.mn[i] = min(p.mn[i], q.mn[i]);
ans.mx[i] = max(p.mx[i], q.mx[i]);
ans.opt = max(ans.opt, p.mx[i] - q.mn[i]);
ans.opt = max(ans.opt, q.mx[i] - p.mn[i]);
}
return ans;
}
int main() {
// freopen("in.txt", "r", stdin);
scanf("%d %d", &n, &k);
for(int i = 1; i <= n; i++) {
for(int j = 0; j < k; j++) {
scanf("%d", &a[j][i]);
}
update(i);
}
int q;
scanf("%d", &q);
while(q--) {
int c;
scanf("%d", &c);
if(c == 1) {
int idx;
scanf("%d", &idx);
for(int i = 0; i < k; i++) {
scanf("%d", &a[i][idx]);
}
update(idx);
} else {
int l, r;
scanf("%d %d", &l, &r);
data ans = query(l, r);
printf("%d\n", ans.opt);
}
}
return 0;
}