0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-27 17:36:27 +00:00

Compare commits

..

2 Commits

2 changed files with 187 additions and 9 deletions

151
Luogu/CF444C/CF444C.cpp Normal file
View File

@ -0,0 +1,151 @@
#include <iostream>
#include <cmath>
using std::cin;
using std::cout;
const char endl = '\n';
struct node {
int l, r;
int color, new_color;
long long sum, sum_delta;
node *lchild, *rchild;
node()
: l(0), r(0), color(-1), new_color(-1), sum(0), sum_delta(0), lchild(nullptr), rchild(nullptr) {}
node(int _l, int _r)
: l(_l), r(_r), color(-1), new_color(-1), sum(0), sum_delta(0), lchild(nullptr), rchild(nullptr) {}
~node() {
delete lchild;
delete rchild;
}
void pushup() {
sum = 0;
color = -1;
if (lchild != nullptr) {
sum += lchild->sum;
color = lchild->color;
}
if (rchild != nullptr) {
sum += rchild->sum;
color = color == rchild->color ? color : -1;
}
}
void pushdown() {
if (new_color == -1 || sum_delta == 0) return;
if (lchild != nullptr) {
if (sum_delta) {
lchild->sum_delta += sum_delta;
lchild->sum += sum_delta * (lchild->r - lchild->l + 1);
}
if (new_color != -1) {
lchild->color = lchild->new_color = new_color;
}
}
if (rchild != nullptr) {
if (sum_delta) {
rchild->sum_delta += sum_delta;
rchild->sum += sum_delta * (rchild->r - rchild->l + 1);
}
if (new_color != -1) {
rchild->color = rchild->new_color = new_color;
}
}
new_color = -1;
sum_delta = 0;
}
};
void build(node *&u, int l, int r) {
u = new node(l, r);
if (l == r) {
u->color = l;
return;
}
int mid = (l + r) >> 1;
if (l <= mid) build(u->lchild, l, mid);
if (r > mid) build(u->rchild, mid + 1, r);
u->pushup();
}
void modify(node *u, int l, int r, int new_color) {
if (l <= u->l && u->r <= r && ~u->color) {
u->sum += static_cast<long long>(std::abs(u->color - new_color)) * (u->r - u->l + 1);
u->sum_delta += std::abs(u->color - new_color);
u->color = u->new_color = new_color;
return;
}
int mid = (u->l + u->r) >> 1;
u->pushdown();
if (l <= mid) modify(u->lchild, l, r, new_color);
if (r > mid) modify(u->rchild, l, r, new_color);
u->pushup();
}
long long query(node *u, int l, int r) {
if (l <= u->l && u->r <= r) {
return u->sum;
}
int mid = (u->l + u->r) >> 1;
long long res = 0;
u->pushdown();
if (l <= mid) res += query(u->lchild, l, r);
if (r > mid) res += query(u->rchild, l, r);
return res;
}
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, m;
node *root;
cin >> n >> m;
build(root, 1, n);
while (m--) {
int op, l, r;
cin >> op >> l >> r;
if (op == 1) {
int x;
cin >> x;
modify(root, l, r, x);
} else {
cout << query(root, l, r) << endl;
}
}
delete root;
return 0;
}

View File

@ -8,7 +8,36 @@ const char endl = '\n';
const int N = 205; const int N = 205;
int n, m; int n, m;
long long g[N][N], ans; long long g[N][N], s[N][N], c[N], ans;
bool check(int x) {
long long t = 0;
for (int i = x; i <= m; i++) {
t = std::min(t, c[i - x]);
if (c[i] > t) return true;
}
return false;
}
int calc() {
int l = 1, r = n, res = 0;
while (l <= r) {
int mid = (l + r) >> 1;
if (check(mid)) {
res = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return res;
}
int main() { int main() {
std::ios::sync_with_stdio(false); std::ios::sync_with_stdio(false);
@ -20,19 +49,17 @@ int main() {
for (int j = 1; j <= m; j++) { for (int j = 1; j <= m; j++) {
cin >> g[i][j]; cin >> g[i][j];
g[i][j] += g[i - 1][j] + g[i][j - 1] - g[i - 1][j - 1]; s[i][j] = s[i - 1][j] + g[i][j];
} }
} }
for (int i = 1; i <= n; i++) { for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) { for (int j = i; j <= n; j++) {
for (int k = i; k <= n; k++) { for (int k = 1; k <= m; k++) {
for (int l = j; l <= m; l++) { c[k] = c[k - 1] + s[j][k] - s[i - 1][k];
if (g[k][l] - g[k][j - 1] - g[i - 1][l] + g[i - 1][j - 1] >= 0) {
ans = std::max(ans, static_cast<long long>(k - i + 1) * (l - j + 1));
}
}
} }
ans = std::max(ans, static_cast<long long>(j - i + 1) * calc());
} }
} }