Problem:
Placement Games
PCK bought a new game to accompany his long autumn nights. The game board is rectangular in shape, divided into equal-sized square sections of $H$ in vertical direction and $W$ in horizontal direction. The player places one rectangular piece on the board that uses $R$ of vertical space and $C$ of horizontal space, aligning it with the boundaries of the sections. However, there are some obstacles placed in the sections, and you cannot place a piece in such a way that it uses a section with an obstacle. Also, the pieces must not be placed in different directions.
The number of points in this game is determined by the number of sections in the same row or column as the section of pieces placed, which meet the following conditions:
It is not a section where pieces or obstacles are placed.
When you move straight from each section to a piece, either vertically or horizontally, you can reach the piece without encountering any obstacles.
For example, suppose we have a board with $H=4$ and $W=5$ as shown in (a) below. The $(r,c)$ on the board represents the number of the sections, and there are obstacles in sections $(2,3)$, $(3,5)$, and $(4,4)$. As shown in (b) below, suppose a player places a piece of size $R=1$, $C=2$ using the sections $(3,2)$ and $(3,3)$. In this case, there are 9 sections $(1,2),(1,3),(2,2),(2,3),(3,1),(3,4),(3,5),(4,2),(4,3)$ in the same row and column as the placed piece, excluding the section where the piece was placed. Of these, the sections marked with a "+" in (c) below satisfy the above condition. The number of such sections is the score in this game, and in this case the score is 6.
PCK is trying to figure out the maximum number of points he can score with various board and piece sizes.
Given the information on the game board and the size of the pieces to be placed, write a program to find the maximum number of points that can be scored in the game.
Input
The input is given in the following format.
$H$ $W$ $R$ $C$
$N$
$r_1$ $c_1$
$r_2$ $c_2$
:
$r_N$ $c_N$
In the first line, we are given the number of vertical sections of the board $H$ ($1 \leq H \leq 1000$), the number of horizontal sections $W$ ($1 \leq W \leq 1000$), the number of vertical sections occupied by pieces $R$ ($1 \leq R \leq H$), and the number of horizontal sections $C$ ($1 \leq C \leq W$) are given. In the next line, the number of obstacles $N$ ($0 \leq N \leq 10,000$) is given. However, $N$ will not exceed $H \times W - R \times C$. In the subsequent $N$ line, the location of the obstacle is given. $r_i$ ($1 \leq r_i \leq H$) and $c_i$ ($1 \leq c_i \leq W$) are integers representing the vertical and horizontal positions, respectively. However, the position of the section in the upper left corner is set to $r=1,c=1$. The same position is never given more than once. You can assume that there is always at least one way to place a piece.
Output
Output the maximum number of points that PCK can obtain on one line.
Sample Input and Output
Sample Inp
...(truncated)

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define rng(i,l,r) for(int i=(l);i<(r);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rrng(i,l,r) for(int i=(r)-1;i>=(l);i--)
#define int long long
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()

int p1[2009][2009],p2[2009][2009];

signed main(){
	int H,W,R,C;cin>>H>>W>>R>>C;
	
	int N;cin>>N;
	
	vector<vector<int>> vh(H,{-1,W}),vw(W,{-1,H});
	rep(_,N){
		int i,j;cin>>i>>j;
		i--,j--;
		p1[i+1][j+1]=1;
		vh[i].pb(j);
		vw[j].pb(i);
	}
	rep(i,H)sort(all(vh[i]));
	rep(i,W)sort(all(vw[i]));
	
	rep(i,H)rep(j,W){
		if(p1[i+1][j+1])continue;
		int li=1+*--lower_bound(all(vw[j]),i);
		int ri=-1+*upper_bound(all(vw[j]),i);
		int lj=1+*--lower_bound(all(vh[i]),j);
		int rj=-1+*upper_bound(all(vh[i]),j);
		assert(li<=i&&i<=ri);assert(lj<=j&&j<=rj);
		int a1=max(0LL,li-R+1),c1=min(H,ri+1),b1=max(0LL,j-C+1),d1=min(W,j+1);
		int a2=max(0LL,i-R+1),c2=min(H,i+1),b2=max(0LL,lj-C+1),d2=min(W,rj+1);
		int a3=max(a1,a2),c3=min(c1,c2),b3=max(b1,b2),d3=min(d1,d2);
		
		p2[a1][b1]++,p2[a2][b2]++,p2[a3][b3]--;
		p2[a1][d1]--,p2[a2][d2]--,p2[a3][d3]++;
		p2[c1][b1]--,p2[c2][b2]--,p2[c3][b3]++;
		p2[c1][d1]++,p2[c2][d2]++,p2[c3][d3]--;
	}
	
	rep(i,H+9)rep(j,W+9)p1[i+1][j]+=p1[i][j],p2[i+1][j]+=p2[i][j];
	rep(i,H+9)rep(j,W+9)p1[i][j+1]+=p1[i][j],p2[i][j+1]+=p2[i][j];
	
	
	int ans=0;
	rep(i,H-R+1)rep(j,W-C+1){
		int sm=p1[i][j]+p1[i+R][j+C]-p1[i+R][j]-p1[i][j+C];
		//cout<<i<<" "<<j<<" "<<sm<<"\n";
		if(sm==0)ans=max(ans,p2[i][j]-R*C);
	}
	
	cout<<ans<<"\n";
}
accuracy: 1
time_ms: 20
memory_kb: 37848
final_rank: 9

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define rep(i,n) for(int i=0;i<(n);i++)
#define rng(i,l,r) for(int i=(l);i<(r);i++)
#define rrep(i,n) for(int i=(n)-1;i>=0;i--)
#define rrng(i,l,r) for(int i=(r)-1;i>=(l);i--)
#define int long long
#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(),(x).end()

int p1[2009][2009],p2[2009][2009];

signed main(){
	int H,W,R,C;cin>>H>>W>>R>>C;
	
	int N;cin>>N;
	
	vector<vector<int>> vh(H,{-1,W}),vw(W,{-1,H});
	rep(_,N){
		int i,j;cin>>i>>j;
		i--,j--;
		p1[i+1][j+1]=1;
		vh[i].pb(j);
		vw[j].pb(i);
	}
	rep(i,H)sort(all(vh[i]));
	rep(i,W)sort(all(vw[i]));
	
	rep(i,H)rep(j,W){
		if(p1[i+1][j+1])continue;
		int li=1+*--lower_bound(all(vw[j]),i);
		int ri=-1+*upper_bound(all(vw[j]),i);
		int lj=1+*--lower_bound(all(vh[i]),j);
		int rj=-1+*upper_bound(all(vh[i]),j);
		
		int a1=max(0LL,li-R+1),c1=min(H,ri+1),b1=max(0LL,j-C+1),d1=min(W,j+1);
		int a2=max(0LL,i-R+1),c2=min(H,i+1),b2=max(0LL,lj-C+1),d2=min(W,rj+1);
		int a3=max(a1,a2),c3=min(c1,c2),b3=max(b1,b2),d3=min(d1,d2);
		
		p2[a1][b1]++,p2[a2][b2]++,p2[a3][b3]--;
		p2[a1][d1]--,p2[a2][d2]--,p2[a3][d3]++;
		p2[c1][b1]--,p2[c2][b2]--,p2[c3][b3]++;
		p2[c1][d1]++,p2[c2][d2]++,p2[c3][d3]--;
	}
	
	rep(i,H)rep(j,W)p1[i+1][j]+=p1[i][j],p2[i+1][j]+=p2[i][j];
	rep(i,H)rep(j,W)p1[i][j+1]+=p1[i][j],p2[i][j+1]+=p2[i][j];
	
	
	int ans=0;
	rep(i,H-R+1)rep(j,W-C+1){
		int sm=p1[i][j]+p1[i+R][j+C]-p1[i+R][j]-p1[i][j+C];
		//cout<<i<<" "<<j<<" "<<sm<<"\n";
		if(sm==0)ans=max(ans,p2[i][j]-R*C);
	}
	
	cout<<ans<<"\n";
}
accuracy: 0.5666666666666667
time_ms: 10
memory_kb: 37544
final_rank: 20

code_snippet: #include<bits/stdc++.h>
using namespace std;
//#include<atcoder/all>
//using namespace atcoder;
//using mint = modint998244353;
#define int long long
signed main() {
  int h,w,r,c;
  cin >> h >> w >> r >> c;
  int n;
  cin >> n;
  vector<vector<bool>> s(h,vector<bool>(w,true));
  vector<vector<int>> a(h+1,vector<int>(w+1,0));
  for(int i = 0; i < n; i++) {
    int x,y;
    cin >> x >> y;
    x--,y--;
    s[x][y] = false;
    a[x+1][y+1]++;
  }
  for(int i = 0; i < h; i++) for(int j = 1; j <= w; j++) a[i+1][j]+=a[i][j];
  for(int i = 1; i <= h; i++) for(int j = 0; j < w; j++) a[i][j+1]+=a[i][j];
  vector<vector<int>> d(h+1,vector<int>(w+1,0)),e(h+1,vector<int>(w+1,0)),f(h+1,vector<int>(w+1,0)),g(h+1,vector<int>(w+1,0));
  for(int i = 0; i < h; i++) {
    for(int j = 0; j < w; j++) {
      if(s[i][j]) d[i+1][j+1] = d[i][j+1]+1;
    }
  }
  for(int i = h-1; i >= 0; i--) {
    for(int j = 0; j < w; j++) {
      if(s[i][j]) e[i][j+1] = e[i+1][j+1]+1;
    }
  }
  for(int i = 0; i < h; i++) {
    for(int j = 0; j < w; j++) {
      if(s[i][j]) f[i+1][j+1] = f[i+1][j]+1;
    }
    for(int j = w-1; j >= 0; j--) {
      if(s[i][j]) g[i+1][j] = g[i+1][j+1]+1;
    }
  }
  /*for(int i = 0; i < h; i++) {
    for(int j = 0; j < w; j++) cout << s[i][j];
    cout << endl;
  }*/
  for(int i = 0; i <= h; i++) for(int j = 0; j < w; j++) d[i][j+1]+=d[i][j], e[i][j+1]+=e[i][j];
  for(int i = 0; i < h; i++) for(int j = 0; j <= w; j++) f[i+1][j]+=f[i][j], g[i+1][j]+=g[i][j];
  int ans = 0;
  for(int i = 0; i <= h-r; i++) {
    for(int j = 0; j <= w-c; j++) {
      if(a[i+r][j+c]-a[i][j+c]-a[i+r][j]+a[i][j] != 0) continue;
      //[i,i+r),[j,j+c)
      //cout << i << ' ' << j << ' ' << (d[i+1][j+c]-d[i+1][j]) << ' ' << (e[i+r-1][j+c]-e[i+r-1][j]) << ' ' << (f[i+r][j+1]-f[i][j+1]) << ' ' << (g[i+r][j+c-1]-g[i][j+c-1]) << endl;
      ans = max(ans,(d[i+1][j+c]-d[i+1][j])+(e[i+r-1][j+c]-e[i+r-1][j])+(f[i+r][j+1]-f[i][j+1])+(g[i+r][j+c-1]-g[i][j+c-1])-r*2-c*2);
    }
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 42688
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
using ll=long long;
int main(){
	int H,W,R,C,N;
	cin>>H>>W>>R>>C>>N;
	int r[N+1],c[N+1];
	for(int i=1;i<=N;i++) cin>>r[i]>>c[i];
	
	
	//setに入れる
	set<int>S1[H+1],S2[W+1];
	for(int i=1;i<=N;i++){
		S1[r[i]].insert(c[i]);
		S2[c[i]].insert(r[i]);
	}
	for(int i=1;i<=H;i++){
		S1[i].insert(0);
		S1[i].insert(W+1);
	}
	for(int i=1;i<=W;i++){
		S2[i].insert(0);
		S2[i].insert(H+1);
	}
	
	int cnt1[H+1][W+1],cnt2[H+1][W+1],cnt3[H+1][W+1],cnt4[H+1][W+1];
	for(int i=0;i<=H;i++){
		for(int j=0;j<=W;j++){
			cnt1[i][j]=0;
			cnt2[i][j]=0;
			cnt3[i][j]=0;
			cnt4[i][j]=0;
		}
	}
	//1が下,2が上,3が右,4が左
	for(int i=1;i<=H;i++){
		for(int j=1;j<=W;j++){
			auto iter1=S1[i].lower_bound(j);
			auto iter2=S2[j].lower_bound(i);
			cnt1[i][j]=(*iter2)-i-1;
			cnt3[i][j]=(*iter1)-j-1;
			iter1--;
			iter2--;
			cnt2[i][j]=i-(*iter2)-1;
			cnt4[i][j]=j-(*iter1)-1;
		}
	}
	//累積和を求める
	for(int i=1;i<=H;i++){
		for(int j=1;j<=W;j++){
			cnt1[i][j]+=cnt1[i][j-1];
			cnt2[i][j]+=cnt2[i][j-1];
			cnt3[i][j]+=cnt3[i-1][j];
			cnt4[i][j]+=cnt4[i-1][j];
		}
	}
	//設置可能か判定
	int ng_cnt[H+1][W+1];
	for(int i=0;i<=H;i++){
		for(int j=0;j<=W;j++) ng_cnt[i][j]=0;
	}
	for(int i=1;i<=N;i++) ng_cnt[r[i]][c[i]]++;
	for(int i=1;i<=H;i++){
		for(int j=1;j<=W;j++) ng_cnt[i][j]+=ng_cnt[i-1][j];
	}
	for(int i=1;i<=H;i++){
		for(int j=1;j<=W;j++) ng_cnt[i][j]+=ng_cnt[i][j-1];
	}
	int ans=0;
	for(int i=R;i<=H;i++){
		for(int j=C;j<=W;j++){
			int sr=i-R,sc=j-C;
			int ng=ng_cnt[i][j]-ng_cnt[sr][j]-ng_cnt[i][sc]+ng_cnt[sr][sc];
			if(ng>=1) continue;
			int now_cnt=0;
			now_cnt+=cnt1[i][j]-cnt1[i][sc];
			now_cnt+=cnt2[sr+1][j]-cnt2[sr+1][sc];
			now_cnt+=cnt3[i][j]-cnt3[sr][j];
			now_cnt+=cnt4[i][sc+1]-cnt4[sr][sc+1];
			ans=max(ans,now_cnt);
		}
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 23304
final_rank: 6

code_snippet: #include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<tuple>
#include<utility>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<deque>
#include<stack>
#include<bit>
#include<bitset>
#include<limits>
#include<iomanip>
#include<unordered_set>

using namespace std;

long int sign(long int A){
  if (A < 0){
    return -1;
  }else if(A > 0){
    return 1;
  }else{
    return 0;
  }
}

long int orientation(pair<long int, long int>a,pair<long int, long int>b,pair<long int, long int>c){
  long int cross = (b.first - a.first) * (c.second - a.second) - (b.second - a.second) * (c.first - a.first);
  return sign(cross);
}

bool segments_intersect(pair<long int, long int>p1,pair<long int, long int>p2,pair<long int, long int>q1,pair<long int, long int>q2){
  if (max(p1.first, p2.first) < min(q1.first, q2.first) || max(q1.first, q2.first) < min(p1.first, p2.first) || max(p1.second, p2.second) < min(q1.second, q2.second) || max(q1.second, q2.second) < min(p1.second, p2.second)){
    return false;
  }
  long int o1 = orientation(p1, p2, q1);
  long int o2 = orientation(p1, p2, q2);
  long int o3 = orientation(q1, q2, p1);
  long int o4 = orientation(q1, q2, p2);
  if (o1 * o2 <= 0 && o3 * o4 <= 0){
    return true;
  }else{
    return false;
  }
}

int main(){
  long int H, W, R, C, N, Y, X;
  cin >> H >> W >> R >> C >> N;
  vector<vector<long int> > NG(H + 2, vector<long int>(W + 2, 0));
  vector<vector<long int> > NGH(H + 2);
  vector<vector<long int> > NGW(W + 2);
  for (long int i = 0; i < N; i++){
    cin >> Y >> X;
    NG.at(Y).at(X) = 1;
    NGH.at(Y).push_back(X);
    NGW.at(X).push_back(Y);
  }
  for (long int i = 0; i < H + 2; i++){
    NG.at(i).at(0) = 1;
    NG.at(i).at(W + 1) = 1;
    NGH.at(i).push_back(0);
    NGW.at(0).push_back(i);
    NGH.at(i).push_back(W + 1);
    NGW.at(W + 1).push_back(i);
  }
  for (long int i = 0; i < W + 2; i++){
    NG.at(0).at(i) = 1;
    NG.at(H + 1).at(i) = 1;
    NGH.at(0).push_back(i);
    NGW.at(i).push_back(0);
    NGH.at(H + 1).push_back(i);
    NGW.at(i).push_back(H + 1);
  }
  for (long int i = 0; i < H + 2; i++){
    sort(NGH.at(i).begin(), NGH.at(i).end());
  }
  for (long int i = 0; i < W + 2; i++){
    sort(NGW.at(i).begin(), NGW.at(i).end());
  }
  vector<vector<long int> > A(H + 2, vector<long int>(W + 2));
  vector<vector<long int> > B(H + 2, vector<long int>(W + 2));
  vector<vector<long int> > score1(H + 2, vector<long int>(W + 2, 0));
  vector<vector<long int> > score2(H + 2, vector<long int>(W + 2, 0));
  vector<vector<long int> > score1s(H + 2, vector<long int>(W + 2, 0));
  vector<vector<long int> > score2s(H + 2, vector<long int>(W + 2, 0));
  for (long int i = 0; i < H + 2; i++){
    for (long int j = 0; j < W + 2; j++){
      if (NG.at(i).at(j) == 0){
        A.at(i).at(j) = A.at(i).at(j - 1) + 1;
      }else{
        A.at(i).at(j) = 0;
      }
    }
  }
  for (long int i = 0; i < H + 2; i++){
    for (long int j = 0; j < W + 2; j++){
      if (A.at(i).at(j) >= C){
        B.at(i).at(j) = B.at(i - 1).at(j) + 1;
      }else{
        B.at(i).at(j) = 0;
      }
    }
  }
  for (long int i = 1; i < H + 1; i++){
    for (long int j = 1; j < W + 1; j++){
      if (NG.at(i).at(j) == 1){
        continue;
      }
      auto itr = upper_bound(NGH.at(i).begin(), NGH.at(i).end(), j);
      score1.at(i).at(j) += *itr - j - 1;
      itr--;
      score1.at(i).at(j) += j - *itr - 1;
      itr = upper_bound(NGW.at(j).begin(), NGW.at(j).end(), i);
      score2.at(i).at(j) += *itr - i - 1;
      itr--;
      score2.at(i).at(j) += i - *itr - 1;
    }
  }
  for (long int i = 1; i < H + 2; i++){
    for (long int j = 0; j < W + 2; j++){
      score1s.at(i).at(j) = score1s.at(i - 1).at(j) + score1.at(i).at(j);
    }
  }
  for (long int i = 0; i < H + 2; i++){
    for (long int j = 1; j < W + 2; j++){
      score2s.at(i).at(j) = score2s.at(i).at(j - 1) + score2.at(i).at(j);
    }
  }
  long int ans = 0;
  for (long int i = R; i < H + 1; i++){
    for (long int j = C; j < W + 1; j++){
      if (B.at(i).at(j) >= R){
        ans = max(ans, score1s.at(i).at(j) - score1s.at(i - R).at(j) + score2s.at(i).at(j) - score2s.at(i).at(j - C) - (R - 1) * C - (C - 1) * R);
      }
    }
  }
cout << ans << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 58752
final_rank: 13

code_snippet: #include<bits/stdc++.h>

using namespace std;

#define rep(i, l, n) for(int i = int(l); i < int(n); i++)
#define ll long long
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()

template<class T> bool chmin(T &a, T b) {if(a > b) {a = b; return true;} return false;}
template<class T> bool chmax(T &a, T b) {if(a < b) {a = b; return true;} return false;}
template<class T> using spq = priority_queue<T, vector<T>, greater<T>>;

// bool -> Yes/No
string answer(bool b) {return b ? "Yes" : "No";}

void fix(int k) {cout << fixed << setprecision(k);}

const int inf = 2e9;
const long long INF = 2e18;
const long double eps = 1e-12;
const long double pi = acos(-1);
int dx[] = {0, -1, 0, 1, -1, -1, 1, 1}, dy[] = {1, 0, -1, 0, 1, -1, -1, 1};

void main_program() {
    int h, w, r, c; cin >> h >> w >> r >> c;
    vector<int> oh[h], ow[w];
    rep(i, 0, h) oh[i].emplace_back(-1), oh[i].emplace_back(w);
    rep(i, 0, w) ow[i].emplace_back(-1), ow[i].emplace_back(h);
    int grid[h + 1][w + 1];
    rep(i, 0, h + 1) rep(j, 0, w + 1) grid[i][j] = 0;
    int n; cin >> n;
    while(n--) {
        int x, y; cin >> x >> y;
        grid[x--][y--]++;
        oh[x].emplace_back(y);
        ow[y].emplace_back(x);
    }
    rep(i, 1, h + 1) rep(j, 1, w + 1) grid[i][j] += grid[i - 1][j] + grid[i][j - 1] - grid[i - 1][j - 1];
    rep(i, 0, h) sort(all(oh[i]));
    rep(i, 0, w) sort(all(ow[i]));
    int distsum[h - r + 1][w - c + 1];
    // 進行方向のものを数える
    rep(i, 0, h - r + 1) {
        spq<pair<int, vector<int>::iterator>> f, b;
        int fsum = 0, bsum = -r;
        rep(j, 0, r) {
            b.emplace(oh[i + j][1], oh[i + j].begin());
            auto itr = lower_bound(all(oh[i + j]), c);
            f.emplace(*itr, itr); fsum += *itr;
        }
        rep(j, 0, w - c + 1) {
            distsum[i][j] = 0;
            if(grid[i + r][j + c] + grid[i][j] - grid[i][j + c] - grid[i + r][j]) continue;
            while(f.top().first < j + c) {
                auto[val, itr] = f.top(); f.pop();
                fsum += *(++itr) - val;
                f.emplace(*itr, itr);
            }
            while(b.top().first < j) {
                auto[val, itr] = b.top(); b.pop();
                bsum += val - *(itr++);
                b.emplace(*next(itr), itr);
            }
            distsum[i][j] += fsum - (j + c) * r;
            distsum[i][j] += (j - 1) * r - bsum;
        }
    }
    // 向きを変えて同じことをやる
    rep(i, 0, w - c + 1) {
        spq<pair<int, vector<int>::iterator>> f, b;
        int fsum = 0, bsum = -c;
        rep(j, 0, c) {
            b.emplace(ow[i + j][1], ow[i + j].begin());
            auto itr = lower_bound(all(ow[i + j]), r);
            f.emplace(*itr, itr); fsum += *itr;
        }
        rep(j, 0, h - r + 1) {
            if(grid[j + r][i + c] + grid[j][i] - grid[j][i + c] - grid[j + r][i]) continue;
            while(f.top().first < j + r) {
                auto[val, itr] = f.top(); f.pop();
                fsum += *(++itr) - val;
                f.emplace(*itr, itr);
            }
            while(b.top().first < j) {
                auto[val, itr] = b.top(); b.pop();
                bsum += val - *(itr++);
                b.emplace(*next(itr), itr);
            }
            distsum[j][i] += fsum - (j + r) * c;
            distsum[j][i] += (j - 1) * c - bsum;
        }
    }/*
    rep(i, 0, h - r + 1) {
        rep(j, 0, w - c + 1) cout << distsum[i][j] << " ";
        cout << "\n";
    }*/
    int ans = 0;
    rep(i, 0, h - r + 1) rep(j, 0, w - c + 1) chmax(ans, distsum[i][j]);
    cout << ans << "\n";
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T = 1;
    //cin >> T;
    while(T--) {
        main_program();
    }
}
accuracy: 1
time_ms: 20
memory_kb: 11272
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
    // step 1. input
    int h, w, r, c; cin >> h >> w >> r >> c;
    int k; cin >> k;
    vector<vector<bool>> shogai(h, vector<bool>(w, false));
    for (int i = 0; i < k; i++) {
        int x, y; cin >> x >> y; x--; y--;
        shogai[x][y] = true;
    }

    // step 2. ruisekiwa
    vector<vector<int>> wa(h + 1, vector<int>(w + 1, 0));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            wa[i + 1][j + 1] = (shogai[i][j] ? 1 : 0);
        }
    }
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            wa[i][j] += wa[i - 1][j];
        }
    }
    for (int i = 1; i <= h; i++) {
        for (int j = 1; j <= w; j++) {
            wa[i][j] += wa[i][j - 1];
        }
    }

    // step 3. solve init
    vector<vector<int>> sumL(h + 1, vector<int>(w + 1, 0));
    vector<vector<int>> sumR(h + 1, vector<int>(w + 1, 0));
    vector<vector<int>> sumU(h + 1, vector<int>(w + 1, 0));
    vector<vector<int>> sumD(h + 1, vector<int>(w + 1, 0));
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (shogai[i][j] == true) sumL[i][j] = 0;
            else sumL[i][j] = (j == 0 ? 0 : sumL[i][j - 1]) + 1;
        }
    }
    for (int i = 0; i < h; i++) {
        for (int j = w - 1; j >= 0; j--) {
            if (shogai[i][j] == true) sumR[i][j] = 0;
            else sumR[i][j] = sumR[i][j + 1] + 1;
        }
    }
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (shogai[i][j] == true) sumU[i][j] = 0;
            else sumU[i][j] = (i == 0 ? 0 : sumU[i - 1][j]) + 1;
        }
    }
    for (int i = h - 1; i >= 0; i--) {
        for (int j = 0; j < w; j++) {
            if (shogai[i][j] == true) sumD[i][j] = 0;
            else sumD[i][j] = sumD[i + 1][j] + 1;
        }
    }
    /*for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            cout << sumD[i][j] << " ";
        }
        cout << endl;
    }*/

    // step 4. solve init (ruisekiwa)
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) sumU[i][j + 1] += sumU[i][j];
        for (int j = 0; j < w; j++) sumD[i][j + 1] += sumD[i][j];
    }
    for (int j = 0; j < w; j++) {
        for (int i = 0; i < h; i++) sumL[i + 1][j] += sumL[i][j];
        for (int i = 0; i < h; i++) sumR[i + 1][j] += sumR[i][j];
    }

    // step 5. solve main
    int max_score = 0;
    for (int i = 0; i <= h - r; i++) {
        for (int j = 0; j <= w - c; j++) {
            int sum_shogai = wa[i + r][j + c] + wa[i][j] - wa[i + r][j] - wa[i][j + c];
            if (sum_shogai > 0) continue;

            // 障害物がない場合
            int score = 0;
            if (i >= 1) {
                score += sumU[i - 1][j + c - 1] - (j == 0 ? 0 : sumU[i - 1][j - 1]);
            }
            if (i <= h - r - 1) {
                score += sumD[i + r][j + c - 1] - (j == 0 ? 0 : sumD[i + r][j - 1]);
            }
            if (j >= 1) {
                score += sumL[i + r - 1][j - 1] - (i == 0 ? 0 : sumL[i - 1][j - 1]);
            }
            if (j <= w - c - 1) {
                score += sumR[i + r - 1][j + c] - (i == 0 ? 0 : sumR[i - 1][j + c]);
            }
            // cerr << i << " " << j << " " << score << endl;
            max_score = max(max_score, score);
        }
    }

    // step 6. output
    cout << max_score << endl;
    return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 23352
