D@C
D@C
class Result {
/*
* Complete the 'quickSort' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
int n = Integer.parseInt(bufferedReader.readLine().trim());
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(Collectors.joining(" "))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
----------------------------------------
//unique divide and conquer
import java.io.*;
import java.util.*;
void solve() {
int n = in.nextInt();
int p = in.nextInt();
int[][] c = new int[n + 1][n + 1];
c[0][0] = 1;
for (int i = 1; i <= n; i++) {
c[i][0] = 1;
for (int j = 1; j <= n; j++) {
c[i][j] = c[i - 1][j - 1] + c[i - 1][j];
if (c[i][j] >= p) {
c[i][j] -= p;
}
}
}
long[] dpSum = new long[n + 1];
long[] ans = new long[n + 1];
long[] dpBad = new long[n + 1];
dpSum[0] = 1;
for (int sz = 1; sz <= n; sz++) {
for (int big = (1 + sz) / 2; big < sz; big++) {
dpBad[sz] += dpSum[sz - 1 - big] * ans[big] % p * big % p
* c[sz - 1][big] % p;
}
dpBad[sz] %= p;
ans[sz] = (dpSum[sz - 1] - dpBad[sz] + p) * sz % p;
for (int size1 = 1; size1 <= sz; size1++) {
dpSum[sz] += ans[size1] * size1 % p * c[sz - 1][size1 - 1] % p
* dpSum[sz - size1] % p;
}
dpSum[sz] %= p;
}
out.println(ans[n]);
}
void run() {
try {
in = new FastScanner(new File("object.in"));
out = new PrintWriter(new File("object.out"));
solve();
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
void runIO() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
-----------------------------------------
//Maximum Subarray Sum
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'maximumSum' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts following parameters:
* 1. LONG_INTEGER_ARRAY a
* 2. LONG_INTEGER m
*/
return maxMod;
int q = Integer.parseInt(bufferedReader.readLine().trim());
int n = Integer.parseInt(firstMultipleInput[0]);
long m = Long.parseLong(firstMultipleInput[1]);
List<Long> a = Stream.of(bufferedReader.readLine().replaceAll("\\s+
$", "").split(" "))
.map(Long::parseLong)
.collect(toList());
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
bufferedWriter.close();
}
}
------------------------------------------------
//Number of rotations
import java.io.*;
import java.util.*;
-------------------------------------------------
//inversion count 11
import java.io.*;
import java.util.*;
private static long mergeAndCount(int[] array, int left, int mid, int right) {
int n1 = mid - left + 1;
int n2 = right - mid;
int i = 0, j = 0;
int k = left;
long count = 0;
return count;
}
int n = Integer.parseInt(bufferedReader.readLine().trim());
int[] array = new int[n];
String[] arrayItems = bufferedReader.readLine().trim().split(" ");
bufferedReader.close();
bufferedWriter.close();
}
}
-------------------------------------
//sorting
import java.io.*;
import java.util.*;
bufferedReader.close();
}
return head;
}
return sb.toString();
}
return slow;
}
if (left != null) {
current.next = left;
}
if (right != null) {
current.next = right;
}
return dummy.next;
}
}
Greedy
//Dijkistra
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import static java.util.stream.Collectors.toList;
class Result {
while (!pq.isEmpty()) {
int[] current = pq.poll();
int currentNode = current[0];
int currentDistance = current[1];
// Explore neighbors
for (int[] neighbor : graph.get(currentNode)) {
int nextNode = neighbor[0];
int weight = neighbor[1];
return result;
}
}
int t = Integer.parseInt(bufferedReader.readLine().trim());
int s = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> result = Result.shortestReach(n, edges, s);
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(Collectors.joining(" ")) + "\n"
);
}
bufferedReader.close();
bufferedWriter.close();
}
}
//
//Prim
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'prims' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. 2D_INTEGER_ARRAY edges
* 3. INTEGER start
*/
return totalCost;
}
int n = Integer.parseInt(firstMultipleInput[0]);
int m = Integer.parseInt(firstMultipleInput[1]);
List<List<Integer>> edges = new ArrayList<>();
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
//Kruskal
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
class Result {
UnionFind(int size) {
parent = new int[size];
rank = new int[size];
for (int i = 0; i < size; i++) {
parent[i] = i;
rank[i] = 0;
}
}
int find(int u) {
if (parent[u] != u) {
parent[u] = find(parent[u]); // Path compression
}
return parent[u];
}
if (rootU != rootV) {
if (rank[rootU] > rank[rootV]) {
parent[rootV] = rootU;
} else if (rank[rootU] < rank[rootV]) {
parent[rootU] = rootV;
} else {
parent[rootV] = rootU;
rank[rootU]++;
}
}
}
}
return totalWeight;
}
// Edge class
static class Edge {
int u, v, weight;
bufferedReader.close();
bufferedWriter.close();
}
}
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'jimOrders' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts 2D_INTEGER_ARRAY orders as parameter.
*/
List<Integer>list=new ArrayList<>();
for(int i=0;i<orders.size();i++){
list.add(orders.get(i).get(0)+orders.get(i).get(1));
}
List<Integer>list2=new ArrayList<>(list);
Collections.sort(list2);// for sorting list
List<Integer>res=new ArrayList<>();
for(int i=0;i<list2.size();i++){
int a=list.indexOf(list2.get(i));
res.add(a+1);
list.set(a,123234);
}
return res;
}
int n = Integer.parseInt(bufferedReader.readLine().trim());
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(joining(" "))
+ "\n"
);
bufferedReader.close();
bufferedWriter.close();
}
}
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'toys' function below.
*
* The function is expected to return an INTEGER.
* The function accepts INTEGER_ARRAY w as parameter.
*/
// Move to the next toy that can be part of the current group
while (i < w.size() && w.get(i) <= groupStartWeight + 4) {
i++;
}
}
return groups;
}
List<Integer> w = Stream.of(bufferedReader.readLine().replaceAll("\\s+$",
"").split(" "))
.map(Integer::parseInt)
.collect(toList());
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
import java.util.*;
import java.util.stream.Collectors;
public class Solution {
System.out.println(result.stream().map(String::valueOf).collect(Collectors.joining(
" ")));
}
}
//Task scheduler
import java.io.*;
import java.util.*;
Dynamic programming
import java.io.*;
import java.util.*;
import java.util.stream.Collectors;
class Result {
public static List<Integer> longestCommonSubsequence(List<Integer> a,
List<Integer> b) {
int n = a.size();
int m = b.size();
Collections.reverse(lcs);
return lcs;
}
}
List<Integer> a = Arrays.stream(bufferedReader.readLine().replaceAll("\\s+
$", "").split(" "))
.map(Integer::parseInt)
.collect(Collectors.toList());
List<Integer> b = Arrays.stream(bufferedReader.readLine().replaceAll("\\s+
$", "").split(" "))
.map(Integer::parseInt)
.collect(Collectors.toList());
bufferedWriter.write(result.stream().map(Object::toString).collect(Collectors.joini
ng(" ")) + "\n");
bufferedReader.close();
bufferedWriter.close();
}
}
class Result {
/*
* Complete the 'getWays' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts following parameters:
* 1. INTEGER n
* 2. LONG_INTEGER_ARRAY c
*/
public static long getWays(int n, List<Long> c) {
// Create a DP array to store the number of ways to make each amount
long[] dp = new long[n + 1];
dp[0] = 1; // There is 1 way to make change for 0 amount (using no coins)
return dp[n];
}
int n = Integer.parseInt(firstMultipleInput[0]);
int m = Integer.parseInt(firstMultipleInput[1]);
List<Long> c = Stream.of(bufferedReader.readLine().replaceAll("\\s+$",
"").split(" "))
.map(Long::parseLong)
.collect(toList());
// Print the number of ways of making change for 'n' units using coins
having the values given by 'c'
bufferedWriter.write(String.valueOf(ways));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
import java.io.*;
import java.util.*;
int t = in.nextInt();
f = new long[1001000];
f[0] = 1;
for (int i = 1; i < f.length; i++)
f[i] = (i * f[i - 1]) % mod;
//Knapsack
import java.io.*;
import java.util.*;
import java.util.stream.*;
class Result {
/*
* Complete the 'unboundedKnapsack' function below.
*
* The function is expected to return an INTEGER.
* The function accepts following parameters:
* 1. INTEGER k
* 2. INTEGER_ARRAY arr
*/
return dp[k];
}
}
int t = Integer.parseInt(bufferedReader.readLine().trim());
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
}
bufferedReader.close();
bufferedWriter.close();
}
}
//Maximum subarray
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
/*
* Complete the 'maxSubarray' function below.
*
* The function is expected to return an INTEGER_ARRAY.
* The function accepts INTEGER_ARRAY arr as parameter.
*/
if (num > 0) {
sumNonContiguous += num;
hasPositive = true;
} else {
maxNonContiguous = Math.max(maxNonContiguous, num);
}
}
if (!hasPositive) {
sumNonContiguous = maxNonContiguous;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new
FileWriter(System.getenv("OUTPUT_PATH")));
int t = Integer.parseInt(bufferedReader.readLine().trim());
List<Integer> arr =
Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
.map(Integer::parseInt)
.collect(toList());
bufferedWriter.write(
result.stream()
.map(Object::toString)
.collect(joining(" "))
+ "\n"
);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
});
bufferedReader.close();
bufferedWriter.close();
}
}
Site iconTheCScience
HackerRank Find the Path Problem Solution
Yashwant Parihar Yashwant Parihar
1 year ago
In this post, we well solve HackerRank Find the Path Problem Solution.
You are given a table, a. with n rows and m columns. The top-left corner of the
table has coordinates (0, 0), and the bottom-right corner has coordinates (n-1, m-
1). The th cell contains integer aj.
A path in the table is a sequence of cells (r1, 1), (r2, C2),…, (rk, Ck) such that
for each {1,…,k-1}, cell (r, c) and cell (ri+1, C++1) share a side.
The weight of the path (r₁, c₁), (r₂, C2),…, (rk, Ck) is defined by 1 ar,,&, where
arc is the weight of the cell (r, c).
You must answer q queries. In each query, you are given the coordinates of two
cells, (ri, ci) and (r2, C2). You must find and print the minimum possible weight
of a path connecting them.
Note: A cell can share sides with at most 4 other cells. A cell with coordinates
(r, c) shares sides with (r-1,c), (r+1,c), (r, c-1) and (r, c + 1).
Input Format
The first line contains 2 space-separated integers, n (the number of rows in a) and
m (the number of columns in a), respectively.
Each of n subsequent lines contains m space-separated integers. The th integer in
the ¿th
line denotes the value of aij.
The next line contains a single integer, q, denoting the number of queries.
Each of the q subsequent lines describes a query in the form of 4 space-separated
integers:
3 5
0 0 0 0 0
1 9 9 9 1
0 0 0 0 0
3
0 0 2 4
0 3 2 3
1 1 1 3
Sample Output
1
1
18
int main(){
int m,q,i,j;
scanf("%d%d",&n,&m);
d=(int****)malloc(m*4*sizeof(int***));
for(i=0;i<n;i++)
for(j=0;j<m;j++)
scanf("%d",&a[i][j]);
build(1,0,m-1);
scanf("%d",&q);
while(q--){
scanf("%d%d%d%d",&r1,&c1,&r2,&c2);
ans=INF;
solve(1,0,m-1);
printf("%d\n",ans);
}
return 0;
}
int min(int x,int y){
return (x<y)?x:y;
}
void build(int v,int l,int r){
int tm,i,j;
tm=(l+r)/2;
d[v]=(int***)malloc(n*sizeof(int**));
for(i=0;i<n;i++){
d[v][i]=(int**)malloc(n*sizeof(int*));
for(j=0;j<n;j++)
d[v][i][j]=(int*)malloc((r-l+1)*sizeof(int));
DJ(i,tm-l,l,r,d[v][i]);
}
if(l!=r){
build(2*v,l,tm);
build(2*v+1,tm+1,r);
}
return;
}
void solve(int v,int l,int r){
int tm,i;
tm=(l+r)/2;
for(i=0;i<n;i++)
ans=min(ans,d[v][i][r1][c1-l]+d[v][i][r2][c2-l]+a[i][tm]);
if(c1<tm && c2<tm)
solve(2*v,l,tm);
else if(c1>tm && c2>tm)
solve(2*v+1,tm+1,r);
return;
}
void DJ(int si,int sj,int l,int r,int **d){
int i,j,next_solve_i,next_solve_j,heap_size=0;
node elem;
for(i=0;i<n;i++)
for(j=0;j<r-l+1;j++)
d[i][j]=-1;
d[si][sj]=0;
next_solve_i=si;
next_solve_j=sj;
while(1){
if(next_solve_i){
if(d[next_solve_i-1][next_solve_j]==-1 || d[next_solve_i-1]
[next_solve_j]>d[next_solve_i][next_solve_j]+a[next_solve_i-1][next_solve_j+l]){
elem.i=next_solve_i-1;
elem.j=next_solve_j;
elem.w=d[next_solve_i][next_solve_j]+a[next_solve_i-1][next_solve_j+l];
if(d[next_solve_i-1][next_solve_j]==-1)
heap_insert(&elem,&heap_size);
else
heap_update(&elem);
d[next_solve_i-1][next_solve_j]=d[next_solve_i][next_solve_j]
+a[next_solve_i-1][next_solve_j+l];
}
}
if(next_solve_i+1<n){
if(d[next_solve_i+1][next_solve_j]==-1 || d[next_solve_i+1]
[next_solve_j]>d[next_solve_i][next_solve_j]+a[next_solve_i+1][next_solve_j+l]){
elem.i=next_solve_i+1;
elem.j=next_solve_j;
elem.w=d[next_solve_i][next_solve_j]+a[next_solve_i+1][next_solve_j+l];
if(d[next_solve_i+1][next_solve_j]==-1)
heap_insert(&elem,&heap_size);
else
heap_update(&elem);
d[next_solve_i+1][next_solve_j]=d[next_solve_i][next_solve_j]
+a[next_solve_i+1][next_solve_j+l];
}
}
if(next_solve_j){
if(d[next_solve_i][next_solve_j-1]==-1 || d[next_solve_i][next_solve_j-
1]>d[next_solve_i][next_solve_j]+a[next_solve_i][next_solve_j-1+l]){
elem.i=next_solve_i;
elem.j=next_solve_j-1;
elem.w=d[next_solve_i][next_solve_j]+a[next_solve_i][next_solve_j-1+l];
if(d[next_solve_i][next_solve_j-1]==-1)
heap_insert(&elem,&heap_size);
else
heap_update(&elem);
d[next_solve_i][next_solve_j-1]=d[next_solve_i][next_solve_j]
+a[next_solve_i][next_solve_j-1+l];
}
}
if(next_solve_j+1+l<=r){
if(d[next_solve_i][next_solve_j+1]==-1 || d[next_solve_i]
[next_solve_j+1]>d[next_solve_i][next_solve_j]+a[next_solve_i][next_solve_j+1+l]){
elem.i=next_solve_i;
elem.j=next_solve_j+1;
elem.w=d[next_solve_i][next_solve_j]+a[next_solve_i][next_solve_j+1+l];
if(d[next_solve_i][next_solve_j+1]==-1)
heap_insert(&elem,&heap_size);
else
heap_update(&elem);
d[next_solve_i][next_solve_j+1]=d[next_solve_i][next_solve_j]
+a[next_solve_i][next_solve_j+1+l];
}
}
if(heap_size==0)
break;
heap_read(&heap_size,&elem);
next_solve_i=elem.i;
next_solve_j=elem.j;
}
return;
}
void heap_insert(node *elem,int *size){
(*size)++;
int index=(*size);
while(index>1 && elem->w<heap[index/2].w){
heap[index]=heap[index/2];
heap_list[heap[index].i][heap[index].j]=index;
index/=2;
}
heap[index]=(*elem);
heap_list[elem->i][elem->j]=index;
return;
}
void heap_update(node *elem){
int index=heap_list[elem->i][elem->j];
while(index>1 && elem->w<heap[index/2].w){
heap[index]=heap[index/2];
heap_list[heap[index].i][heap[index].j]=index;
index/=2;
}
heap[index]=(*elem);
heap_list[elem->i][elem->j]=index;
return;
}
void heap_read(int *size,node *ans){
(*ans)=heap[1];
int index=1;
while(index*2<*size && heap[*size].w>heap[index*2].w || index*2+1<*size &&
heap[*size].w>heap[index*2+1].w){
index=(heap[index*2].w>heap[index*2+1].w)?index*2+1:index*2;
heap[index/2]=heap[index];
heap_list[heap[index].i][heap[index].j]=index/2;
}
heap[index]=heap[*size];
heap_list[heap[index].i][heap[index].j]=index;
(*size)--;
return;
}
Find the Path C++ Solution
#include <bits/stdc++.h>
int N, M;
vector<vector<int64_t>> G;
const int64_t INF = 1e11;
vector<vector<int>> Qs;
vector<int64_t> ans;
int main() {
ios::sync_with_stdio(false);
cin >> N >> M;
G.assign(N, vector<int64_t>(M));
for (auto &v : G) {
for (int64_t& i : v) {
cin >> i;
}
}
int Q;
cin >> Q;
Qs.resize(Q);
ans.assign(Q, INF);
vector<int> Qis;
for (int i = 0; i < Q; ++i) {
int a,b,c,d;
cin >> a >> b >> c >> d;
Qs[i] = {a,b,c,d};
Qis.push_back(i);
}
div_and_conq(0, M-1, Qis);
for (int64_t i : ans) {
cout << i << endl;
}
return 0;
}
Find the Path C Sharp Solution
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;
struct Node
{
public int w;
public int r;
public int c;
class Heap
{
public int length = 0;
private Node[] heap;
class Collectively
{
private int n, m, max_w;
private int [] middle_w;
private int [,,] map;
private List<List<int>> a;
private Heap heap;
class Individually
{
private int n, m;
private List<List<int>> a;
private Heap heap;
class WorkSpace
{
private int n, m;
public int[] result;
private List<List<int>> a, queries, index_remained;
private bool[] index_solved;
private Collectively collectively;
private Individually individually;
class Result
{
public static List<int> shortestPath(List<List<int>> a, List<List<int>>
queries)
{
int m=a[0].Count();
WorkSpace work_space = new WorkSpace(a, queries);
work_space.solve(Enumerable.Range(0, queries.Count()).ToList(), 0, m-1);
work_space.solve_remained();
return new List<int>(work_space.result);
}
}
class Solution
{
public static void Main(string[] args)
{
TextWriter textWriter = new
StreamWriter(@System.Environment.GetEnvironmentVariable("OUTPUT_PATH"), true);
string[] firstMultipleInput = Console.ReadLine().TrimEnd().Split(' ');
int n = Convert.ToInt32(firstMultipleInput[0]);
int m = Convert.ToInt32(firstMultipleInput[1]);
int q = Convert.ToInt32(Console.ReadLine().Trim());
textWriter.WriteLine(String.Join("\n", result));
textWriter.Flush();
textWriter.Close();
}
}
public class C {
InputStream is;
PrintWriter out;
String INPUT = "";
void solve()
{
int n = ni(), m = ni();
int[][] a = new int[m][n];
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
a[j][i] = ni();
}
}
d = new long[m][n][][];
build(0, m, a);
int Q = ni();
for(int q = 0;q < Q;q++){
int sc = ni(), sr = ni();
int tc = ni(), tr = ni();
if(sr > tr){
{int du = tr; tr = sr; sr = du;}
{int du = tc; tc = sc; sc = du;}
}
out.println(go(0, m, sr, sc, tr, tc, a));
}
static long go(int L, int R, int sr, int sc, int tr, int tc, int[][] a)
{
int M = L+R>>>1;
int m = a[0].length;
long ret = Long.MAX_VALUE;
for(int i = 0;i < m;i++){
ret = Math.min(ret, d[M][i][sr-L][sc] + d[M][i][tr-L][tc] - a[M][i]);
}
if(sr <= M && M <= tr){
return ret;
}
if(R-L <= 1)return ret;
if(tr < M){
return Math.min(ret, go(L, M, sr, sc, tr, tc, a));
}else{
return Math.min(ret, go(M, R, sr, sc, tr, tc, a));
}
}
static long[][][][] d;
public static long[][] dijk(int[][] a, int sr, int sc, int L, int R)
{
int m = a[0].length;
long[][] td = new long[R-L][m];
for(int i = 0;i < R-L;i++){
Arrays.fill(td[i], Long.MAX_VALUE / 3);
}
td[sr-L][sc] = 0;
MinHeapL q = new MinHeapL((R-L)*m);
q.add((sr-L)*m+sc, 0L);
td[sr-L][sc] = a[sr][sc];
int[] dr = { 1, 0, -1, 0 };
int[] dc = { 0, 1, 0, -1 };
return td;
}
public MinHeapL(int m)
{
n = Integer.highestOneBit((m+1)<<1);
a = new long[n];
map = new int[n];
imap = new int[n];
Arrays.fill(a, INF);
Arrays.fill(map, -1);
Arrays.fill(imap, -1);
pos = 1;
}
pos--;
int rem = imap[ind];
long ret = a[rem];
map[rem] = map[pos];
imap[map[pos]] = rem;
imap[ind] = -1;
a[rem] = a[pos];
a[pos] = INF;
map[pos] = -1;
up(rem);
down(rem);
return ret;
}
long s = System.currentTimeMillis();
solve();
out.flush();
if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}