2 solutions

  • 0
    @ 2026-10-27 8:36:04
    #include <bits/stdc++.h>
    using namespace std;
    #define FIO(s)                    \
        freopen(s ".in", "r", stdin); \
        freopen(s ".out", "w", stdout);
    #ifdef _WIN32
    #define getchar() p1 == p2 && (p2 = (p1 = buf) + _fread_nolock(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++
    #else
    #define getchar() p1 == p2 && (p2 = (p1 = buf) + fread_unlocked(buf, 1, 1000000, stdin), p1 == p2) ? EOF : *p1++
    #endif
    char buf[1000000], *p1 = buf, *p2 = buf;
    template <typename T>
    void read(T &x)
    {
        x = 0;
        int f = 1;
        char c = getchar();
        for (; c < '0' || c > '9'; c = getchar())
            if (c == '-')
                f = -f;
        for (; c >= '0' && c <= '9'; c = getchar())
            x = x * 10 + c - '0';
        x *= f;
    }
    template <typename T, typename... Args>
    void read(T &x, Args &...y)
    {
        read(x);
        read(y...);
    }
    #ifdef _WIN32
    #define putchar(x) _putchar_nolock(x)
    #else
    #define putchar(x) putchar_unlocked(x)
    #endif
    template <class T>
    void write(T x)
    {
        static int stk[30];
        if (x < 0)
            putchar('-'), x = -x;
        int top = 0;
        do
        {
            stk[top++] = x % 10, x /= 10;
        } while (x);
        while (top)
            putchar(stk[--top] + '0');
    }
    template <class T>
    void write(T x, char lastChar) { write(x), putchar(lastChar); }
    using ll = long long;
    template <typename T>
    void chkmx(T &x, T y) { x = max(x, y); }
    int bit_width(int x) { return !x ? 0 : 32 - __builtin_clz(x); }
    int main()
    {
        FIO("matrix");
        int n, m, q, V;
        read(n, n, m, q, V);
        vector<int> R(n);
        vector<int> C(m);
        for (int i = 0; i < n; i++)
            read(R[i]);
        for (int j = 0; j < m; j++)
            read(C[j]);
        vector<int> P(n);
        vector<int> D(m);
        for (int i = 0; i < n; i++)
            read(P[i]);
        for (int j = 0; j < m; j++)
            read(D[j]);
        vector<vector<int>> a(n, vector<int>(m));
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
                read(a[i][j]);
        }
        bool gg = 0;
        for (int i = 0; i < n; i++)
        {
            for (int j = 0; j < m; j++)
            {
                if (a[i][j] != (a[0][0] ^ a[i][0] ^ a[0][j]))
                    gg = 1;
            }
        }
        struct Trie
        {
            vector<array<int, 2>> c;
            vector<ll> tagf;
            vector<ll> f;
            vector<int> tagg;
            vector<int> g;
            Trie() : c(1, array<int, 2>()), tagf(1), f(1), tagg(1), g(1) {}
            int newnode()
            {
                c.emplace_back(array<int, 2>());
                tagf.emplace_back(0);
                f.emplace_back(0);
                tagg.emplace_back(0);
                g.emplace_back(0);
                return c.size() - 1;
            }
            void pushdown(int u)
            {
                for (int i = 0; i < 2; i++)
                {
                    if (!c[u][i])
                    {
                        int x = newnode();
                        c[u][i] = x;
                    }
                    tagf[c[u][i]] += tagf[u];
                    f[c[u][i]] += tagf[u];
                    tagg[c[u][i]] += tagg[u];
                    g[c[u][i]] += tagg[u];
                }
                tagf[u] = 0;
                tagg[u] = 0;
            }
            void pushup(int u)
            {
                g[u] = 0;
                for (int i = 0; i < 2; i++)
                {
                    if (c[u][i])
                        chkmx(g[u], g[c[u][i]]);
                }
                f[u] = 0;
                for (int i = 0; i < 2; i++)
                {
                    if (c[u][i] && g[c[u][i]] == g[u])
                        chkmx(f[u], f[c[u][i]]);
                }
            }
            void insert(int x, int y, int val, int valcnt) // (u ^ x <= y), u += val
            {
                int u = 0;
                vector<int> path;
                path.reserve(30);
                for (int i = 29; i >= 0; i--)
                {
                    path.emplace_back(u);
                    int bx = x >> i & 1;
                    int by = y >> i & 1;
                    pushdown(u);
                    if (by)
                    {
                        tagf[c[u][bx]] += val;
                        f[c[u][bx]] += val;
                        tagg[c[u][bx]] += valcnt;
                        g[c[u][bx]] += valcnt;
                        u = c[u][bx ^ 1];
                    }
                    else
                    {
                        u = c[u][bx];
                    }
                }
                tagf[u] += val;
                f[u] += val;
                tagg[u] += valcnt;
                g[u] += valcnt;
                reverse(path.begin(), path.end());
                for (int u : path)
                    pushup(u);
            }
        } T;
        for (int i = 0; i < n; i++)
        {
            // int lim = (1 << __bit_width((uint)R[i])) - 1;
            int lim = (1 << bit_width(R[i])) - 1;
            T.insert(a[i][0], lim, 0, 1);
            T.insert(a[i][0], R[i], P[i], 0);
            T.insert(a[i][0], 0, P[i], 0);
        }
        for (int j = 0; j < m; j++)
        {
            // int lim = (1 << __bit_width((uint)C[j])) - 1;
            int lim = (1 << bit_width(C[j])) - 1;
            T.insert(a[0][j] ^ a[0][0], lim, 0, 1);
            T.insert(a[0][j] ^ a[0][0], C[j], D[j], 0);
            T.insert(a[0][j] ^ a[0][0], 0, D[j], 0);
        }
        ll tot = 0;
        for (int i = 0; i < n; i++)
            tot += P[i];
        for (int j = 0; j < m; j++)
            tot += D[j];
        auto answer = [&]()
        {
            if (gg)
                return write(-1, '\n'), void();
            if (T.g[0] < n + m)
                return write(-1, '\n'), void();
            write((tot << 1) - T.f[0], '\n');
        };
        answer();
        while (q--)
        {
            int op, x, y;
            read(op, x, y);
            x--;
            if (!gg)
            {
                if (op & 1)
                {
                    {
                        // int lim = (1 << __bit_width((uint)R[x])) - 1;
                        int lim = (1 << bit_width(R[x])) - 1;
                        T.insert(a[x][0], lim, 0, -1);
                        T.insert(a[x][0], R[x], -P[x], 0);
                        T.insert(a[x][0], 0, -P[x], 0);
                    }
                    R[x] = y;
                    {
                        // int lim = (1 << __bit_width((uint)R[x])) - 1;
                        int lim = (1 << bit_width(R[x])) - 1;
                        T.insert(a[x][0], lim, 0, 1);
                        T.insert(a[x][0], R[x], P[x], 0);
                        T.insert(a[x][0], 0, P[x], 0);
                    }
                }
                else
                {
                    {
                        // int lim = (1 << __bit_width((uint)C[x])) - 1;
                        int lim = (1 << bit_width(C[x])) - 1;
                        T.insert(a[0][x] ^ a[0][0], lim, 0, -1);
                        T.insert(a[0][x] ^ a[0][0], C[x], -D[x], 0);
                        T.insert(a[0][x] ^ a[0][0], 0, -D[x], 0);
                    }
                    C[x] = y;
                    {
                        // int lim = (1 << __bit_width((uint)C[x])) - 1;
                        int lim = (1 << bit_width(C[x])) - 1;
                        T.insert(a[0][x] ^ a[0][0], lim, 0, 1);
                        T.insert(a[0][x] ^ a[0][0], C[x], D[x], 0);
                        T.insert(a[0][x] ^ a[0][0], 0, D[x], 0);
                    }
                }
            }
            answer();
        }
        return 0;
    }
    

    Information

    ID
    3154
    Time
    2000ms
    Memory
    1024MiB
    Difficulty
    (None)
    Tags
    (None)
    # Submissions
    0
    Accepted
    0
    Uploaded By