final_rank: 7

code_snippet: #include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <algorithm>
#include <set>
using ll = long long;
using namespace std;

int dp[1001][1001][4];

int SDP[4][1001][1001];



int sum(int a, int b, int d, int e, int dr) {
	return (SDP[dr][d][e] - SDP[dr][a-1][e]-SDP[dr][d][b-1] + SDP[dr][a-1][b-1]);
}
int main(){
	int H, W, R, C;cin >> H >> W >> R >> C;
	int N;cin >> N;
	vector<vector<int>> A(H+1, vector<int>(W+1, 0));
	for (int i = 0;i < N;i++) {
		int r, c;cin >> r >> c;
		A[r][c] = 1;
	}
	vector<vector<int>> S(H+1, vector<int>(W+1,0));
	for (int i = 0;i <= H;i++) for (int j = 0;j <= W;j++) {
		if (i == 0 or j == 0) {
			for (int k = 0;k < 4;k++) dp[i][j][k] = 0;
			continue;
		}
		S[i][j] = A[i][j] + S[i-1][j] + S[i][j-1] - S[i-1][j-1];
		if (A[i][j] == 1) {
			dp[i][j][0] = 0;
			dp[i][j][1] = 0;
			continue;
		}
		if (i == 1) {
			dp[i][j][0] = 1;
		} else {
			dp[i][j][0] = 1 + dp[i-1][j][0];
		}
		
		if (j == 1) {
			dp[i][j][1] = 1;
		} else {
			dp[i][j][1] = 1 + dp[i][j-1][1];
		}
	}
	for (int i = H;i >= 1;i--) for (int j = W;j >= 1;j--) {
		if (A[i][j] == 1) {
			dp[i][j][2] = 0;
			dp[i][j][3] = 0;
			continue;
		}
		if (i == H) {
			dp[i][j][2] = 1;
		} else {
			dp[i][j][2] = 1 + dp[i+1][j][2];
		}
		if (j == W) {
			dp[i][j][3] = 1;
		} else {
			dp[i][j][3] = 1 + dp[i][j+1][3];
		}
	}
	for (int k = 0;k < 4;k++) for (int i = 0;i <= H;i++) for (int j = 0;j <= W;j++) {
		if (i == 0 or j == 0) {
			SDP[k][i][j] = 0;
			continue;
		}
		SDP[k][i][j] = dp[i][j][k] + SDP[k][i-1][j] + SDP[k][i][j-1] - SDP[k][i-1][j-1];
		//cout << k << " " << i << " " << j << " " << dp[i][j][k] << " " << SDP[k][i][j] << endl;
	}
	int res = 0;
	for (int i = 1;i <= H-R+1;i++) for (int j = 1;j <= W-C+1;j++) {
		int s = i + R - 1;
		int t = j + C - 1;
		int cnts = S[s][t] - S[i-1][t] - S[s][j-1] + S[i-1][j-1];
		if (cnts != 0) continue;
		int sc = 0;
		if (i >= 2) {
			sc += sum(i-1, j, i-1,t, 0);
			///cout << "Z0 " << sum(i-1, j, i-1, t, 0) << endl;
		}
		if (j >= 2) {
			sc += sum(i, j-1, s, j-1, 1);
		//	cout << "Z1 " <<  sum(i, j-1, s, j-1, 1) << " " << i << " " << j-1 << " " << s << " " << j-1 << " " << endl;
			//cout << "V " << SDP[1][s][j-1] << " " << SDP[1][i-1][j-1]  << " " << SDP[1][s][j-2] << " " << SDP[1][i-1][j-2] << endl;
		}
		
		if (s < H) {
			sc += sum(s+1, j, s+1, t, 2);
		//	cout <<"Z2 " << sum(s+1, j, s+1, t, 2) << endl;
		}
		
		if (t < W) {
			sc += sum(i, t +1, s, t+1, 3);
		//	cout << "Z3 " << sum(i, t+1, s, t+1, 3) << endl;
		}
		
	
		res = max(res, sc);
	}		
	cout << res << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 42876
final_rank: 8

code_snippet: #include<bits/stdc++.h>
using namespace std;

#define ll long long
#define vec vector
#define vi vector<int>
#define vll vector<ll>
#define pq priority_queue
#define pb push_back
#define ep emplace_back
#define rep(i, l, r) for(ll i = (ll)(l); i < (ll)(r); i++)
#define ld long double

const ll INf = 1e9;
const ll inf = 5e18;

const vector<ll> di = {-1, 0, 1, 0}, dj = {0, 1, 0, -1};

template<class T>
bool chmax(T&a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for(int i = 0; i < (int)v.size(); i++) {
        if(i != (int)v.size() - 1) os << v[i] << " ";
        else os << v[i];
    }
    return os;
}

int main() {
    int h, w, r, c, n; cin >> h >> w >> r >> c >> n;
    vec<vi> g(h + 1, vi(w + 1, 0));
    rep(i, 0, n) {
        int ri, ci; cin >> ri >> ci;
        g[ri][ci]++;
    }
    vec<vi> rs(h, vi(w, 0)), cs(h, vi(w, 0));
    rep(i, 0, h) {
        rep(j, 0, w) {
            rep(k, j, w) {
                if(g[i + 1][k + 1] == 1) {
                    rep(l, j, k) rs[i][l] = k - j;
                    j = k;
                    break;
                }
                if(k == w - 1) {
                    rep(l, j, w) rs[i][l] = w - j;
                    j = w;
                    break;
                }
            }
        }
    }
    rep(i, 0, w) {
        rep(j, 0, h) {
            rep(k, j, h) {
                if(g[k + 1][i + 1] == 1) {
                    rep(l, j, k) cs[l][i] = k - j;
                    j = k;
                    break;
                }
                if(k == h - 1) {
                    rep(l, j, h) cs[l][i] = h - j;
                    j = h;
                    break;
                }
            }
        }
    }
    rep(i, 0, h + 1) rep(j, 1, w + 1) if(j != 0) g[i][j] += g[i][j - 1];
    rep(i, 0, w + 1) rep(j, 1, h + 1) if(j != 0) g[j][i] += g[j - 1][i];
    vec<vi> cnts(h - r + 1, vi(w - c + 1, -1));
    int ans = 0;
    rep(i, 0, h - r + 1) {
        if(g[i + r][c] + g[i][0] - g[i][c] - g[i + r][0] == 0) {
            cnts[i][0] = 0;
            if(i != 0) {
                if(cnts[i - 1][0] != -1) {
                    cnts[i][0] = cnts[i - 1][0];
                    cnts[i][0] += rs[i + r - 1][0] - c;
                    cnts[i][0] -= rs[i - 1][0] - c;
                }
                else {
                    rep(j, 0, r) cnts[i][0] += rs[i + j][0] - c;
                    rep(j, 0, c) cnts[i][0] += cs[i][j] - r;
                }
            }
            else {
                rep(j, 0, r) cnts[i][0] += rs[i + j][0] - c;
                rep(j, 0, c) cnts[i][0] += cs[i][j] - r;
            }
        }
        chmax(ans, cnts[i][0]);
        rep(j, 1, w - c + 1) {
            if(g[i + r][j + c] + g[i][j] - g[i][j + c] - g[i + r][j] != 0) continue;
            if(cnts[i][j - 1] != -1) {
                cnts[i][j] = cnts[i][j - 1];
                cnts[i][j] += cs[i][j + c - 1] - r;
                cnts[i][j] -= cs[i][j - 1] - r;
            }
            else if(i != 0 && cnts[i - 1][j] != -1) {
                cnts[i][j] = cnts[i - 1][j];
                cnts[i][j] += rs[i + r - 1][j] - c;
                cnts[i][j] -= rs[i - 1][j] - c;
            }
            else {
                cnts[i][j] = 0;
                rep(k, 0, r) cnts[i][j] += rs[i + k][j] - c;
                rep(k, 0, c) cnts[i][j] += cs[i][j + k] - r;
            }
            chmax(ans, cnts[i][j]);
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 19124
final_rank: 1

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, s, e) for (int i = (int)(s); i < (int)(e); ++i)

int main() {
	cin.tie(nullptr);
	ios_base::sync_with_stdio(false);
	
	int H, W, R, C, N;
	cin >> H >> W >> R >> C >> N;
	
	vector wall(H, vector<bool>(W));
	vector cnt(H, vector<int>(W));
	rep(i, 0, N) {
		int r, c;
		cin >> r >> c;
		--r, --c;
		wall[r][c] = true;
		++cnt[r][c];
	}
	
	rep(i, 1, H) rep(j, 0, W) cnt[i][j] += cnt[i - 1][j];
	rep(i, 0, H) rep(j, 1, W) cnt[i][j] += cnt[i][j - 1];
	
	vector vert(H, vector<ll>(W)), hori(H, vector<ll>(W));
	// vert
	rep(j, 0, W) {
		rep(i, 0, H) {
			if (wall[i][j]) continue;
			
			if (i == 0 || wall[i - 1][j]) {
				int cnter = 1;
				while (true) {
					if (i + cnter >= H || wall[i + cnter][j]) break;
					++cnter;
				}
				vert[i][j] = cnter;
			} else {
				vert[i][j] = vert[i - 1][j];
			}
		}
	}
	
	// hori
	rep(i, 0, H) {
		rep(j, 0, W) {
			if (wall[i][j]) continue;
			
			if (j == 0 || wall[i][j - 1]) {
				int cnter = 1;
				while (true) {
					if (j + cnter >= W || wall[i][j + cnter]) break;
					++cnter;
				}
				hori[i][j] = cnter;
			} else {
				hori[i][j] = hori[i][j - 1];
			}
		}
	}
	
	rep(i, 0, H) rep(j, 1, W) vert[i][j] += vert[i][j - 1];
	rep(i, 1, H) rep(j, 0, W) hori[i][j] += hori[i - 1][j];
	
	ll ans = 0;
	rep(i, 0, H - R + 1) rep(j, 0, W - C + 1) {
		int wallcnt = cnt[i + R - 1][j + C - 1];
		if (i != 0) wallcnt -= cnt[i - 1][j + C - 1];
		if (j != 0) wallcnt -= cnt[i + R - 1][j - 1];
		if (i != 0 && j != 0) wallcnt += cnt[i - 1][j - 1];
		
		if (wallcnt > 0) continue;
		
		ll res = vert[i][j + C - 1] + hori[i + R - 1][j] - 2*R*C;
		if (j != 0) res -= vert[i][j - 1];
		if (i != 0) res -= hori[i - 1][j];
		
		ans = max(ans, res);
	}
	cout << ans << '\n';
}
accuracy: 1
time_ms: 10
memory_kb: 22876
final_rank: 4

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main(){
	int h,w,r,c;
	cin >> h >> w >> r >> c;
	int n;
	cin >> n;
	vector<vector<int>> x(h,vector<int>()),y(w,vector<int>());
	for(int i = 0;i < n;i++){
		int a,b;
		cin >> a >> b;
		a--;b--;
		x[a].push_back(b);
		y[b].push_back(a);
	}
	for(int i = 0;i < h;i++){
		sort(x[i].begin(),x[i].end());
	}
	for(int i = 0;i < w;i++){
		sort(y[i].begin(),y[i].end());
	}
	vector<vector<vector<int>>> t(4,vector<vector<int>>(h,vector<int>(w,0)));
	vector<vector<vector<int>>> a(4,vector<vector<int>>(h,vector<int>(w,0)));
	for(int i = 0;i < h;i++){
		int now = 0,cou = 0;
		for(int j = 0;j < w;j++){
			cou++;
			if(now < (int)x[i].size() && x[i][now] == j){
				cou = 0;
				now++;
			}
			a[0][i][j] = cou;
			t[0][i][j] += cou;
			if(i - r >= 0){
				t[0][i - r][j] -= cou;
			}
		}
	}
	for(int i = 0;i < h;i++){
		int now = (int)x[i].size() - 1,cou = 0;
		for(int j = w - 1;j >= 0;j--){
			cou++;
			if(now >= 0 && x[i][now] == j){
				cou = 0;
				now--;
			}
			a[1][i][j] = cou;
			t[1][i][j] += cou;
			if(i - r >= 0){
				t[1][i - r][j] -= cou;
			}
		}
	}
	for(int i = h - 2;i >= 0;i--){
		for(int j = 0;j < w;j++){
			t[0][i][j] += t[0][i + 1][j];
			t[1][i][j] += t[1][i + 1][j];
		}
	}
	for(int j = 0;j < w;j++){
		int now = 0,cou = 0;
		for(int i = 0;i < h;i++){
			cou++;
			if(now < (int)y[j].size() && y[j][now] == i){
				cou = 0;
				now++;
			}
			a[2][i][j] = cou;
			t[2][i][j] += cou;
			if(j - c >= 0){
				t[2][i][j - c] -= cou;
			}
		}
	}
	for(int j = 0;j < w;j++){
		int now = (int)y[j].size() - 1,cou = 0;
		for(int i = h - 1;i >= 0;i--){
			cou++;
			if(now >= 0 && y[j][now] == i){
				cou = 0;
				now--;
			}
			a[3][i][j] = cou;
			t[3][i][j] += cou;
			if(j - c >= 0){
				t[3][i][j - c] -= cou;
			}
		}
	}
	for(int i = 0;i < h;i++){
		for(int j = w - 2;j >= 0;j--){
			t[2][i][j] += t[2][i][j + 1];
			t[3][i][j] += t[3][i][j + 1];
		}
	}
	int ans = 0;
	for(int i = 0;i < h;i++){
		int cou = 0;
		for(int j = 0;j < w;j++){
			if(a[3][i][j] >= r){
				cou++;
			}
			if(j - c >= 0 && a[3][i][j - c] >= r){
				cou--;
			}
			if(cou < c) continue;
			int it = (j - c >= 0 ? t[0][i][j - c] : 0) + (j + 1 < w ? t[1][i][j + 1] : 0) + (i - 1 >= 0 && j - c + 1 >= 0 ? t[2][i - 1][j - c + 1] : 0) + (i + r < h && j - c + 1 >= 0 ? t[3][i + r][j - c + 1] : 0);
			ans = max(ans,it);
		}
	}
	cout << ans << endl;
}
accuracy: 1
time_ms: 40
memory_kb: 38724
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main(){
	int h,w,r,c;
	cin >> h >> w >> r >> c;
	int n;
	cin >> n;
	vector<vector<int>> x(h,vector<int>()),y(w,vector<int>());
	for(int i = 0;i < n;i++){
		int a,b;
		cin >> a >> b;
		a--;b--;
		x[a].push_back(b);
		y[b].push_back(a);
	}
	for(int i = 0;i < h;i++){
		sort(x[i].begin(),x[i].end());
	}
	for(int i = 0;i < w;i++){
		sort(y[i].begin(),y[i].end());
	}
	vector<vector<vector<int>>> t(4,vector<vector<int>>(h,vector<int>(w,0)));
	vector<vector<vector<int>>> a(4,vector<vector<int>>(h,vector<int>(w,0)));
	for(int i = 0;i < h;i++){
		int now = 0,cou = 0;
		for(int j = 0;j < w;j++){
			cou++;
			if(now < (int)x[i].size() && x[i][now] == j){
				cou = 0;
				now++;
			}
			a[0][i][j] = cou;
			t[0][i][j] += cou;
			if(i - r >= 0){
				t[0][i - r][j] -= cou;
			}
		}
	}
	for(int i = 0;i < h;i++){
		int now = (int)x[i].size() - 1,cou = 0;
		for(int j = w - 1;j >= 0;j--){
			cou++;
			if(now >= 0 && x[i][now] == j){
				cou = 0;
				now--;
			}
			a[1][i][j] = cou;
			t[1][i][j] += cou;
			if(i - r >= 0){
				t[1][i - r][j] -= cou;
			}
		}
	}
	for(int i = h - 2;i >= 0;i--){
		for(int j = 0;j < w;j++){
			t[0][i][j] += t[0][i + 1][j];
			t[1][i][j] += t[1][i + 1][j];
		}
	}
	for(int j = 0;j < w;j++){
		int now = 0,cou = 0;
		for(int i = 0;i < h;i++){
			cou++;
			if(now < (int)y[j].size() && y[j][now] == i){
				cou = 0;
				now++;
			}
			a[2][i][j] = cou;
			t[2][i][j] += cou;
			if(j - c >= 0){
				t[2][i][j - c] -= cou;
			}
		}
	}
	for(int j = 0;j < w;j++){
		int now = (int)y[j].size() - 1,cou = 0;
		for(int i = h - 1;i >= 0;i--){
			cou++;
			if(now >= 0 && y[j][now] == i){
				cou = 0;
				now--;
			}
			a[3][i][j] = cou;
			t[3][i][j] += cou;
			if(j - c >= 0){
				t[3][i][j - c] -= cou;
			}
		}
	}
	for(int i = 0;i < h;i++){
		for(int j = w - 2;j >= 0;j--){
			t[2][i][j] += t[2][i][j + 1];
			t[3][i][j] += t[3][i][j + 1];
		}
	}
	int ans = 0;
	for(int i = 0;i < h;i++){
		int cou = 0;
		for(int j = 0;j < w;j++){
			if(a[3][i][j] >= r){
				cou++;
			}
			if(j - c >= 0 && a[3][i][j - c] >= r){
				cou--;
			}
			if(cou < c) continue;
			int it = (j - c >= 0 ? t[0][i][j - c] : 0) + (j + 1 < w ? t[1][i][j + 1] : 0) + (i - 1 >= 0 && j - c >= 0 ? t[2][i - 1][j - c] : 0) + (i + r < h && j - c >= 0 ? t[3][i + r][j - c] : 0);
			ans = max(ans,it);
		}
	}
	cout << ans << endl;
}
accuracy: 0.9666666666666667
time_ms: 40
memory_kb: 38748
final_rank: 19

code_snippet: #include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
using ll = long long;
bool chmin(ll &a,ll b){
  if(a>b){
    a=b;
    return true;
  }
  return false;
}
int main(){
  ll n,m,x,y;
  cin>>n>>m>>x>>y;
  vector<vector<ll>> f(n,vector<ll>(m));
  ll k;
  cin>>k;
  for(int i=0;i<k;i++){
    ll p,q;
    cin>>p>>q;
    p--;
    q--;
    f[p][q]=1;
  }
  vector<vector<ll>> a(n,vector<ll>(m+1)),b(n,vector<ll>(m+1)),c(n+1,vector<ll>(m)),d(n+1,vector<ll>(m));
  //上、下、左、右
  for(int i=0;i<m;i++){
    ll now=0;
    for(int j=0;j<n;j++){
      a[j][i+1]=now;
      if(f[j][i]) now=0;
      else now++;
    }
  }
  for(int i=0;i<m;i++){
    ll now=0;
    for(int j=n-1;j>=0;j--){
      b[j][i+1]=now;
      if(f[j][i]) now=0;
      else now++;
    }
  }
  for(int i=0;i<n;i++){
    ll now=0;
    for(int j=0;j<m;j++){
      c[i+1][j]=now;
      if(f[i][j]) now=0;
      else now++;
    }
  }
  for(int i=0;i<n;i++){
    ll now=0;
    for(int j=m-1;j>=0;j--){
      d[i+1][j]=now;
      if(f[i][j]) now=0;
      else now++;
    }
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++){
      a[i][j+1]+=a[i][j];
      b[i][j+1]+=b[i][j];
      c[i+1][j]+=c[i][j];
      d[i+1][j]+=d[i][j];
    }
  }
  vector<vector<ll>> g(n+1,vector<ll>(m+1));
  for(int i=0;i<n;i++){
    for(int j=0;j<m;j++) g[i+1][j+1]=f[i][j];
  }
  for(int i=0;i<=n;i++){
    for(int j=0;j<m;j++) g[i][j+1]+=g[i][j];
  }
  for(int i=0;i<n;i++){
    for(int j=0;j<=m;j++) g[i+1][j]+=g[i][j];
  }
  ll ans=0;
  for(int i=0;i+x-1<n;i++){
    for(int j=0;j+y-1<m;j++){
      if(g[i+x][j+y]-g[i+x][j]-g[i][j+y]+g[i][j]) continue;
      ll sum=0;
      sum+=a[i][j+y]-a[i][j];
      sum+=b[i+x-1][j+y]-b[i+x-1][j];
      sum+=c[i+x][j]-c[i][j];
      sum+=d[i+x][j+y-1]-d[i][j+y-1];
      ans=max(ans,sum);
    }
  }
  cout<<ans<<endl;
}
accuracy: 1
time_ms: 30
memory_kb: 50332
final_rank: 16

code_snippet: #include <bits/stdc++.h>
#define rep(i,n) for(int i=0;i<(n);++i)
using namespace std;
using ll = long long;

int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int H,W,R,C,N;
    cin >> H >> W >> R >> C >> N;
    vector<vector<ll>> A(H,vector<ll>(W,0));
    rep(i,N){
        int r,c;
        cin >> r >> c;
        r--;c--;
        A[r][c] = 1;
    }   
    vector<vector<vector<ll>>> ren(4,vector<vector<ll>>(H,vector<ll>(W,0)));
    rep(i,H){
        rep(j,W-1){
            if(A[i][j]==1) ren[0][i][j+1] = 0;
            else ren[0][i][j+1] = ren[0][i][j]+1;
        }
    }
    rep(i,H){
        rep(j,W-1){
            if(A[i][W-j-1]==1) ren[1][i][W-j-2] = 0;
            else ren[1][i][W-j-2] = ren[1][i][W-j-1]+1;
        }
    }
    rep(j,W){
        rep(i,H-1){
            if(A[i][j]==1) ren[2][i+1][j] = 0;
            else ren[2][i+1][j] = ren[2][i][j]+1;
        }
    }
    rep(j,W){
        rep(i,H-1){
            if(A[H-i-1][j]==1) ren[3][H-i-2][j] = 0;
            else ren[3][H-i-2][j] = ren[3][H-i-1][j]+1;
        }
    }

    vector<vector<vector<ll>>> cw(2,vector<vector<ll>>(H-R+1,vector<ll>(W,0)));
    rep(j,W){
        rep(i,R) {
            cw[0][H-R][j] += ren[0][H-i-1][j];
            cw[1][H-R][j] += ren[1][H-i-1][j];
        }
        rep(i,H-R){
            cw[0][H-R-i-1][j] = cw[0][H-R-i][j]+ren[0][H-R-i-1][j]-ren[0][H-i-1][j];
            cw[1][H-R-i-1][j] = cw[1][H-R-i][j]+ren[1][H-R-i-1][j]-ren[1][H-i-1][j];
        }
    }

    vector<vector<ll>> sum(H+1,vector<ll>(W+1,0));
    rep(i,H){
        rep(j,W){
            sum[i+1][j+1] += A[i][j]+sum[i][j+1];
        }
    }
    rep(i,H){
        rep(j,W){
            sum[i+1][j+1] += sum[i+1][j];
        }
    }

    
    ll ans = -1;
    rep(i,H-R+1){
        ll now = 0;
        rep(j,R) now += ren[0][i+j][0]; 
        rep(j,R) now += ren[1][i+j][C-1];
        rep(j,C) now += ren[2][i][j]; 
        rep(j,C) now += ren[3][i+R-1][j]; 
        if(sum[i+R][C]-sum[i+R][0]-sum[i][C]+sum[i][0]==0) ans = max(ans,now);

        rep(j,W-C){
            now -= cw[0][i][j];
            now += cw[0][i][j+1];
            now -= cw[1][i][j+C-1];
            now += cw[1][i][j+C];

            now -= ren[2][i][j];
            now += ren[2][i][j+C];
            now -= ren[3][i+R-1][j];
            now += ren[3][i+R-1][j+C];
                        

            if(sum[i+R][j+C+1]-sum[i+R][j+1]-sum[i][j+C+1]+sum[i][j+1]==0) ans = max(ans,now);
        }
    }
    cout << ans << endl;
}
accuracy: 1
time_ms: 60
memory_kb: 65880
final_rank: 18

code_snippet: #include <bits/stdc++.h>

std::vector<std::vector<int>> f(const std::vector<std::vector<bool>>& A) {
    std::vector s(A.size(), std::vector<int>(A[0].size()));
    for (int i{} ; i < (int)A.size() ; i++) {
        int val{};
        for (int j{} ; j < (int)A[i].size() ; j++) {
            s[i][j] = val;
            val = (A[i][j] ? 0 : val + 1);
        }
    }
    std::vector res(A.size() + 1, std::vector<int>(A[0].size()));
    for (int i{} ; i < (int)A[0].size() ; i++) {
        for (int j{} ; j < (int)A.size() ; j++) {
            res[j + 1][i] = res[j][i] + s[j][i];
        }
    }
    return res;
}

int main() {
    int H, W, R, C, N;
    std::cin >> H >> W >> R >> C >> N;
    std::vector A(H, std::vector<bool>(W)), B(W, std::vector<bool>(H));
    for (int i{} ; i < N ; i++) {
        int r, c;
        std::cin >> r >> c;
        r--; c--;
        A[r][c] = B[c][H - r - 1] = true;
    }
    std::vector sum(H + 1, std::vector<int>(W + 1));
    for (int i{} ; i < H ; i++) {
        for (int j{} ; j < W ; j++) {
            sum[i + 1][j + 1] = -sum[i][j] + sum[i + 1][j] + sum[i][j + 1] + A[i][j];
        }
    }
    auto left{f(A)};
    auto down{f(B)};
    for (auto& x : A) std::reverse(x.begin(), x.end());
    for (auto& x : B) std::reverse(x.begin(), x.end());
    auto right{f(A)};
    auto up{f(B)};
    int ans{};
    for (int i{} ; i + R - 1 < H ; i++) {
        for (int j{} ; j + C - 1 < W ; j++) {
            if (sum[i + R][j + C] - sum[i + R][j] - sum[i][j + C] + sum[i][j] == 0) {
                int val{};
                val += left[i + R][j] - left[i][j];
                val += right[i + R][W - (j + C)] - right[i][W - (j + C)];
                val += down[j + C][H - (i + R)] - down[j][H - (i + R)];
                val += up[j + C][i] - up[j][i];
                ans = std::max(ans, val);
            }
        }
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 50
memory_kb: 27108
final_rank: 14

code_snippet: #include <bits/stdc++.h>

std::vector<std::vector<int>> f(const std::vector<std::vector<bool>>& A) {
    std::vector s(A.size(), std::vector<int>(A[0].size()));
    for (int i{} ; i < (int)A.size() ; i++) {
        int val{};
        for (int j{} ; j < (int)A[i].size() ; j++) {
            s[i][j] = val;
            val = (A[i][j] ? 0 : val + 1);
        }
    }
    std::vector res(A.size() + 1, std::vector<int>(A[0].size()));
    for (int i{} ; i < (int)A[0].size() ; i++) {
        for (int j{} ; j < (int)A.size() ; j++) {
            res[j + 1][i] = res[j][i] + s[j][i];
        }
    }
    return res;
}

int main() {
    int H, W, R, C, N;
    std::cin >> H >> W >> R >> C >> N;
    std::vector A(H, std::vector<bool>(W)), B(W, std::vector<bool>(H));
    for (int i{} ; i < N ; i++) {
        int r, c;
        std::cin >> r >> c;
        r--; c--;
        A[r][c] = B[c][H - r - 1] = true;
    }
    std::vector sum(H + 1, std::vector<int>(W + 1));
    for (int i{} ; i < H ; i++) {
        for (int j{} ; j < W ; j++) {
            sum[i + 1][j + 1] = -sum[i][j] + sum[i + 1][j] + sum[i][j + 1] + A[i][j];
        }
    }
    std::vector<std::pair<int, int>> ok;
    for (int i{} ; i + R <= H ; i++) {
        for (int j{} ; j + C <= W ; j++) {
            int val{sum[i + R][j + C] - sum[i + R][j] - sum[i][j + C] + sum[i][j]};
            if (val == 0) ok.push_back(std::make_pair(i, j));
        }
    }
    auto left{f(A)};
    auto down{f(B)};
    for (auto& x : A) std::reverse(x.begin(), x.end());
    for (auto& x : B) std::reverse(x.begin(), x.end());
    auto right{f(A)};
    auto up{f(B)};
    int ans{};
    for (auto [i, j] : ok) {
        int val{};
        val += left[i + R][j] - left[i][j];
        val += right[i + R][W - (j + C)] - right[i][W - (j + C)];
        val += down[j + C][H - (i + R)] - down[j][H - (i + R)];
        val += up[j + C][i] - up[j][i];
        ans = std::max(ans, val);
    }
    std::cout << ans << '\n';
}
accuracy: 1
time_ms: 50
memory_kb: 35380
final_rank: 17

code_snippet: #include <bits/stdc++.h>

int main() {
    int h, w, r, c;
    std::cin >> h >> w >> r >> c;
    int n;
    std::cin >> n;
    std::vector obs(h, std::vector<int>(w));
    for (int i = 0; i < n; i++) {
        int a, b;
        std::cin >> a >> b;
        a--; b--;
        obs[a][b] = 1;
    }
    std::vector<std::vector<std::vector<int>>> ans(4);
    auto rotate = [](const std::vector<std::vector<int>>& data) {
        int h = data.size();
        int w = data.front().size();
        std::vector new_data(w, std::vector<int>(h));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                new_data[j][h - i - 1] = data[i][j];
            }
        }
        return new_data;
    };
    for (int q = 0; q < 4; q++) {
        ans[q].assign(h, std::vector<int>(w, -1));
        std::vector obs_count(h + 1, std::vector<int>(w + 1));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                obs_count[i + 1][j + 1] = obs_count[i + 1][j] + obs_count[i][j + 1] - obs_count[i][j] + obs[i][j];
            }
        }
        auto can_put = [&](int i, int j) {
            if (i + r > h || j + c > w) return false;
            int count = 0;
            count += obs_count[i + r][j + c];
            count += obs_count[i][j];
            count -= obs_count[i][j + c];
            count -= obs_count[i + r][j];
            return count == 0;
        };
        std::vector left(h + 1, std::vector<int>(w));
        for (int i = 0; i < h; i++) {
            for (int j = 0; j + 1 < w; j++) {
                if (obs[i][j] == 0) {
                    left[i + 1][j + 1] = left[i + 1][j] + 1;
                } else {
                    left[i + 1][j] = 0;
                }
            }
        }
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                left[i + 1][j] += left[i][j];
            }
        }

        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                if (can_put(i, j)) {
                    ans[q][i][j] = left[i + r][j] - left[i][j];
                }
            }
        }

        obs = rotate(obs);
        std::swap(h, w);
        std::swap(r, c);
        for (int i = 0; i < 4 - q; i++) {
            ans[q] = rotate(ans[q]);
        }
    }

    int result = 0;
    for (int i = 0; i < h; i++) {
        for (int j = 0; j < w; j++) {
            if (ans[0][i][j] != -1) {
                int now = 0;
                assert(ans[0][i][j] != -1);
                now += ans[0][i][j];
                assert(ans[1][i + r - 1][j] != -1);
                now += ans[1][i + r - 1][j];
                assert(ans[2][i + r - 1][j + c - 1] != -1);
                now += ans[2][i + r - 1][j + c - 1];
                assert(ans[3][i][j + c - 1] != -1);
                now += ans[3][i][j + c - 1];
                result = std::max(result, now);
            }
        }
    }
    auto print = [](const std::vector<std::vector<int>>& a) {
        int h = a.size();
        int w = a.front().size();
        for (int i = 0; i < h; i++) {
            for (int j = 0; j < w; j++) {
                std::cerr << a[i][j] << ' ';
            }
            std::cerr << std::endl;
        }
    };
    // print(ans[0]);

    std::cout << result << std::endl;
}
accuracy: 1
time_ms: 40
memory_kb: 34832
final_rank: 11

