Submission #923985


Source Code Expand

import java.io.IOException;
import java.io.InputStream;
import java.util.*;
import java.util.function.BiFunction;

public class Main {
  FastScanner sc = new FastScanner(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;
  boolean[][] done;

  void run() {
    h = ni();
    w = ni();
    field = new int[h + 2][w + 2];
    dp = new long[h + 2][w + 2];
    done = new boolean[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;
        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;
      }

      done[atom.y][atom.x] = true;
    }

    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;
    }
  }

  class FastScanner {
    private final InputStream in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    FastScanner(InputStream in) {
      this.in = in;
    }

    private boolean hasNextByte() {
      if (ptr < buflen) {
        return true;
      } else {
        ptr = 0;
        try {
          buflen = in.read(buffer);
        } catch (IOException e) {
          e.printStackTrace();
        }
        if (buflen <= 0) {
          return false;
        }
      }
      return true;
    }

    private int readByte() {
      if (hasNextByte()) return buffer[ptr++];
      else return -1;
    }

    private boolean isPrintableChar(int c) {
      return 33 <= c && c <= 126;
    }

    private void skipUnprintable() {
      while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
    }

    public boolean hasNext() {
      skipUnprintable();
      return hasNextByte();
    }

    public String next() {
      if (!hasNext()) throw new NoSuchElementException();
      StringBuilder sb = new StringBuilder();
      int b = readByte();
      while (isPrintableChar(b)) {
        sb.appendCodePoint(b);
        b = readByte();
      }
      return sb.toString();
    }

    public long nextLong() {
      if (!hasNext()) throw new NoSuchElementException();
      long n = 0;
      boolean minus = false;
      int b = readByte();
      if (b == '-') {
        minus = true;
        b = readByte();
      }
      if (b < '0' || '9' < b) {
        throw new NumberFormatException();
      }
      while (true) {
        if ('0' <= b && b <= '9') {
          n *= 10;
          n += b - '0';
        } else if (b == -1 || !isPrintableChar(b)) {
          return minus ? -n : n;
        } else {
          throw new NumberFormatException();
        }
        b = readByte();
      }
    }

    int ni() {
      return (int) nextLong();
    }
  }
}

Submission Info

Submission Time
Task D - 経路
User arukuka
Language Java8 (OpenJDK 1.8.0)
Score 100
Code Size 4580 Byte
Status AC
Exec Time 1974 ms
Memory 131444 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 1204 ms 131444 KB
01.txt AC 1096 ms 127960 KB
02.txt AC 1027 ms 130936 KB
03.txt AC 191 ms 13380 KB
04.txt AC 186 ms 13256 KB
05.txt AC 198 ms 14028 KB
06.txt AC 209 ms 13640 KB
07.txt AC 203 ms 14024 KB
08.txt AC 194 ms 13516 KB
09.txt AC 195 ms 13392 KB
10.txt AC 218 ms 14536 KB
11.txt AC 1836 ms 127192 KB
12.txt AC 1822 ms 126988 KB
13.txt AC 1793 ms 126236 KB
14.txt AC 1787 ms 127184 KB
15.txt AC 1420 ms 127808 KB
16.txt AC 1771 ms 127484 KB
17.txt AC 1974 ms 126948 KB
18.txt AC 1402 ms 131392 KB
19.txt AC 742 ms 130788 KB
sample01.txt AC 183 ms 13900 KB
sample02.txt AC 183 ms 13260 KB