@codeforces - 1186E@ Vus the Cossack and a Field


@description@

Given an n * m matrix 01, to generate an infinite matrix obtained by this matrix, as follows:

(1) This matrix will be written in the upper left corner.
(2) the inverse of this matrix each written in the top right corner.
(3) the inverse of this matrix each written in the bottom left corner.
(4) This matrix will be written in the lower right corner.
(5) The matrix is then obtained as the initial matrix, these operations are repeated.

For example, for original matrix:
\ [\} the begin Matrix {0. 1 & & & \\. 1. 1 & \\ \ Matrix End {} \]

它操作一次变为:
\[\begin{matrix} 1 & 0 & 0 & 1 \\ 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 0 \\ 0 & 0 & 1 & 1 \\ \end{matrix}\]

再操作一次变为:
\[\begin{matrix} 1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 \\ 1 & 1 & 0 & 0 & 0 & 0 & 1 & 1 \\ 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 \\ 0 & 0 & 1 & 1 & 1 & 1 & 0 & 0 \\ 0 & 1 & 1 & 0 & 1 & 0 & 0 & 1 \\ 0 & 0 & 1 & 1 & 1 & 1 & 0 & 0 \\ 1 & 0 & 0 & 1 & 0 & 1 & 1 & 0 \\ 1 & 1 & 0& 0 & 0 & 0 & 1 & 1 \\ \end{matrix}\]

Then continue ......

We will be referred to as the upper left corner (1, 1).
Q times now ask, each time asking to (x1, y1) to the upper left corner, (x2, y2) for the lower right corner of the matrix and within all elements.

input
of the first line contains three integers n, m, q (1 < = n, m <= 1000, 1 <= q <= 10 ^ 5).
The next n lines each containing m characters cij, described initial matrix.
The next q lines contains four integers x1, y1, x2, y2, a query is described.

output
for each inquiry, output the answer.

sample input 1
2 2 5
10
11
1 1 8 8
2 4 5 6
1 2 7 8
3 3 6 8
5 6 7 8
sample output 1
32
5
25
14
4

sample input 2
2 3 7
100
101
4 12 5 17
5 4 9 4
1 4 13 18
12 1 14 9
3 10 7 18
3 15 12 17
8 6 8 12
sample output 2
6
3
98
13
22
15
3

@solution@

Use recursion recursive beat!

Consider a query will be split into a prefix and a differential form, then converted to equivalent interrogation (1, 1) to (x, y) and the prefix.
If the direct recursive divide and conquer to solve the apparently direct the bombing, so we have to find some properties.

We might remember the original matrix M0, operation once the matrix M1, the operation twice matrix M2, ......, and so on.
M1 was observed in the sample given subject, M2, difficult to find the number of each row are equal to 1, and is half the total number of the line elements; Similarly columns.
We solved quickly and all elements Mi, Mi and solved before a number of rows / columns and all elements (where i = 0 to special treatment, because without this nature)

Mi is provided comprising (x, y), consider the (x, y) position:
(1) In the upper left portion Mi, recursive directly to M (i-1).
(2) in the upper right portion Mi, this time asking contains two parts: There are a number in the upper left part of the entire line, in the upper right part is one of the smaller sub-problems. Upper left directly with the formula calculation, upper right recursion.
(3) in the lower left portion Mi, it is substantially the same (2) is performed.
(4) in the lower right portion Mi, considered as the upper left, lower left, upper right, lower right are what shape, found that only the lower right is the original title with a similar, smaller sub-problems. Then probably deal with (2) a.
Like when i = 0 to special treatment.

Found that each time only one down recursive, recursive least once in half scale, so ask for what is O (log) time complexity.

Specific codes may be more convincing.

@accepted code@

#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN = 1000;
ll s[MAXN + 5][MAXN + 5];
int n[MAXN + 5], m[MAXN + 5];
char c[MAXN + 5];
ll fun1(int dep, int x, int y) {
    if( dep == -1 ) return s[x][y];
    if( x <= n[dep] && y <= m[dep] )
        return fun1(dep - 1, x, y);
    if( x <= n[dep] && y > m[dep] ) {
        if( dep ) return (1LL*x*(y-m[dep]) - fun1(dep - 1, x, y-m[dep])) + 1LL*x*m[dep-1];
        else return (1LL*x*(y-m[dep]) - fun1(dep - 1, x, y-m[dep]) + fun1(dep - 1, x, m[dep]));
    }
    if( x > n[dep] && y <= m[dep] ) {
        if( dep ) return (1LL*(x-n[dep])*y - fun1(dep - 1, x-n[dep], y)) + 1LL*n[dep-1]*y;
        else return (1LL*(x-n[dep])*y - fun1(dep - 1, x-n[dep], y) + fun1(dep - 1, n[dep], y));
    }
    if( x > n[dep] && y > m[dep] ) {
        //puts("?");
        if( dep ) {
            //printf("? %lld %lld %lld\n", fun2(dep - 1, x-n[dep]), fun3(dep - 1, y-m[dep]), 1LL*n[dep]*m[dep]/2);
            return fun1(dep - 1, x-n[dep], y-m[dep]) + 1LL*(x-n[dep])*m[dep-1] + 1LL*n[dep-1]*(y-m[dep]) + 1LL*n[dep]*m[dep]/2;
        }
        else {
            //printf("| %lld %lld %lld %lld\n", 1LL*x*y, (1LL*(x-n[dep])*(y-m[dep]) - fun1(dep - 1, x-n[dep], y-m[dep])), fun1(dep - 1, x-n[dep], m[dep]) + fun1(dep - 1, n[dep], y-m[dep]), s[n[0]][m[0]]);
            return 1LL*(x-n[dep])*m[dep] + 1LL*n[dep]*(y-m[dep]) + fun1(dep - 1, x-n[dep], y-m[dep]) + s[n[0]][m[0]] - fun1(dep - 1, x-n[dep], m[dep]) - fun1(dep - 1, n[dep], y-m[dep]);
        }
    }
}
ll sum(int x, int y) {
    if( x == 0 || y == 0 ) return 0;
    int dep; for(dep = 0; x - n[dep] > n[dep] || y - m[dep] > m[dep]; dep++);
    return fun1(dep, x, y);
}
int main() {
    int q;
    scanf("%d%d%d", &n[0], &m[0], &q);
    for(int i=1;i<=n[0];i++) {
        scanf("%s", c + 1);
        for(int j=1;j<=m[0];j++)
            s[i][j] = s[i-1][j] + s[i][j-1] - s[i-1][j-1] + (c[j] - '0');
    }
    for(int i=1;i<=MAXN;i++)
        n[i] = 2*n[i-1], m[i] = 2*m[i-1];
    for(int i=1;i<=q;i++) {
        int x1, y1, x2, y2;
        scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
        printf("%lld\n", sum(x2, y2) - sum(x1-1, y2) - sum(x2, y1-1) + sum(x1-1, y1-1));
    }
}

@details@

A long time did not play CF ...... Events hands and feet.
But the question is B pit. . . I made a fake title for nearly one hour.
Probably thinking I might have to drop ashes, then suddenly released CF notice that marked B title count is wrong, then the whole questions will suddenly disappear. Fortunately, last unrated, or else dead loss www.

This question is not difficult, but very CF style.

Guess you like

Origin www.cnblogs.com/Tiw-Air-OAO/p/11105345.html