Prefix and a two-dimensional template title

Enter an integer matrix of n rows and m columns, then enter a query q, each challenge comprising four integers x1, y1, x2, y2, upper-left coordinates and lower right coordinates represents a sub-matrices.

For each sub-query output matrix and all the numbers.

Input Format

The first line contains three integers n, m, q.

Next n lines, each line contains integers m represents an integer matrix.

Next q rows, each row comprising four integers x1, y1, x2, y2, represents a group of query.

Output Format

A total of q rows, each row outputs a result of the inquiry.

data range

. 1 n- , m 1000
. 1 Q 200000 ,
. 1 X . 1 X 2 n- ,
. 1 Y . 1 Y 2 m
- 1000 rectangular array within the element pixel the value of 1000

Sample input:

3 4 3
1 7 2 4
3 6 2 8
2 1 2 3
1 1 2 2
2 1 3 4
1 3 3 4

Sample output:

17
27
21

AC代码
#pragma GCC optimize(2)
#include<bits/stdc++.h>
using namespace std;
inline int read() {int x=0,f=1;char c=getchar();while(c!='-'&&(c<'0'||c>'9'))c=getchar();if(c=='-')f=-1,c=getchar();while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar();return f*x;}
typedef long long ll;
const int maxn = 1e3+10;
int a[maxn][maxn];
int s[maxn][maxn];
int n,m,q;
int main()
{
    cin>>n>>m>>q;
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cin>>a[i][j];
            s[i][j]=a[i][j]+s[i-1][j]+s[i][j-1]-s[i-1][j-1];
        }
    }
    int x1,x2,y1,y2;
    int sum=0;
    for(int i=0;i<q;i++){
        cin>>x1>>y1>>x2>>y2;
        sum=s[x2][y2]-s[x1-1][y2]-s[x2][y1-1]+s[x1-1][y1-1];
        printf("%d\n",sum);
    }
}

 

 

Guess you like

Origin www.cnblogs.com/lipu123/p/12233484.html