Language: C++

    #include <bits/stdc++.h>
    using namespace std;
    using LL = long long;

    int H,W,L;
    int P;

    const int N_MAX = 4001;

    short la[N_MAX][N_MAX];
    short ra[N_MAX][N_MAX];
    short ua[N_MAX][N_MAX];
    short da[N_MAX][N_MAX];

    struct BIT{
    	vector<int>seg;
    	int width;
    	BIT(int n):width(1){
    		while(width < n)width *= 2;
    		seg.resize(width + 1);
    	}
    	void add(int x,int val){
    		x++;
    		while(x <= width){
    			seg[x] += val;
    			x += (x & -x);
    		}
    	}
    	int range(int x){
    		int res = 0;
    		while(x > 0){
    			res += seg[x];
    			x -= (x & -x);
    		}
    		return res;
    	}
    };

    int main(){
    	cin >> H >> W >> L >> P;
    	for(int i = 0;i < H;++i){
    		for(int j = 0;j < W;++j){
    			la[i][j] = ra[i][j] = ua[i][j] = da[i][j] = 6666;
    		}
    	}
    	for(int i = 0;i < P;++i){
    		int a,b;
    		cin >> a >> b;
    		--a;--b;
    		la[a][b] = ra[a][b] = ua[a][b] = da[a][b] = 0;
    	}
    	for(int i = 0;i < H;++i){
    		for(int j = 0;j < W;++j){
    			la[i][j] = (short)min(la[i][j],(int)((j)?(la[i][j-1]+(int)1):(int)1));
    		}
    	}
    	for(int i = 0;i < H;++i){
    		for(int j = W - 1;j >= 0;--j){
    			ra[i][j] =(short) min(ra[i][j],(int)(ra[i][j+1]+(int)1));
    		}
    	}
    	for(int i = 0;i < H;++i){
    		for(int j = 0;j < W;++j){
    			ua[i][j] = (short)min(ua[i][j],(int)((i)?(ua[i-1][j]+(int)1):(int)1));
    		}
    	}
    	for(int i = H - 1;i >= 0;--i){
    		for(int j = 0;j < W;++j){
    			da[i][j] = (short)min(da[i][j],(int)(da[i+1][j]+(int)1));
    		}
    	}
    	for(int i = 0;i < H;++i){
    		for(int j = 0;j < W;++j){
    			ra[i][j] = min(ra[i][j],da[i][j]);
    		}
    	}
    	for(int i = 0;i < H;++i){
    		for(int j = 0;j < W;++j){
    			la[i][j] = min(la[i][j],ua[i][j]);
    		}
    	}
    	vector<vector<int>>pos(H),neg(W-1);
    	vector<vector<int>>pro(H),nro(W-1);
    	for(int i = 0;i < H;++i){
    		for(int j = 0;j < W;++j){
    			if(i - j >= 0){
    				pos[i - j].push_back(la[i][j]-pos[i - j].size()-1);
    				pro[i - j].push_back(ra[i][j]);
    			}else{
    				neg[j - i - 1].push_back(la[i][j]-neg[j - i - 1].size()-1);
    				nro[j - i - 1].push_back(ra[i][j]);
    			}
    		}
    	}
    	long long ans = 0;

    	for(int k = 0;k < H;++k){
    		BIT bit(pos.size());
    		priority_queue<pair<int,int>>que;
    		for(int i = 0;i < pos[k].size();++i){
    			que.push(make_pair((int)pos[k][i],i));
    		}
    		for(int i = 0;i < pos[k].size();++i){
    			while(!que.empty() && que.top().first >= -i){
    				bit.add(que.top().second,1);
    				que.pop();
    			}
    			int dn = i + L - 1;dn = min(dn,(int)pos[k].size());
    			int up = i + pro[k][i];
    			if(dn < up){
    				int ub = bit.range(up);
    				int db = bit.range(dn);
    				int can = ub - db;
    				ans += (long long)can;
    			}
    		}
    	}

    	for(int k = 0;k < W-1;++k){
    	    BIT bit(neg.size());
    	    priority_queue<pair<int,int>>que;
    	    for(int i = 0;i < neg[k].size();++i){
    	    	que.push(make_pair((int)neg[k][i],i));
    	    }
    	    for(int i = 0;i < neg[k].size();++i){
    	    	while(!que.empty() && que.top().first >= -i){
    	   			bit.add(que.top().second,1);
    	   			que.pop();
    	    	}
    	    	int dn = i + L - 1;dn = min(dn,(int)neg[k].size());
    	   		int up = i + nro[k][i];
    	    	if(dn < up){
    	    		int can = bit.range(up) - bit.range(dn);
    	    		ans += (long long)can;
    	   		}
    	   	}
    	}
    	cout << ans << endl;
    	return 0;
    }

Compiler output:
a.cc: In function 'int main()':
a.cc:54:46: error: no matching function for call to 'min(short int&, int)'
   54 |                         la[i][j] = (short)min(la[i][j],(int)((j)?(la[i][j-1]+(int)1):(int)1));
      |                                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:54:46: note:   deduced conflicting types for parameter 'const _Tp' ('short int' and 'int')
   54 |                         la[i][j] = (short)min(la[i][j],(int)((j)?(la[i][j-1]+(int)1):(int)1));
      |                                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:54:46: note:   mismatched types 'std::initializer_list<_Tp>' and 'short int'
   54 |                         la[i][j] = (short)min(la[i][j],(int)((j)?(la[i][j-1]+(int)1):(int)1));
      |                                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:59:46: error: no matching function for call to 'min(short int&, int)'
   59 |                         ra[i][j] =(short) min(ra[i][j],(int)(ra[i][j+1]+(int)1));
      |                                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:59:46: note:   deduced conflicting types for parameter 'const _Tp' ('short int' and 'int')
   59 |                         ra[i][j] =(short) min(ra[i][j],(int)(ra[i][j+1]+(int)1));
      |                                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:59:46: note:   mismatched types 'std::initializer_list<_Tp>' and 'short int'
   59 |                         ra[i][j] =(short) min(ra[i][j],(int)(ra[i][j+1]+(int)1));
      |                                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:64:46: error: no matching function for call to 'min(short int&, int)'
   64 |                         ua[i][j] = (short)min(ua[i][j],(int)((i)?(ua[i-1][j]+(int)1):(int)1));
      |                                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:64:46: note:   deduced conflicting types for parameter 'const _Tp' ('short int' and 'int')
   64 |                         ua[i][j] = (short)min(ua[i][j],(int)((i)?(ua[i-1][j]+(int)1):(int)1));
      |                                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:64:46: note:   mismatched types 'std::initializer_list<_Tp>' and 'short int'
   64 |                         ua[i][j] = (short)min(ua[i][j],(int)((i)?(ua[i-1][j]+(int)1):(int)1));
      |                                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:69:46: error: no matching function for call to 'min(short int&, int)'
   69 |                         da[i][j] = (short)min(da[i][j],(int)(da[i+1][j]+(int)1));
      |                                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:69:46: note:   deduced conflicting types for parameter 'const _Tp' ('short int' and 'int')
   69 |                         da[i][j] = (short)min(da[i][j],(int)(da[i+1][j]+(int)1));
      |                                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:69:46: note:   mismatched types 'std::initializer_list<_Tp>' and 'short int'
   69 |                         da[i][j] = (short)min(da[i][j],(int)(da[i+1][j]+(int)1));
      |                                           ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~