611C.md (2864B)
1 Source: https://codeforces.com/problemset/problem/611/C 2 3 C. New Year and Domino 4 5 time limit per test 6 7 3 seconds 8 9 memory limit per test 10 11 256 megabytes 12 13 input 14 15 standard input 16 17 output 18 19 standard output 20 21 They say "years are like dominoes, tumbling one after the other". But would a year fit into a grid? I don't think so. 22 23 Limak is a little polar bear who loves to play. He has recently got a rectangular grid with *h* rows and *w* columns. Each cell is a square, either empty (denoted by '.') or forbidden (denoted by '#'). Rows are numbered 1 through *h* from top to bottom. Columns are numbered 1 through *w* from left to right. 24 25 Also, Limak has a single domino. He wants to put it somewhere in a grid. A domino will occupy exactly two adjacent cells, located either in one row or in one column. Both adjacent cells must be empty and must be inside a grid. 26 27 Limak needs more fun and thus he is going to consider some queries. In each query he chooses some rectangle and wonders, how many way are there to put a single domino inside of the chosen rectangle? 28 29 Input 30 31 The first line of the input contains two integers *h* and *w* (1 ≤ *h*, *w* ≤ 500) – the number of rows and the number of columns, respectively. 32 33 The next *h* lines describe a grid. Each line contains a string of the length *w*. Each character is either '.' or '#' — denoting an empty or forbidden cell, respectively. 34 35 The next line contains a single integer *q* (1 ≤ *q* ≤ 100 000) — the number of queries. 36 37 Each of the next *q* lines contains four integers *r*1*i*, *c*1*i*, *r*2*i*, *c*2*i* (1 ≤ *r*1*i* ≤ *r*2*i* ≤ *h*, 1 ≤ *c*1*i* ≤ *c*2*i* ≤ *w*) — the *i*-th query. Numbers *r*1*i* and *c*1*i* denote the row and the column (respectively) of the upper left cell of the rectangle. Numbers *r*2*i* and *c*2*i* denote the row and the column (respectively) of the bottom right cell of the rectangle. 38 39 Output 40 41 Print *q* integers, *i*-th should be equal to the number of ways to put a single domino inside the *i*-th rectangle. 42 43 Examples 44 45 Input 46 47 ``` 48 5 8 49 ....#..# 50 .#...... 51 ##.#.... 52 ##..#.## 53 ........ 54 4 55 1 1 2 3 56 4 1 4 1 57 1 2 4 5 58 2 5 5 8 59 ``` 60 61 Output 62 63 ``` 64 4 65 0 66 10 67 15 68 ``` 69 70 Input 71 72 ``` 73 7 39 74 ....................................... 75 .###..###..#..###.....###..###..#..###. 76 ...#..#.#..#..#.........#..#.#..#..#... 77 .###..#.#..#..###.....###..#.#..#..###. 78 .#....#.#..#....#.....#....#.#..#..#.#. 79 .###..###..#..###.....###..###..#..###. 80 ....................................... 81 6 82 1 1 3 20 83 2 10 6 30 84 2 10 7 30 85 2 2 7 7 86 1 7 7 7 87 1 8 7 8 88 ``` 89 90 Output 91 92 ``` 93 53 94 89 95 120 96 23 97 0 98 2 99 ``` 100 101 Note 102 103 A red frame below corresponds to the first query of the first sample. A domino can be placed in 4 possible ways. 104 105 