统计子矩阵

 

70分代码:

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;

public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)500 + 10;
    static long a[][] = new long[N][N];
    static long s[][] = new long[N][N];

    public static void main(String[] args ) throws IOException
    {
        long n = rd.nextLong();
        long m = rd.nextLong();
        long k = rd.nextLong();

        for(long i = 1 ; i <= n ; i ++)
        {
            for(long j = 1 ; j <= m ; j ++)
            {
                a[Math.toIntExact(i)][Math.toIntExact(j)] = rd.nextLong();
            }
        }

        for(long i = 1 ; i <= n ; i ++)
        {
            for(long j = 1 ; j <= m ; j ++)
            {
                s[Math.toIntExact(i)][Math.toIntExact(j)] = s[Math.toIntExact(i - 1)][Math.toIntExact(j)] + s[Math.toIntExact(i)][Math.toIntExact(j - 1)] - s[Math.toIntExact(i - 1)][Math.toIntExact(j - 1)] + a[Math.toIntExact(i)][Math.toIntExact(j)];
            }
        }

        long res = 0;
        for(long x1 = 1 ; x1 <= n ;  ++ x1)
        {
            for(long y1 = 1 ; y1 <= m ; ++ y1)
            {
                for(long x2 = x1  ; x2 <= n ;  ++ x2)
                {
                    for(long y2 = y1 ; y2 <= m ; ++ y2)
                    {
                        if(s[Math.toIntExact(x2)][Math.toIntExact(y2)] - s[Math.toIntExact(x2)][Math.toIntExact(y1 - 1)] - s[Math.toIntExact(x1 - 1)][Math.toIntExact(y2)] + s[Math.toIntExact(x1 - 1)][Math.toIntExact(y1 - 1)] <= k)  res ++;
                    }
                }
            }
        }
        pw.println(res);
        pw.flush();
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException  { return reader.readLine(); }

    static String next() throws IOException
    {
        while (!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }

    static int nextInt() throws IOException  { return Integer.parseInt(next()); }

    static double nextDouble() throws IOException { return Double.parseDouble(next()); }

    static long nextLong() throws IOException  { return Long.parseLong(next());}

    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class PII
{
    int x,y;
    public PII(int x ,int y)
    {
        this.x = x;
        this.y = y;
    }
}

猜你喜欢

转载自blog.csdn.net/RGHLY21/article/details/129930002