#include <bits/stdc++.h>
using namespace std;
template <typename T>
class binary_indexed_tree {
const size_t n;
vector<T> tree;
public:
binary_indexed_tree(size_t n) : n(n), tree(n + 1) {}
// a[i] += val
void update(size_t i, T val) {
assert(0 <= i and i <= n);
for (++i; i <= n; i += i & -i)
tree[i] += val;
}
// sum in range [0...i)
T query(size_t i) const {
assert(0 <= i and i <= n);
T ret = 0;
for (; i; i &= i - 1)
ret += tree[i];
return ret;
}
};
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
#ifdef palilo
freopen("in", "r", stdin);
freopen("out", "w", stdout);
#endif
int n;
cin >> n;
vector a(n, vector<int>(n));
for (auto& vt : a) {
for (auto& x : vt) {
cin >> x;
}
}
partial_sum(a[0].begin(), a[0].end(), a[0].begin());
for (int i = 1; i != n; ++i) {
a[i][0] += a[i - 1][0];
for (int j = 1; j != n; ++j) {
a[i][j] += max(a[i - 1][j], a[i][j - 1]);
}
}
vector bits(n, binary_indexed_tree<int>(n));
for (int i = 0; i != n; ++i) {
for (int j = 0; j != n; ++j) {
bits[i].update(j, a[i][j]);
bits[i].update(j + 1, -a[i][j]);
}
}
auto sum = accumulate(a.begin(), a.end(), 0ll, [&](const auto& sum, const auto& vt) {
return sum + accumulate(vt.begin(), vt.end(), 0ll);
});
vector<pair<int, int>> intervals(n);
char op;
for (int q = n, r, c; q--;) {
cout << sum << '\n';
cin >> op >> r >> c, --r, --c;
const int x = op == 'U';
intervals[r] = {c, n};
if (r) {
int ed = intervals[r].first + 1;
while (ed != n && bits[r - 1].query(ed + 1) < bits[r].query(ed) + x) ++ed;
intervals[r].second = ed;
}
int r2 = r + 1;
for (; r2 != n; ++r2) {
auto [st, ed] = intervals[r2 - 1];
while (st != ed && bits[r2 - 1].query(st + 1) < bits[r2].query(st) + !x) ++st;
if (st == ed) break;
while (ed != n && bits[r2 - 1].query(ed + 1) < bits[r2].query(ed) + x) ++ed;
intervals[r2] = {st, ed};
}
for (int i = r; i < r2; ++i) {
sum += (intervals[i].second - intervals[i].first) * (x ? 1 : -1);
bits[i].update(intervals[i].first, x ? 1 : -1);
bits[i].update(intervals[i].second, x ? -1 : 1);
}
}
cout << sum << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
324 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
328 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
22460 KB |
Output is correct |
2 |
Correct |
206 ms |
22456 KB |
Output is correct |
3 |
Correct |
212 ms |
22468 KB |
Output is correct |
4 |
Correct |
201 ms |
22464 KB |
Output is correct |
5 |
Correct |
215 ms |
22472 KB |
Output is correct |
6 |
Correct |
212 ms |
22476 KB |
Output is correct |
7 |
Correct |
207 ms |
22492 KB |
Output is correct |
8 |
Correct |
202 ms |
22456 KB |
Output is correct |
9 |
Correct |
201 ms |
22464 KB |
Output is correct |
10 |
Correct |
201 ms |
22500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
2 ms |
324 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
2 ms |
328 KB |
Output is correct |
7 |
Correct |
2 ms |
332 KB |
Output is correct |
8 |
Correct |
2 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
207 ms |
22460 KB |
Output is correct |
11 |
Correct |
206 ms |
22456 KB |
Output is correct |
12 |
Correct |
212 ms |
22468 KB |
Output is correct |
13 |
Correct |
201 ms |
22464 KB |
Output is correct |
14 |
Correct |
215 ms |
22472 KB |
Output is correct |
15 |
Correct |
212 ms |
22476 KB |
Output is correct |
16 |
Correct |
207 ms |
22492 KB |
Output is correct |
17 |
Correct |
202 ms |
22456 KB |
Output is correct |
18 |
Correct |
201 ms |
22464 KB |
Output is correct |
19 |
Correct |
201 ms |
22500 KB |
Output is correct |
20 |
Correct |
2 ms |
332 KB |
Output is correct |
21 |
Correct |
240 ms |
26552 KB |
Output is correct |
22 |
Correct |
246 ms |
26560 KB |
Output is correct |
23 |
Correct |
249 ms |
26580 KB |
Output is correct |
24 |
Correct |
479 ms |
26656 KB |
Output is correct |
25 |
Correct |
458 ms |
26656 KB |
Output is correct |
26 |
Correct |
494 ms |
26788 KB |
Output is correct |
27 |
Correct |
204 ms |
22468 KB |
Output is correct |
28 |
Correct |
211 ms |
22456 KB |
Output is correct |
29 |
Correct |
257 ms |
26568 KB |
Output is correct |
30 |
Correct |
243 ms |
26556 KB |
Output is correct |
31 |
Correct |
461 ms |
26664 KB |
Output is correct |
32 |
Correct |
478 ms |
26668 KB |
Output is correct |
33 |
Correct |
204 ms |
22456 KB |
Output is correct |
34 |
Correct |
197 ms |
22464 KB |
Output is correct |