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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#ifndef LOCAL
#pragma GCC optimize("O3")
#endif
#include <bits/stdc++.h>
#define FOR(i,p,k) for(int i=(p); i<=(k); ++i)
#define REP(i,k) FOR(i,0,(k)-1)
#define RFOR(i,p,n) for(int i=(p); i>=(n); --i)
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define ssize(x) int((x).size())
#define fi first
#define se second
#define V vector
#define pb push_back
#define eb emplace_back
#define C const
#define pn printf("\n")
using namespace std;
typedef long long ll;
typedef V <int> vi;
typedef V <ll> vll;
typedef C int ci;
typedef C ll cll;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
void chmin(auto &a, auto b){a=min(a,b);}
void chmax(auto &a, auto b){a=max(a,b);}
ci inf=2.1e9;
cll infll=4.5e18;
int I(){
    int z;
    while ((z=getchar_unlocked())<'0'||z>'9');
    int r=z-'0';
    while ((z=getchar_unlocked())>='0'&&z<='9')
        r=r*10+z-'0';
    return r;
}
void ans(){
    [[maybe_unused]] ci n=I(),m=I(); ci qqq=I(),q=I();
    struct pkt{
        int x,y;
        bool operator<(C pkt &i) C{
            return x<i.x||(x<=i.x&&y<i.y);
        }
        pkt operator+(C pkt &i) C{
            return {x+i.x,y+i.y};
        }
    };
    struct prz{
        int p,k;
        bool operator<(C prz &i) C{
            return p<i.p;
        }
        bool zaw(ci i) C{
            return i>=p&&i<=k;
        }
        int dl() C{
            return k-p+1;
        }
    };
    V <pkt> sas={{-1, 0}, {-1, 1}, {0, 1}, {1, 1}, {1, 0}, {1, -1}, {0, -1}, {-1, -1}};
    vi wyn(qqq+q);
    V <pkt> wej;
    REP(_, qqq+q){
        ci x=I(),y=I();
        wej.eb(x, y);
    }
    REP(podejrzus, 2){
        int w=0;
        map <pkt, bool> mpczy;
        map <pkt, int> mpust; // -1 / 0 / 1
        map <int, set <prz>> schody; // TODO: WARNING: NOTE: opt z mapy do tablicy
        [[maybe_unused]] auto czyzaw=[&](C set <prz> &s, ci i) -> bool{
            auto itr=s.upper_bound({i, 0});
            if (itr==s.begin())
                return 0;
            --itr;
            return itr->zaw(i);
        };
        auto wstaw=[&](ci c, ci ind){
            auto &s=schody[c];
            auto itr=s.lower_bound({ind, 0});
            assert(itr==s.end()||itr->p>ind);
            int p=ind,k=ind;
            if (itr!=s.end()&&itr->p==ind+1){
                auto nitr=itr;
                ++itr;
                k=nitr->k,s.erase(nitr);
            }
            if (itr!=s.begin()){
                --itr;
                if (itr->k==ind-1)
                    p=itr->p,s.erase(itr);
            }
            s.emplace(p, k);
        };
        auto usun=[&](ci c, ci ind){
            auto &s=schody[c];
            auto itr=s.upper_bound({ind, 0});
            if (itr==s.begin())
                return;
            //assert(itr!=s.begin());
            --itr;
            if (!itr->zaw(ind))
                return;
            //assert(itr->zaw(ind));
            C prz a=*itr;
            s.erase(itr);
            if (a.p<ind)
                s.emplace(a.p, ind-1);
            if (a.k>ind)
                s.emplace(ind+1, a.k);
        };
        auto dajindhigh=[&](C pkt &p){
            [[maybe_unused]] ci c=p.x-p.y;
            return 2*p.y;
        };
        auto dajindlow=[&](C pkt &p){
            [[maybe_unused]] ci c=p.x-p.y-1;
            return 2*p.y+1;
        };
        auto akt=[&](C pkt &p, ci mul){
            if (mpust.contains(p)){
                ci l=mpust[p];
                if (mpust[p]==5)
                    usun(p.x-p.y, dajindhigh(p));
                if (mpust[p]==1)
                    usun(p.x-p.y-1, dajindlow(p));
            }
            if (!mpczy.contains(p)||!mpczy[p])
                return;
            static vi v(8);
            for (int i=0; C pkt &d : sas){
                C pkt a=d+p;
                v[i++]=mpczy.contains(a)&&mpczy[a];
            }
            int czy=0;
            for (ci i : {1, 3, 5, 7})
                czy|=v[i-1]&v[i]&v[(i+1)%8];
            if (czy)
                mpust[p]=0;
            else{
                // czy mamy niepewność?
                for (ci i : {1, 3, 5, 7})
                    if (!v[i]&&v[i-1]&&v[(i-2+8)%8]&&v[(i+1)%8]&&v[(i+2)%8])
                        assert(!czy),czy=i;
                if (mul>0){
                    if (czy==1)
                        wstaw(p.x-p.y-1, dajindlow(p));
                    else if (czy==5)
                        wstaw(p.x-p.y, dajindhigh(p));
                }
                if (!czy)
                    w+=mul*podejrzus;
                mpust[p]=czy ? czy : -1;
            }
        };
        auto czyumozliwia=[&](C pkt &p) -> bool{
            if (!mpczy.contains(p)||!mpczy[p])
                return 1;
            ci l=mpust[p];
            assert(l<=0);
            return l==-1;
        };
        auto revind=[&](ci c, ci ind){
            ci y=(ind-(ind&1))/2;
            return pkt{c+y+(ind&1), y};
        };
        auto aktsch=[&](C pkt &p, ci mul){
            set <pii> s; // <c, indeks początku przedziału>
            FOR(dx, -2, 2) FOR(dy, -2, 2)
                if (abs(dx)<2||abs(dy)<2){
                    C pkt a=p+pkt(dx, dy);
                    ci mp=mpust.contains(a) ? mpust[a] : -2;
                    if (mp<=0||!mpczy.contains(a)||!mpczy[a])
                        continue;
                    if (mp==1){ // jako low
                        ci c=a.x-a.y-1,ind=dajindlow(a);
                        auto &sc=schody[c];
                        auto itr=sc.upper_bound({ind,0});
                        if (itr!=sc.begin()){
                            --itr;
                            if (itr->zaw(ind)&&s.emplace(c, itr->p).second)
                                w+=mul*itr->dl()*(czyumozliwia(revind(c, itr->p-1))||
                                        czyumozliwia(revind(c, itr->k+1)));
                        }
                    }
                    if (mp==5){ // jako high
                        ci c=a.x-a.y,ind=dajindhigh(a);
                        auto &sc=schody[c];
                        auto itr=sc.upper_bound({ind, 0});
                        if (itr!=sc.begin()){
                            --itr;
                            if (itr->zaw(ind)&&s.emplace(c, itr->p).second)
                                w+=mul*itr->dl()*(czyumozliwia(revind(c, itr->p-1))||
                                        czyumozliwia(revind(c, itr->k+1)));
                        }
                    }
                }
        };
        auto ksor=[&](ci x, ci y){
            C pkt p={x, y};
            aktsch(p, -1);
            akt(p, -1);
            for (C pkt &d : sas)
                akt(p+d, -1);
            mpczy[p]^=1;
            akt(p, 1);
            for (C pkt &d : sas)
                akt(p+d, 1);
            aktsch(p, 1);
        };
        REP(i, qqq+q){
            ksor(wej[i].x, wej[i].y);
            wyn[i]+=w;
        }
        for (pkt &i : wej)
            i.y*=-1;
    }
    FOR(i, qqq-1, qqq+q-1)
        printf("%d\n", wyn[i]);
}
int main(){
    //ios_base::sync_with_stdio(0),cin.tie(0);
    int tt=1;
    //tt=I();
    while (tt--)ans();
}