[NOIP Simulation Test]: any (Duty) (and prefix)

Title Description

$ liu after _runda $ retired lost dream \ begin salted fish life ......
$ bilibili $ Sketchpad summer activities, everyone can create pixel art on a drawing board. $ UOJ $ boring group of a group of people who decided to create a $ 50 \ times 50 $ of $ UOJ $ of $ LOGO $ on the drawing board. As shown below:


This is actually the drawing board is a big rectangular grid. A grid is a pixel.
One person every three minutes to draw a pixel. So $ liu \ _runda $ salted fish life is very boring.
Guo God said he really could not stand $ liu \ _runda $ only decadent dog, so readily God out of a problem, $ liu \ _runda $ will not do, then give in to the exam.
A black and white rectangular regions of satisfying such properties on the drawing board: that if the same color blocks can be connected in four directions up and down, then the two black squares or not any communication, but the communication between either only one simple path ( Unique path through a grid of the same).
This rectangular region M $ N $ $ $ column line, top to bottom order of $ 1,2,3 ... N-1, N $ row, from left to right with # $ 2, 3 .. .M-1, M $ column.
Guo God asks each child a rectangle within this rectangle. When considering only the pixels within the sub-rectangle (i.e., and not beyond the sub-pixels from the interior of the rectangular sub-rectangle communicated), ask the black squares in the number of sub-rectangles communication block.
If you can not complete this task, $ liu \ _runda $ Guo God will be some criticism ......


Input Format

The first row of three integers $ N $, $ M $, $ Q $, a rectangular region represents N $ $ $ M $ column line, there is $ Q $ query group.
Next, $ N $ lines, each of a length M $ $ $ 01 of $ string. $ 0 $ express white, $ 1 $ represents black. The first row $ i $ $ j $ character represents the color of the first row $ i $ $ j $ column.
Next $ Q $ rows each integers $ 4 $ $ x_1, y_1, x_2, y_2 , (x1 <= x2, y1 <= y2) $ rectangular region represented by the selected two diagonal. I.e., the upper left corner is selected as a first line of $ x_1 $ $ $ Y_1 column, the lower right corner of x_2 $ $ $ Y_2 $ column line, comprising $ x_2-x_1 + 1 $ line, $ y_2-y_1 + 1 $ column Area.


 

Output Format

$ Q $ line, the first line of an integer $ i $ $ ans $ $ i $ denotes the group answer to the inquiry.


 

Sample

Sample input 1:

3 4 4
1101
0110
1101
1 1 3 4
1 1 3 1
2 2 3 4
1 2 2 4

Sample output 1:

3
2
2
2

Sample input 2:

5 5 6
11010
01110
10101
11101
01010
1 1 5 5
1 2 4 5
2 3 3 4
3 3 3 3
3 1 3 5
1 1 3 4

Sample Output 2:

3
2
1
1
3
2


 

Data range and tips

对于第$1,2$个测试点,$Q=1$。
对于第$3,4$个测试点,$N=1$。
对于第$5,6,7$个测试点,$N=2$。
对于第$8$个测试点,$N,M\leqslant 1,000$。
对于第$9$个测试点,$N,M\leqslant 1500$。
对于全部测试点,$1\leqslant N,M\leqslant 2,000,1\leqslant Q\leqslant 200,000,1\leqslant x_1\leqslant x_2\leqslant N,1\leqslant y_1\leqslant y_2\leqslant M$,保证任意两个黑色像素之间最多只有一条简单路径。


 

题解

题目应该不是很难懂,手膜一下样例就好了。

$70\%$算法:

暴力$DFS$看有几个联通块就好了。

时间复杂度:$\Theta(Q\times N\times M)$。

期望得分:$70$分。

实际得分:$70$分。

$100\%$算法:

看到$Q$这么大,前面的同学居然在想二维莫队,但是本蒟蒻并不会,所以我就想了前缀和。

显然前缀和不好搞,先来证明一个性质吧,联通块的个数=点数-边数

Guess you like

Origin www.cnblogs.com/wzc521/p/11354897.html