Submission #923995


Source Code Expand

import java.util.ArrayList;
import java.util.Arrays;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.function.BiFunction;

public class Main {
  Scanner sc = new Scanner(System.in);

  public static void main(String[] args) {
    new Main().run();
  }

  int[][] ofs = {
      {-1, 0},
      {0, -1},
      {0, 1},
      {1, 0}
  };

  class Node {
    int x, y, value;

    Node(int x, int y, int value) {
      this.x = x;
      this.y = y;
      this.value = value;
    }

    Node(Node n) {
      this(n.x, n.y, n.value);
    }
  }

  int h, w;
  int[][] field;
  long MOD = 1_000_000_007;

  long[][] dp;

  void run() {
    h = ni();
    w = ni();
    field = new int[h + 2][w + 2];
    dp = new long[h + 2][w + 2];
    PriorityQueue<Node> queue = new PriorityQueue<>((a, b) -> b.value - a.value);
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        field[i][j] = ni();
        dp[i][j] = 1;
      }
    }
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        Node node = new Node(j, i, field[i][j]);
        queue.add(node);
      }
    }

    while (queue.size() > 0) {
      Node atom = queue.poll();
      for (int i = 0; i < 4; ++i) {
        int nx = atom.x + ofs[i][0];
        int ny = atom.y + ofs[i][1];
        if (field[ny][nx] == 0) {
          continue;
        }
        if (field[atom.y][atom.x] <= field[ny][nx]) {
          continue;
        }
        dp[ny][nx] += dp[atom.y][atom.x];
        dp[ny][nx] %= MOD;
      }
    }

    long sum = 0;
    for (int i = 1; i <= h; ++i) {
      for (int j = 1; j <= w; ++j) {
        sum += dp[i][j];
        sum %= MOD;
      }
    }
    System.out.println(sum);
  }

  int ni() {
    return Integer.parseInt(sc.next());
  }

  void debug(Object... os) {
    System.err.println(Arrays.deepToString(os));
  }

  class BIT<T> {
    int n;
    ArrayList<T> bit;
    BiFunction<T, T, T> bif;

    BIT(int n, BiFunction<T, T, T> bif, T defaultValue) {
      this.n = n;
      bit = new ArrayList<>(n + 1);
      for (int i = 0; i < n + 1; ++i) {
        bit.add(defaultValue);
      }
      this.bif = bif;
    }

    void update(int i, T v) {
      for (int x = i; x <= n; x += x & -x) {
        bit.set(x, bif.apply(bit.get(x), v));
      }
    }

    T reduce(int i, T defaultValue) {
      T ret = defaultValue;
      for (int x = i; x > 0; x -= x & -x) {
        ret = bif.apply(ret, bit.get(x));
      }
      return ret;
    }
  }

}

Submission Info

Submission Time
Task D - 経路
User arukuka
Language Java8 (OpenJDK 1.8.0)
Score 100
Code Size 2605 Byte
Status AC
Exec Time 1971 ms
Memory 196108 KB

Judge Result

Set Name sample All
Score / Max Score 0 / 0 100 / 100
Status
AC × 2
AC × 22
Set Name Test Cases
sample sample01.txt, sample02.txt
All 00.txt, 01.txt, 02.txt, 03.txt, 04.txt, 05.txt, 06.txt, 07.txt, 08.txt, 09.txt, 10.txt, 11.txt, 12.txt, 13.txt, 14.txt, 15.txt, 16.txt, 17.txt, 18.txt, 19.txt, sample01.txt, sample02.txt
Case Name Status Exec Time Memory
00.txt AC 1799 ms 196108 KB
01.txt AC 1628 ms 193604 KB
02.txt AC 1197 ms 149204 KB
03.txt AC 208 ms 14800 KB
04.txt AC 208 ms 14532 KB
05.txt AC 224 ms 14540 KB
06.txt AC 237 ms 15052 KB
07.txt AC 245 ms 15176 KB
08.txt AC 242 ms 14796 KB
09.txt AC 214 ms 15056 KB
10.txt AC 284 ms 17200 KB
11.txt AC 1971 ms 165680 KB
12.txt AC 1958 ms 164760 KB
13.txt AC 1877 ms 164696 KB
14.txt AC 1939 ms 163712 KB
15.txt AC 1679 ms 171232 KB
16.txt AC 1941 ms 164648 KB
17.txt AC 1967 ms 165196 KB
18.txt AC 1193 ms 148812 KB
19.txt AC 1002 ms 150296 KB
sample01.txt AC 211 ms 15052 KB
sample02.txt AC 211 ms 15048 KB