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
#include <bits/stdc++.h>
using namespace std;
vector<pair<int,int>> p;
set<pair<int,int>> e2;
void licz()
{
    set<pair<int,int>> e = e2;
    int ans = 0;
    map<pair<int,int>,int> m;
    vector<pair<int,int>> k(p.size());
    vector<bool> vis(p.size());
    for(int i  =0 ;i<k.size();i++)
    {
        k[i].first =0;
        k[i].second = 0;
        m[p[i]] = i;
        vis[i] = 0;
    }
    for(int i = 0;i<p.size();i++)
    {
        if(e.find({p[i].first+1,p[i].second}) != e.end())
        {
            k[i].first++;
        }
        if(e.find({p[i].first-1,p[i].second}) != e.end())
        {
            k[i].first++;
        }
        if(e.find({p[i].first,p[i].second-1}) != e.end())
        {
            k[i].second++;
        }
        if(e.find({p[i].first,p[i].second+1}) != e.end())
        {
            k[i].second++;
        }
    }
    queue<int> q;
    for(int i = 0;i<k.size();i++)
    {
        if(k[i].first == 0 || k[i].second == 0)
        {
            //cout<<'a';
            q.push(i);
            vis[i] = true;
            e.erase(p[i]);
        }
    }
    while(!q.empty())
    {
        //cout<<q.front()<<" v\n";
        if(e.find({p[q.front()].first-1,p[q.front()].second}) != e.end())
        {
            k[m[{p[q.front()].first-1,p[q.front()].second}]].first--;
            if(k[m[{p[q.front()].first-1,p[q.front()].second}]].first == 0)
            {
                q.push(m[{p[q.front()].first-1,p[q.front()].second}]);
                e.erase({p[q.front()].first-1,p[q.front()].second});
            }
        }
        if(e.find({p[q.front()].first+1,p[q.front()].second}) != e.end())
        {
            k[m[{p[q.front()].first+1,p[q.front()].second}]].first--;
            if(k[m[{p[q.front()].first+1,p[q.front()].second}]].first == 0)
            {
                q.push(m[{p[q.front()].first+1,p[q.front()].second}]);
                e.erase({p[q.front()].first+1,p[q.front()].second});
            }
        }
        if(e.find({p[q.front()].first,p[q.front()].second-1}) != e.end())
        {
            k[m[{p[q.front()].first,p[q.front()].second-1}]].second--;
            if(k[m[{p[q.front()].first,p[q.front()].second-1}]].second == 0)
            {
                q.push(m[{p[q.front()].first,p[q.front()].second-1}]);
                e.erase({p[q.front()].first,p[q.front()].second-1});
            }
        }
        if(e.find({p[q.front()].first,p[q.front()].second+1}) != e.end())
        {
            k[m[{p[q.front()].first,p[q.front()].second+1}]].second--;
            if(k[m[{p[q.front()].first,p[q.front()].second+1}]].second == 0)
            {
                q.push(m[{p[q.front()].first,p[q.front()].second+1}]);
                e.erase({p[q.front()].first,p[q.front()].second+1});
            }
        }
        q.pop();
        ans++;
    }
    cout<<ans<<"\n";
}
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,m,k,q;
    cin>>n>>m>>k>>q;
    for(int i =0;i<k;i++)
    {
        int x,y;
        cin>>x>>y;
        x--;y--;
        p.push_back({x,y});
        e2.insert({x,y});
    }
    licz();
    for(int w = 0;w<q;w++)
    {

        int x,y;
        cin>>x>>y;
        x--;y--;
        if(e2.find({x,y}) != e2.end())
        {
            e2.erase({x,y});
            p.clear();
            for(auto i = e2.begin();i != e2.end() ;i++)
            {
                p.push_back(*i);
            }
        }
        else
        {
            p.push_back({x,y});
            e2.insert({x,y});
        }
        licz();
    }
}