1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Karol Kosinski 2025
#include <bits/stdc++.h>
#define FOR(i,a,b) for(int i=(a),_b=(b);i<_b;++i)
#define FR_(i,a,b) for(int i=(a),_b=(b);i<=_b;++i)
#define FD_(i,b,a) for(int i=(b),_a=(a);i>=_a;--i)
#define ALL(c) (c).begin(),(c).end()
#define SIZE(c) int((c).size())
#define X first
#define Y second
#define endl '\n'
#define NAM(x) #x,'=',x,"  "
using namespace std;
using LL = long long;
using ULL = unsigned long long;
using PII = pair<int, int>;
using TIII = tuple<int, int, int>;
template<class...T> void _cout(T...a){(cout<<...<<a);}

#ifndef ENABLE_DEBUG
#define DEB(k,p,f,x...)
#else
#define DEB(k,p,f,x...) {if(k)_cout("------",setw(4),__LINE__," : ",__FUNCTION__,endl);if(p)f(x);}
#endif
#define DEBF(f,x...)    DEB(1,1,f,x)
#define DEBL            DEBF(void,0)
#define DEBC(p,x...)    DEB(0,p,_cout,x)
#define DEBUG(x...)     DEBC(1,x)

constexpr int KQX = 150'005;

int n, m, k, q;
PII P[KQX];
map<PII, int> A, B;
bool Mark[KQX], LeftA[KQX], RightA[KQX], LeftB[KQX], RightB[KQX];
vector<int> V;

void select(const map<PII, int>& C, bool* Left, bool* Right)
{
    bool hole = true;
    auto it1 = C.begin();
    auto it2 = C.begin();
    ++ it2;
    while ( it2 != C.end() )
    {
        auto [ xy1, ind1 ] = *it1;
        auto xy2 = it2->X;
        bool next_hole = ( xy1.X != xy2.X or xy2.Y - xy1.Y > 1 );
        Left[ind1] = hole;
        Right[ind1] = next_hole;
        if ( hole and next_hole and not Mark[ind1] )
        {
            Mark[ind1] = true;
            V.push_back(ind1);
        }
        hole = next_hole;
        ++ it1, ++ it2;
    }
    int ind1 = it1->Y;
    Left[ind1] = hole;
    Right[ind1] = true;
    if ( hole and not Mark[ind1] )
    {
        Mark[ind1] = true;
        V.push_back(ind1);
    }
}

void checkLR(const map<PII, int>& C, bool* Left, bool* Right, const PII& key)
{
    auto it = C.find(key);
    auto itL = it, itR = it;
    ++ itR;
    if ( itL != C.begin() )
    {
        -- itL;
        int ind = itL->Y;
        Right[ind] = true;
        if ( Left[ind] and not Mark[ind] )
        {
            Mark[ind] = true;
            V.push_back(ind);
        }
    }
    if ( itR != C.end() )
    {
        int ind = itR->Y;
        Left[ind] = true;
        if ( Right[ind] and not Mark[ind] )
        {
            Mark[ind] = true;
            V.push_back(ind);
        }
    }
}

void iteration()
{
    DEBL;
    select( A, LeftA, RightA );
    select( B, LeftB, RightB );
    int res = 0;
    while ( not V.empty() )
    {
        int u = V.back();
        V.pop_back();
        DEBUG( "** ", NAM(u), "( ", P[u].X, " | ", P[u].Y, " )", endl );
        checkLR( A, LeftA, RightA, P[u] );
        checkLR( B, LeftB, RightB, pair( P[u].Y, P[u].X ) );
        ++ res;
    }
    cout << res << endl;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    cin >> n >> m >> k >> q;
    FOR(i,0,k)
    {
        int x, y; cin >> x >> y;
        P[i] = pair( x, y );
        A.emplace( P[i], i );
        B.emplace( pair( y, x ), i );
    }
    iteration();
    FOR(i,0,q)
    {
        int x, y; cin >> x >> y;
        auto p = pair( x, y );
        auto it = A.find(p);
        if ( it != A.end() )
        {
            A.erase(it);
            B.erase( pair( y, x ) );
        }
        else
        {
            P[k] = p;
            A.emplace( p, k );
            B.emplace( pair( y, x ), k );
            ++ k;
        }
        for ( auto el : A )
        {
            int i = el.Y;
            Mark[i] = LeftA[i] = RightA[i] = LeftB[i] = RightB[i] = false;
        }
        iteration();
    }
    return 0;
}