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
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, mm, k, q;
map<pair<ll, ll>, ll>m;
map<ll, pair<ll, ll>>odw;
unordered_map<ll, ll>stan;
ll dasie=0;
ll lastidx=0;

map<pair<ll, ll>, ll>s;
bool takeoff(pair<ll, ll>p)
{
    //cout<<p.first<<" "<<p.second<<" p "<<(bool)(s.find({p.first-1, p.second})!=s.end()&&(s.find({p.first, p.second+1})!=s.end()||s.find({p.first, p.second-1})!=s.end()))<<" "<<(bool)(s.find({p.first+1, p.second})!=s.end()&&(s.find({p.first+1, p.second})!=s.end()||s.find({p.first-1, p.second})!=s.end()))<<endl;
    if(s.find({p.first-1, p.second})!=s.end()&&(s.find({p.first, p.second+1})!=s.end()||s.find({p.first, p.second-1})!=s.end()))
        return 0;
    //cout<<(bool)(s.find({p.first+1, p.second})!=s.end())<<" \n";
    if(s.find({p.first+1, p.second})!=s.end()&&(s.find({p.first, p.second+1})!=s.end()||s.find({p.first, p.second-1})!=s.end()))
        return 0;
    return 1;
}
void solv()
{
    s.clear();
    s=m;
    /*
    for(auto x:s)
    {
        pair<ll, ll>point=x.first;
        odw[x.second]=point;
    }
    */
    ll ans=s.size();
    queue<pair<ll, ll>>que;
    for(auto x:m)
    {
        pair<ll, ll>point=x.first;
            //cout<<point.first<<" "<<point.second<<endl;
        if(takeoff(point))
        {
            que.push(point);
            s.erase(point);
        }
    }
    while(!que.empty())
    {
        ans--;
        pair<ll, ll>point=que.front();
        //cout<<point.first<<" "<<point.second<<" p\n";
        que.pop();
        vector<pair<ll, ll>>e= {{point.first+1, point.second}, {point.first-1, point.second}, {point.first, point.second+1},{point.first, point.second-1}};
        for(auto x:e)
        {
            if(s.find(x)!=s.end()&&takeoff(x))
            {
                s.erase(x);
                que.push(x);
            }
        }
    }
    cout<<m.size()-ans<<"\n";
}
/*
ll punkt(ll x, ll y)
{
    return x+((y-1)*m);
}
pair<ll, ll> decode(ll a)
{
    pair<ll, ll>res;
    res.first=a%m;
    res.second=0;
    if(res.first=0)
    {
        res.first=m;
        res.second=-1;
    }
    res.second+=((a/m)+1);
    return res;
}
*/
/*
pair<ll, ll> ktoraprzek(ll x, ll y)
{
    return ({x+y-1, n-x+y});
}
void napraw(ll x, ll y)
{
    if(m.find({x, y})==m.end())
        return;
    if((m.find({x+1, y})!=m.end()&&m.find({x+1, y+1})!=m.end())&&m.find({x, y+1})!=m.end())
        stan[m[{x, y}]]=3;
    if((m.find({x-1, y})!=m.end()&&m.find({x-1, y-1})!=m.end())&&m.find({x, y+1})!=m.end())
        stan[m[{x, y}]]=3;
    if((m.find({x-1, y})!=m.end()&&m.find({x-1, y-1})!=m.end())&&m.find({x, y-1})!=m.end())
        stan[m[{x, y}]]=3;
    if((m.find({x+1, y})!=m.end()&&m.find({x+1, y-1})!=m.end())&&m.find({x, y-1})!=m.end())
        stan[m[{x, y}]]=3;

}
*/
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    cin>>n>>mm>>k>>q;
    for(int i=0; i<k; i++)
    {
        ll x, y;
        cin>>x>>y;
        m[ {x, y}]=i;
        odw[i]= {x, y};
    }
    solv();
    for(int i=0; i<q; i++)
    {
        ll a, b;
        cin>>a>>b;
        lastidx=k;
        //cout<<m.size()<<" m1\n";
        if(m.find({a, b})==m.end())
        {
            m[ {a, b}]=lastidx;
            odw[lastidx]= {a, b};
            lastidx++;
        }
        else
        {
            odw.erase(m[ {a, b}]);
            m.erase({a, b});
        }
        //cout<<m.size()<<" m\n";
        solv();
    }
}