code_snippet: #include<bits/stdc++.h>
#define overload(a,b,c,d,...) d
#define rep1(n) for(ll _=0;_<n;++_)
#define rep2(i,n) for(ll i=0;i<n;++i)
#define rep3(i,a,b) for(ll i=(ll)a;i<(ll)b;++i)
#define rep(...) overload(__VA_ARGS__,rep3,rep2,rep1)(__VA_ARGS__)
#define rrep1(i,n) for(ll i=(ll)n-1;i>=0;--i)
#define rrep2(i,a,b) for(ll i=(ll)b-1;i>=(ll)a;--i)
#define rrep(...) overload(__VA_ARGS__,rrep2,rrep1)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
using namespace std;
using ll=int;
using ld=long double;
using vl=vector<ll>;
template<typename T,typename U>inline bool chmin(T&a,const U&b){return (a>b?a=b,true:false);}
template<typename T,typename U>inline bool chmax(T&a,const U&b){return (a<b?a=b,true:false);}
static constexpr ll inf=numeric_limits<ll>::max()/2;
int main(){
	ll h,w,r,c;cin>>h>>w>>r>>c;
	ll n;cin>>n;
	vector<vl>sum(h+1,vl(w+1,0));
	rep(i,n){
		ll r1,c1;cin>>r1>>c1;
		sum[r1][c1]++;
		//cerr<<r1<<" "<<c1<<endl;
	}
	rep(i,h)rep(j,w+1)sum[i+1][j]+=sum[i][j];
	rep(i,h+1)rep(j,w)sum[i][j+1]+=sum[i][j];
	vector<vl>sum1(h+1,vl(w+1,0));
	auto sum2=sum1,sum3=sum1,sum4=sum1;
	auto get=[&](ll x,ll y)->ll {
		return sum[x+1][y+1]-sum[x+1][y]-sum[x][y+1]+sum[x][y];
	};
	auto get2=[&](ll x1,ll y1,ll x2,ll y2)->ll {
		return sum[x2+1][y2+1]-sum[x2+1][y1]-sum[x1][y2+1]+sum[x1][y1];
	};
	auto get3=[&](ll x1,ll y1,ll x2,ll y2)->ll {
		return sum1[x2+1][y2+1]-sum1[x2+1][y1]-sum1[x1][y2+1]+sum1[x1][y1];
	};
	auto get4=[&](ll x1,ll y1,ll x2,ll y2)->ll {
		return sum2[x2+1][y2+1]-sum2[x2+1][y1]-sum2[x1][y2+1]+sum2[x1][y1];
	};
	auto get5=[&](ll x1,ll y1,ll x2,ll y2)->ll {
		return sum3[x2+1][y2+1]-sum3[x2+1][y1]-sum3[x1][y2+1]+sum3[x1][y1];
	};
	auto get6=[&](ll x1,ll y1,ll x2,ll y2)->ll {
		return sum4[x2+1][y2+1]-sum4[x2+1][y1]-sum4[x1][y2+1]+sum4[x1][y1];
	};

	rep(i,h)rep(j,w){
		if(i==0||get(i-1,j))sum1[i+1][j+1]=0;
		else sum1[i+1][j+1]=sum1[i][j+1]+1;
		if(j==0||get(i,j-1))sum2[i+1][j+1]=0;
		else sum2[i+1][j+1]=sum2[i+1][j]+1;
	}
	rrep(i,h)rrep(j,w){
		if(i+1==h||get(i+1,j))sum3[i+1][j+1]=0;
		else sum3[i+1][j+1]=sum3[i+2][j+1]+1;
		if(j+1==w||get(i,j+1))sum4[i+1][j+1]=0;
		else sum4[i+1][j+1]=sum4[i+1][j+2]+1;
	}
	rep(i,h)rep(j,w+1)sum1[i+1][j]+=sum1[i][j];
	rep(i,h+1)rep(j,w)sum1[i][j+1]+=sum1[i][j];
	rep(i,h)rep(j,w+1)sum2[i+1][j]+=sum2[i][j];
	rep(i,h+1)rep(j,w)sum2[i][j+1]+=sum2[i][j];
	rep(i,h)rep(j,w+1)sum3[i+1][j]+=sum3[i][j];
	rep(i,h+1)rep(j,w)sum3[i][j+1]+=sum3[i][j];
	rep(i,h)rep(j,w+1)sum4[i+1][j]+=sum4[i][j];
	rep(i,h+1)rep(j,w)sum4[i][j+1]+=sum4[i][j];
	/*rep(i,h)rep(j,w){
		cerr<<get5(i,j,i,j)<<endl;
	}*/
	ll ans=0;
	rep(i,h)rep(j,w){
		if(i+r>h||j+c>w)continue;
		if(get2(i,j,i+r-1,j+c-1))continue;
		//cerr<<i<<" "<<j<<endl;
		ll tmp=get3(i,j,i,j+c-1)+get4(i,j,i+r-1,j)+get5(i+r-1,j,i+r-1,j+c-1)+get6(i,j+c-1,i+r-1,j+c-1);
		chmax(ans,tmp);
	}
	cout<<ans<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 23088
final_rank: 5

code_snippet: #include<bits/stdc++.h>
using namespace std;
int rui[1002][1002];
int le[1002][1002], ri[1002][1002], up[1002][1002], dn[1002][1002];
int main(){
    int h, w, r, c, n, rr, cc;
    cin >> h >> w >> r >> c >> n;
    for(int i=0;i<=h;i++){
        for(int j=0;j<=w;j++){
            if(i==0 || j==0) rui[i][j] = 0;
            else rui[i][j] = 1;
            le[i][j] = 0;
            ri[i][j] = 0;
            up[i][j] = 0;
            dn[i][j] = 0;
        }
    }
    for(int i=0;i<n;i++){
        cin >> rr >> cc;
        rui[rr][cc] = 0;
    }
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            if(rui[i][j]){
                le[i][j] = le[i][j-1]+1;
                up[i][j] = up[i-1][j]+1;
            }
        }
    }
    for(int i=h;i>0;i--){
        for(int j=w;j>0;j--){
            if(rui[i][j]){
                ri[i][j] = ri[i][j+1]+1;
                dn[i][j] = dn[i+1][j]+1;
            }
        }
    }
    for(int i=1;i<=h;i++){
        for(int j=1;j<=w;j++){
            le[i][j] += le[i-1][j];
            ri[i][j] += ri[i-1][j];
            up[i][j] += up[i][j-1];
            dn[i][j] += dn[i][j-1];
            rui[i][j] += rui[i-1][j]+rui[i][j-1]-rui[i-1][j-1];
        }
    }
    int sum, ans=0;
    for(int i=1;i<=h-r+1;i++){
        for(int j=1;j<=w-c+1;j++){
            if(rui[i+r-1][j+c-1]-rui[i-1][j+c-1]-rui[i+r-1][j-1]+rui[i-1][j-1] < r*c) continue;
            sum = le[i+r-1][j-1]-le[i-1][j-1];
            sum += ri[i+r-1][j+c]-ri[i-1][j+c];
            sum += up[i-1][j+c-1]-up[i-1][j-1];
            sum += dn[i+r][j+c-1]-dn[i+r][j-1];
            ans = max(ans, sum);
        }
    }
    cout << ans << endl;
    return(0);
}
accuracy: 1
time_ms: 10
memory_kb: 22692
final_rank: 3

