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
#include<bits/stdc++.h>
#include<bits/extc++.h>
using namespace std;
using namespace __gnu_pbds;
int n,m;
int ans,tot;
class bundle
{
	tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> Tr0,Tr1,Tr2;
	set<int> Tr3;
	void check(int l,int r,int v)
	{
		int c0=Tr0.order_of_key(r)-Tr0.order_of_key(l);
		int c1=Tr1.order_of_key(r)-Tr1.order_of_key(l+1);
		int c2=Tr2.order_of_key(r)-Tr2.order_of_key(l);
		if(c1==(r-l)/2-1&&(c0==(r-l)/2||c2==(r-l)/2))
			ans+=v*((r-l)/2-1);
	}
	public:
		void insert_type0(int x)
		{
			auto it=Tr3.lower_bound(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,-1);
			Tr0.insert(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,1);
		}
		void erase_type0(int x)
		{
			auto it=Tr3.lower_bound(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,-1);
			Tr0.erase(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,1);
		}
		void insert_type1(int x)
		{
			auto it=Tr3.lower_bound(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,-1);
			Tr1.insert(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,1);
		}
		void erase_type1(int x)
		{
			auto it=Tr3.lower_bound(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,-1);
			Tr1.erase(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,1);
		}
		void insert_type2(int x)
		{
			auto it=Tr3.lower_bound(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,-1);
			Tr2.insert(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,1);
		}
		void erase_type2(int x)
		{
			auto it=Tr3.lower_bound(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,-1);
			Tr2.erase(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,1);
		}
		void insert_type3(int x)
		{
			auto it=Tr3.lower_bound(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,-1);
			if(it!=Tr3.end())
				check(x,*it,1);
			if(it!=Tr3.begin())
				check(*prev(it),x,1);
			Tr3.insert(x);
		}
		void erase_type3(int x)
		{
			Tr3.erase(x);
			auto it=Tr3.lower_bound(x);
			if(it!=Tr3.end()&&it!=Tr3.begin())
				check(*prev(it),*it,1);
			if(it!=Tr3.end())
				check(x,*it,-1);
			if(it!=Tr3.begin())
				check(*prev(it),x,-1);
		}
}U[400004],V[400004];
map<int,int> G[200002],C[200002];
void set_cube(int x,int y)
{
	ans++;
	U[x+y].insert_type3(y-x);
	V[y-x+n].insert_type3(x+y);
}
void reset_cube(int x,int y)
{
	ans--;
	U[x+y].erase_type3(y-x);
	V[y-x+n].erase_type3(x+y);
}
void insert_square(int x,int y)
{
	for(int dx=0;dx<=1;dx++)
		for(int dy=0;dy<=1;dy++)
		{
			C[x+dx][y+dy]++;
			if(C[x+dx][y+dy]==1)
				set_cube(x+dx,y+dy);
		}
}
void erase_square(int x,int y)
{
	for(int dx=0;dx<=1;dx++)
		for(int dy=0;dy<=1;dy++)
		{
			C[x+dx][y+dy]--;
			if(C[x+dx][y+dy]==0)
				reset_cube(x+dx,y+dy);
		}
}
inline int count_square(int x,int y){return G[x][y]+G[x+1][y]+G[x][y+1]+G[x+1][y+1];}
void insert_cube(int x,int y)
{
	tot++;
	G[x][y]=1;
	U[x+y-1].insert_type2(y-x);
	U[x+y].insert_type1(y-x);
	U[x+y+1].insert_type0(y-x);
	V[y-x-1+n].insert_type2(x+y);
	V[y-x+n].insert_type1(x+y);
	V[y-x+1+n].insert_type0(x+y);
	for(int dx=-1;dx<=0;dx++)
		for(int dy=-1;dy<=0;dy++)
			if(count_square(dx+x,dy+y)==4)
				insert_square(dx+x,dy+y);
}
void erase_cube(int x,int y)
{
	tot--;
	G[x][y]=0;
	U[x+y-1].erase_type2(y-x);
	U[x+y].erase_type1(y-x);
	U[x+y+1].erase_type0(y-x);
	V[y-x-1+n].erase_type2(x+y);
	V[y-x+n].erase_type1(x+y);
	V[y-x+1+n].erase_type0(x+y);
	for(int dx=-1;dx<=0;dx++)
		for(int dy=-1;dy<=0;dy++)
			if(count_square(dx+x,dy+y)==3)
				erase_square(dx+x,dy+y);
}
void answer_query()
{
	cout<<tot-ans<<'\n';
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	int b,q;
	cin>>n>>m>>b>>q;
	while(b--)
	{
		int x,y;
		cin>>x>>y;
		insert_cube(x,y);
	}
	answer_query();
	while(q--)
	{
		int x,y;
		cin>>x>>y;
		if(G[x][y]) erase_cube(x,y);
		else insert_cube(x,y);
		answer_query();
	}
	return 0;
}