[JZOJ6404] [11.04] B analog NOIP2019

Subject to the effect

Gives a \ (n * m \) matrix and an integer \ (K \) . Set \ (f (i, j) \) represented by \ ((i, j) \ ) is the upper left corner, side length \ (K \) number of types of weights within a square.
You ask \ (f (i, j) \) the sum of the maximum.

\(n,m\leq 3000,a_{i,j}\leq100000\)

Solution

Scanning line maintenance \ (K \) column \ (n-k + 1 \ ) squares answer, consider an influence of the right movement.
So there is to be a plus, one you want to delete.
For each of our weight, maintain a bitsetrecord of this weight in \ (k \) appeared in the column which a few lines. For the number you want to delete, query our predecessor successor, it will be able to know which lines the answer will be deleted \ (--1 \) . Clearly, these lines are continuously added to a number of empathy. Then the maintenance interval by differential changes on the line.

Complexity \ (O (\ FRAC nm ^ {2} {\ Omega}) \) .

Code

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 3007, M = 100007;
inline int read() {
    int x = 0, f = 0;
    char c = getchar();
    for (; c < '0' || c > '9'; c = getchar()) if (c == '-') f = 1;
    for (; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ '0');
    return f ? -x : x;
}

int n, m, k, cnt, ans[N], a[N][N], buc[M], c[N];
bool g[N][N];
int ans1; long long ans2;

struct bitset {
    unsigned _[95];
    void set(int po) { _[po >> 5] |= (1 << (po & 31)); }
    void del(int po) { _[po >> 5] -= (1 << (po & 31)); }
    int get(int po) { return (_[po >> 5] >> (po & 31)) & 1; }
    int pre(int po) {
        int a = po >> 5, b = po & 31;
        for (int i = b - 1; i >= 0; --i) if (_[a] & (1 << i)) return po - b + i;
        for (int i = a - 1; i >= 0; --i) if (_[i]) for (int j = 31; j >= 0; --j) if (_[i] & (1 << j)) return (i << 5) + j;
    }
    int nxt(int po) {
        int a = po >> 5, b = po & 31;
        for (int i = b + 1; i < 32; ++i) if (_[a] & (1 << i)) return po + i - b;
        for (int i = a + 1; i < 94; ++i) if (_[i]) for (int j = 0; j < 32; ++j) if (_[i] & (1 << j)) return (i << 5) + j;
    }
} b[M];

void upd(int res) { ans1 = max(ans1, res), ans2 += res; }
void plus(int l, int r, int v) { if (l <= r) c[l] += v, c[r + 1] -= v; }
void updans() {
    for (int i = 1; i <= n; ++i) ans[i] += (c[i] += c[i - 1]);
    for (int i = 1; i <= n; ++i) c[i] = 0;
}

int main() {
    freopen("b.in", "r", stdin);
    //freopen("b.out", "w", stdout);
    n = read(), m = read(), k = read();
    for (int i = 1; i <= n; ++i) for (int j = 1; j <= m; ++j) a[i][j] = read();
    cnt = 0;
    for (int i = 1; i <= k; ++i) for (int j = 1; j <= k; ++j) cnt += (buc[a[i][j]] == 0), ++buc[a[i][j]];
    ans[1] = cnt, upd(ans[1]);
    for (int i = 1; i <= n - k; ++i) {
        for (int j = 1; j <= k; ++j) --buc[a[i][j]], cnt -= (buc[a[i][j]] == 0);
        for (int j = 1; j <= k; ++j) cnt += (buc[a[i + k][j]] == 0), ++buc[a[i + k][j]];
        ans[i + 1] = cnt, upd(ans[i + 1]);
    }
    for (int i = 1; i <= n; ++i) {
        for (int j = 1; j <= m; ++j) buc[a[i][j]] = 0x3f3f3f3f;
        for (int j = m; j >= 1; --j) g[i][j] = (buc[a[i][j]] - j) <= k, buc[a[i][j]] = j;
    }
    for (int i = 1; i <= n; ++i) for (int j = 1; j <= k; ++j) b[a[i][j]].set(i);
    for (int i = 1; i <= 100000; ++i) b[i].set(0), b[i].set(n + 1);
    for (int j = 1; j <= m - k; ++j) {
        for (int i = 1; i <= n; ++i)
            if (!g[i][j]) {
                int a1 = b[a[i][j]].pre(i) + 1, a2 = b[a[i][j]].nxt(i) - 1;
                plus(max(a1, i - k + 1), min(a2 - k + 1, i), -1);
                b[a[i][j]].del(i);
            }
        for (int i = 1; i <= n; ++i)
            if (!b[a[i][j + k]].get(i)) {
                int a1 = b[a[i][j + k]].pre(i) + 1, a2 = b[a[i][j + k]].nxt(i) - 1;
                plus(max(a1, i - k + 1), min(a2 - k + 1, i), 1);
                b[a[i][j + k]].set(i);
            }
        updans();
        for (int i = 1; i <= n; ++i) upd(ans[i]);
    }
    printf("%d %lld\n", ans1, ans2);
    return 0;
}

Guess you like

Origin www.cnblogs.com/zjlcnblogs/p/11795274.html