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
| // 더 많은 정보는 42jerrykim.github.io 에서 확인하세요.
#include <bits/stdc++.h>
using namespace std;
static const int MAXN = 1515;
int n;
long long arrv[MAXN][MAXN];
long long dp[MAXN][MAXN];
long long totalSum;
struct BIT {
long long tree[MAXN];
void update(int l, int r, int v){
if(l > r) return;
for(; l <= n; l += l & -l) tree[l] += v;
for(r++; r <= n; r += r & -r) tree[r] -= v;
}
long long query(int x) const {
long long ret = 0;
for(; x; x ^= x & -x) ret += tree[x];
return ret;
}
} bitRow[MAXN];
inline long long getVal(int i, int j){
if(i <= 0 || j <= 0) return 0;
return dp[i][j] + bitRow[i].query(j);
}
void buildDP(){
totalSum = 0;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
dp[i][j] = max(dp[i-1][j], dp[i][j-1]) + arrv[i][j];
totalSum += dp[i][j];
}
}
}
void applyUpdate(int a, int b, int c){
static int s[MAXN], e[MAXN];
// initialize segments
for(int i = a + 1; i <= n; ++i) s[i] = n + 1, e[i] = 0;
s[a] = e[a] = b;
// compute right boundary e[i]
{
int i = a, j = b;
while(true){
if(j < n && max(getVal(i-1, j+1), getVal(i, j)) + c ==
max(getVal(i-1, j+1), getVal(i, j) + c)){
++j;
}else{
++i;
}
if(i > n) break;
e[i] = j;
}
}
// compute left boundary s[i]
{
int i = a, j = b;
while(true){
if(i < n && max(getVal(i+1, j-1), getVal(i, j)) + c ==
max(getVal(i+1, j-1), getVal(i, j) + c)){
++i;
}else{
++j;
}
if(j > n || e[i] < j) break;
s[i] = min(s[i], j);
}
}
// apply row-wise range adds and fix total sum
for(int i = a; i <= n; ++i){
if(s[i] <= e[i]){
bitRow[i].update(s[i], e[i], c);
totalSum += 1LL * c * (e[i] - s[i] + 1);
}
}
}
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
if(!(cin >> n)) return 0;
for(int i = 1; i <= n; ++i){
for(int j = 1; j <= n; ++j){
cin >> arrv[i][j];
}
}
buildDP();
cout << totalSum << '\n';
for(int qi = 1; qi <= n; ++qi){
char op; int r, c; cin >> op >> r >> c;
applyUpdate(r, c, op == 'U' ? +1 : -1);
cout << totalSum << '\n';
}
return 0;
}
|