fork download
/* paiza POH! vol.2
 * result:
 * http://p...content-available-to-author-only...a.jp/poh/paizen/result/5111b574f1753713f0393d7e49ba0387
 * author: Leonardone @ NEETSDKASU
 */
import java.util.*;
import java.lang.*;
import java.io.*;

class Main
{
	public static void main (String[] args) throws java.lang.Exception
	{
		Paiza.getInstance().resolve(new MyResolver());
	}
}

class MyResolver extends Paiza.Resolver
{
	private int[][] space2right = null;
	private int[][] space2bottom = null;
	private int[][] result = null;
	private int[] maxSpace2right = null;
	private int[] maxSpace2bottom = null;
	
	@Override
	public void setHome(Paiza.Home home) {
		super.setHome(home);
		
		space2right = new int[home.getH()][home.getW()];
		maxSpace2right = new int[home.getH()];
		int allSpaceCount = 0;
		
		for (int y = 0; y < home.getH(); y++) {
			int count = 0, max = 0;
			for (int x = home.getW() - 1; x >= 0; x--) {
				if (home.isSpace(x, y)) {
					count++;
					allSpaceCount++;
					if (count > max) {
						max = count;
					}
				} else {
					count = 0;
				}
				space2right[y][x] = count;
			}
			maxSpace2right[y] = max;
		}
		
		space2bottom = new int[home.getH()][home.getW()];
		maxSpace2bottom = new int[home.getW()];
		
		for (int x = 0; x < home.getW(); x++) {
			int count = 0, max = 0;
			for (int y = home.getH() - 1; y >= 0; y--) {
				if (home.isSpace(x, y)) {
					count++;
					if (count > max) {
						max = count;
					}
				} else {
					count = 0;
				}
				space2bottom[y][x] = count;
			}
			maxSpace2bottom[x] = max;
		}
		
		result = new int[301][301];
		result[1][1] = allSpaceCount + 1;

	}
	
	@Override
	public int resolve(Paiza.Widget widget) {
		if (result[widget.getS()][widget.getT()] > 0) {
			return result[widget.getS()][widget.getT()] - 1;
		}
		int count;
		if (widget.getS() > widget.getT()) {
			count = resolve2bottom(widget);
		} else {
			count = resolve2right(widget);
		}
		result[widget.getS()][widget.getT()] = count + 1;
		return count;
	}
	
	private int resolve2right(Paiza.Widget widget) {
		int count = 0;
		for (int y = 0; y <= home.getH() - widget.getS(); y++) {
			for (int yy = y + widget.getS() - 1; yy > y; yy--) {
				if (maxSpace2right[yy] < widget.getT()) {
					y = yy;
					break;
				}
			}
			if (maxSpace2right[y] < widget.getT()) {
				continue;
			}
			for (int x = 0; x <= home.getW() - widget.getT(); x++) {
				if (space2bottom[y][x] >= widget.getS()) {
					boolean placeable = true;
					for (int dy = 0; placeable && dy < widget.getS(); dy++) {
						if (space2right[y + dy][x] < widget.getT()) {
							placeable = false;
							x += space2right[y + dy][x];
						}
					}
					if (placeable) {
						count++;
					}
				} else if (space2right[y][x] < widget.getT()) {
					x += space2right[y][x];
				}
			}
		}
		return count;
	}

	private int resolve2bottom(Paiza.Widget widget) {
		int count = 0;
		for (int x = 0; x <= home.getW() - widget.getT(); x++) {
			for (int xx = x + widget.getT() - 1; xx > x; xx--) {
				if (maxSpace2bottom[xx] < widget.getS()) {
					x = xx;
					break;
				}
			}
			if (maxSpace2bottom[x] < widget.getS()) {
				continue;
			}
			for (int y = 0; y <= home.getH() - widget.getS(); y++) {
				if (space2right[y][x] >= widget.getT()) {
					boolean placeable = true;
					for (int dx = 0; placeable && dx < widget.getT(); dx++) {
						if (space2bottom[y][x + dx] < widget.getS()) {
							placeable = false;
							y += space2bottom[y][x + dx];
						}
					}
					if (placeable) {
						count++;
					}
				} else if (space2bottom[y][x] < widget.getS()) {
					y += space2bottom[y][x];
				}
			}
		}
		return count;
	}

}

	
abstract class Resolver
{
	protected Paiza.Home home;
	
	public void setHome(Paiza.Home home) {
		this.home = home;
	}

	public abstract int resolve(Paiza.Widget widget);
}

final class Paiza
{
	public static abstract class Resolver
	{
		protected Paiza.Home home;
		
		public void setHome(Paiza.Home home) {
			this.home = home;
		}
	
		public abstract int resolve(Paiza.Widget widget);
	}
	
	public static Paiza getInstance() throws java.lang.Exception {
		if (paiza == null) {
			paiza = new Paiza();
		}
		return paiza;
	}	

	public void resolve(Resolver resolver) {
		init(resolver);
		for (Widget widget : widgets) {
			answer(resolver.resolve(widget));
		}
		flush();
	}

	
	public final class Home
	{
		private final int H;
		private final int W;
		private int[][] display;
		private Home(int H, int W) {
			this.H = H;
			this.W = W;
			display = new int[H][W];
		}
		
		private void setDisplay(int x, int y, int e) throws java.lang.Exception {
			if (x < 0 || x >= W) {
				throw new ArrayIndexOutOfBoundsException("x : " + x);
			}
			if (y < 0 || y >= H) {
				throw new ArrayIndexOutOfBoundsException("y : " + y);
			}
			if (e != 0 && e != 1) {
				throw new IllegalArgumentException("e");
			}
			display[y][x] = e;
		}
		
		public int getH() {
			return H;
		}
		
		public int getW() {
			return W;
		}
		
		public boolean isSpace(int x, int y) {
			if (x < 0 || y < 0 || x >= W || y >= H) {
				return false;
			}
			return display[y][x] == 0;
		}
	}
	
	public final class Widget
	{
		private final int s;
		private final int t;
		private Widget(int s, int t) {
			this.s = s;
			this.t = t;
		}
		
		public int getS() {
			return s;
		}
		
		public int getT() {
			return t;
		}
	}
	
	private static Paiza paiza = null;
	
	private Home home;
	private ArrayList<Widget> widgets;
	
	private Paiza() throws java.lang.Exception {
		BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
		String[] hw = in.readLine().split(" ");
		home = new Home(Integer.parseInt(hw[0]), Integer.parseInt(hw[1]));
		for (int y = 0; y < home.getH(); y++) {
			String line = in.readLine();
			for (int x = 0; x < home.getW(); x++) {
				home.setDisplay(x, y, (int)(line.charAt(x) - '0'));
			}
		}
		int N = Integer.parseInt(in.readLine());
		widgets = new ArrayList<Widget>(N);
		for (int i = 0; i< N; i++) {
			String[] st = in.readLine().split(" ");
			widgets.add(new Widget(Integer.parseInt(st[0]), Integer.parseInt(st[1])));
		}
	}
	
	private StringBuilder output = null;
	private static final String NEWLINE = System.getProperty("line.separator");
	
	private void init(Resolver resolver) {
		resolver.setHome(home);
		output = new StringBuilder(widgets.size() * 6);
	}
	
	private void answer(int count) {
		output.append(count);
		output.append(NEWLINE);
	}

	private void flush() {
		System.out.print(output);
	}
}
Success #stdin #stdout 0.07s 380224KB
stdin
5 5
00000
00100
00010
10001
10000
3
2 2
1 1
3 2
stdout
6
20
2