P11923 [PA 2025] 砖块收集 / Zbieranie klocków

· · 题解

提供一份也许比较易懂的实现参考,因此推导过程较为省略。

大体思路就是,首先 2\times 2 的方形不能被消除,其次两个方形之间若存在形似阶梯状物,满足这些格子不能被立即消去,那么这些格子永远无法消去。

于是可以用 map 维护每个格子的状态 \texttt{0/1/2},分别表示【能立即消去】【不能立即消去但不构成 2\times2 方形】【属于 2\times2 方形一部分】。

状态 \texttt{2} 的贡献是容易维护的,只需要更新时判一下就行,而状态 \texttt{1} 有贡献当且仅当在经过这个格子的主对角线或副对角线上出现形如 \texttt{211\ldots12}。

考虑将每条对角线作为一个根,动态开点线段树维护区间前缀构成形如 \texttt{11\ldots12} 时 \texttt{2} 的位置 lt,类似地维护后缀的位置 gt,则两个区间合并时 \texttt{211\ldots1} 和 \texttt{11\ldots12} 可以拼在一起产生 lt_r-gt_l-1 的贡献。

有没有可能一个 \texttt{1} 在主对角线和副对角线都产生贡献而导致算重呢?实际上并不会,手模可以发现这样必定会构成一个 2\times2 方形。

最后更新一个点时,把周围八相邻的点都扫一遍更新即可。

不严谨的复杂度 O(Q\log (n+Q))。

::::info[code]

#include<iostream>
#include<algorithm>
#include<queue>
#include<array>
#include<numeric>
#include<bitset>
#include<map>
#if defined FIO
auto III=freopen("1.in","r",stdin);
auto OOO=freopen("1.out","w",stdout);
#endif
#if defined BUG
#define bug(x...) ({x;void();})
#else
#define bug(x...) ({})
#endif
#define i64 long long
#define pb push_back
#define vec vector
#define arr array
#define SIZ(x) (int)(x).size()
#define ALL(x) begin(x),end(x)
#define RNG(x,l,r) &(x)[l],1+&(x)[r]
#define rep(i,l,r) for(int i=(l),i##_=(r);i<=i##_;i++)
#define per(i,l,r) for(int i=(l),i##_=(r);i>=i##_;i--)
template<class U,class V> void tomin(U&x,V&&y){if(y<x)x=y;}
template<class U,class V> void tomax(U&x,V&&y){if(y>x)x=y;}
auto SSS=std::cin.tie(0)->sync_with_stdio(0);
using namespace std; namespace __{

constexpr int N = 4e5 + 5;
int n,m,K,Q,ans;
map<arr<int,2>,int> mp;

int a[N], b[N], tot;
struct zz{ int ch[2], lt, gt, all, ans; } z[N * 40];
#define lc(o) z[o].ch[0]
#define rc(o) z[o].ch[1]
#define mid ((l+r)>>1)
#define LLL lc(o),l,mid
#define RRR rc(o),mid+1,r
#define OOO o,l,r
void up(int o){
    zz &a = z[lc(o)], &b = z[rc(o)], &c = z[o];
    c.lt = a.lt ? a.lt : a.all * b.lt;
    c.gt = b.gt ? b.gt : b.all * a.gt;
    c.all = a.all & b.all;
    c.ans = a.ans + b.ans + (b.lt && a.gt ? b.lt - a.gt - 1 : 0);
}
void mdf(int x,int t,int& o,int l=1,int r=n){
    if(!o) o = ++tot;
    if(l == r){
        if(t == 2) z[o].lt = z[o].gt = l, z[o].all = 0, z[o].ans = 0;
        if(t == 1) z[o].lt = z[o].gt = 0, z[o].all = 1, z[o].ans = 0;
        if(t == 0) z[o] = z[0];
        return;
    }
    if(x<=mid) mdf(x,t,LLL); else mdf(x,t,RRR);
    up(o);
}

int type(int x,int y){
    int t[3][3]{};
    rep(dx,-1,1) rep(dy,-1,1) if(int tx = x + dx, ty = y + dy; (tx != x || ty != y) && tx >= 1 && tx <= n && ty >= 1 && ty <= m){
        t[dx+1][dy+1] = mp.count({tx, ty});
    }
    if(!t[0][1] && !t[2][1]) return 0;
    if(!t[1][0] && !t[1][2]) return 0;
    if(t[0][0] && t[0][1] && t[1][0]) return 2;
    if(t[0][1] && t[0][2] && t[1][2]) return 2;
    if(t[1][0] && t[2][0] && t[2][1]) return 2;
    if(t[1][2] && t[2][1] && t[2][2]) return 2;
    return 1;
}

void upd(int x,int y,int op){
    if(op){
        ans += z[a[x + y]].ans + z[b[x - y + m]].ans + (mp[{x,y}] == 2);
        mdf(x, 0, a[x + y]);
        mdf(x, 0, b[x - y + m]);
        ans -= z[a[x + y]].ans + z[b[x - y + m]].ans;
        mp.erase({x,y});
    }else{
        int t = type(x,y);
        ans += z[a[x + y]].ans + z[b[x - y + m]].ans + (mp[{x,y}] == 2);
        mp[{x,y}] = t;
        mdf(x, t, a[x + y]);
        mdf(x, t, b[x - y + m]);
        ans -= z[a[x + y]].ans + z[b[x - y + m]].ans + (mp[{x,y}] == 2);
    }
}

void go(int x,int y,int op){
    upd(x,y,op);
    rep(dx,-1,1) rep(dy,-1,1) if(int tx = x + dx, ty = y + dy; (tx != x || ty != y) && tx >= 1 && tx <= n && ty >= 1 && ty <= m){
        if(mp.count({tx, ty})) upd(tx, ty, 0);
    }
}

void main(){
    // signed tid,cse; for(cin>>tid>>cse; cse--; clear())
[&](){
    cin>>n>>m>>K>>Q;
    rep(i,1,K){
        int x,y; cin>>x>>y;
        ans++;
        go(x,y,0);
    }
    cout << ans << '\n';
    rep(i,1,Q){
        int x,y; cin>>x>>y;
        if(mp.count({x,y})){
            ans--;
            go(x,y,1);
        }else{
            ans++;
            go(x,y,0);
        }
        cout << ans << '\n';
    }
}();}}
signed main(){__::main();}