code_snippet: #include<bits/stdc++.h>
//#include<atcoder/all>
using namespace std;
//using namespace atcoder;
//using mint = modint1000000007;
//using mint = modint998244353;
using ll = long long;
#define rep(i, n) for (ll i = 0; i < ll(n); i++)
#define all(v) v.begin(), v.end()

ll ret(ll a, ll b, ll c, ll d, vector<vector<ll>> &v) {
    if (a == 0 && b == 0) {
        return v[c][d];
    } else if (a == 0) {
        return v[c][d] - v[c][b - 1];
    } else if (b == 0) {
        return v[c][d] - v[a - 1][d];
    } else {
        return v[c][d] - v[c][b - 1] - v[a - 1][d] + v[a - 1][b - 1];
    }
}

ll ret2(ll a, ll b, vector<ll> &v) {
    if (a == 0) {
        return v[b];
    } else {
        return v[b] - v[a - 1];
    }
}

int main() {
    //1. 置ける場所を全列挙
    //2. それぞれのマスに対してスコア算出
    //3. それぞれのおき方に対して点数算出

    //1 (1) 配列作成
    ll h, w, r, c, n;
    cin >> h >> w >> r >> c >> n;
    vector<pair<ll, ll>> ob(n);
    rep(i, n) {
        cin >> ob[i].first >> ob[i].second;
        ob[i].first--;ob[i].second--;
    }
    vector<vector<ll>> obs(h, vector<ll> (w));
    rep(i, n) {
        obs[ob[i].first][ob[i].second]++;
    }
    rep(i, h) {
        rep(j, w) {
            if (j) {
                obs[i][j] += obs[i][j - 1];
            }
        }
    }
    rep(i, w) {
        rep(j, h) {
            if (j) {
                obs[j][i] += obs[j - 1][i];
            }
        }
    }
    //1 (2) 各点について判定
    vector<pair<ll, ll>> can;
    rep(i, h - r + 1) {
        rep(j, w - c + 1) {
            if (!ret(i, j, i + r - 1, j + c - 1, obs)) {
                can.push_back({i, j});
            }
        }
    }
    //2 (1) 縦に見る
    vector<vector<ll>> sc1(h, vector<ll> (w));
    vector<vector<ll>> ob1(h, vector<ll> (1, -1));
    rep(i, n) {
        ob1[ob[i].first].push_back(ob[i].second);
    }
    rep(i, h) {
        ob1[i].push_back(w);
    }
    rep(i, h) {
        sort(all(ob1[i]));
    }
    rep(i, h) {
        //i行目
        ll ind = 0;
        rep(j, w) {
            if (j == ob1[i][ind + 1]) {
                ind++;
            } else {
                sc1[i][j] = ob1[i][ind + 1] - ob1[i][ind] - 1;
            }
        }
    }
    rep(i, h) {
        rep(j, w) {
            sc1[i][j]--;
        }
    }
    //2 (2) 横に見る
    vector<vector<ll>> sc2(h, vector<ll> (w));
    vector<vector<ll>> ob2(w, vector<ll> (1, -1));
    rep(i, n) {
        ob2[ob[i].second].push_back(ob[i].first);
    }
    rep(i, w) {
        ob2[i].push_back(h);
    }
    rep(i, w) {
        sort(all(ob2[i]));
    }
    rep(j, w) {
        //j列目
        ll ind = 0;
        rep(i, h) {
            if (i == ob2[j][ind + 1]) {
                ind++;
            } else {
                sc2[i][j] = ob2[j][ind + 1] - ob2[j][ind] - 1;
            }
        }
    }
    rep(i, h) {
        rep(j, w) {
            sc2[i][j]--;
        }
    }
    //3 (1) 縦配列作成
    vector<vector<ll>> sm1(h + 1, vector<ll> (w));
    rep(i, h) {
        rep(j, w) {
            sm1[i + 1][j] = sm1[i][j] + sc1[i][j];
        }
    }
    //3 (2) 横配列作成
    vector<vector<ll>> sm2(h, vector<ll> (w + 1));
    rep(i, h) {
        rep(j, w) {
            sm2[i][j + 1] = sm2[i][j] + sc2[i][j];
        }
    }
    ll mx = -1;
    for (auto p : can) {
        ll ans = 0;
        ans += -(sm1[p.first][p.second] - sm1[p.first + r][p.second]) - r * (c - 1);
        ans += -(sm2[p.first][p.second] - sm2[p.first][p.second + c]) - c * (r - 1);
        mx = max(mx, ans);
    }
    cout << mx << endl;
}
accuracy: 1
time_ms: 20
memory_kb: 58032
final_rank: 12