-
Notifications
You must be signed in to change notification settings - Fork 1
/
A.BoxisPull.java
330 lines (280 loc) · 7.51 KB
/
A.BoxisPull.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import java.text.DecimalFormat;
import java.util.stream.LongStream;
import java.util.stream.IntStream;
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0){
Naveen problem = new Naveen(sc);
problem.solve(out);
}
out.flush();
}
}
class Naveen {
int x1,x2,y1,y2;
//int[] arr;
Naveen(FastScanner sc) {
x1 = sc.nextInt();
y1 = sc.nextInt();
x2 = sc.nextInt();
y2 = sc.nextInt();
// arr = sc.arrayInt(n);
}
void solve(PrintWriter out) {
if(x1==x2||y1==y2){
out.println(Math.abs(x1-x2)+Math.abs(y1-y2));
}else{
out.println(Math.abs(x1-x2)+Math.abs(y1-y2)+2);
}
}
}
class FastScanner {
private final InputStream in = System.in;
private final byte[] buffer = new byte[1024];
private int ptr = 0;
private int buflen = 0;
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 static boolean isPrintableChar(int c) {
return 33 <= c && c <= 126;
}
public boolean hasNext() {
while (hasNextByte() && !isPrintableChar(buffer[ptr])) {
ptr++;
}
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();
}
}
public int nextInt() {
long nl = nextLong();
if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) {
throw new NumberFormatException();
}
return (int) nl;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] arrayInt(int N) {
int[] array = new int[N];
for (int i = 0; i < N; i++) {
array[i] = nextInt();
}
return array;
}
public long[] arrayLong(int N) {
long[] array = new long[N];
for (int i = 0; i < N; i++) {
array[i] = nextLong();
}
return array;
}
public double[] arrayDouble(int N) {
double[] array = new double[N];
for (int i = 0; i < N; i++) {
array[i] = nextDouble();
}
return array;
}
public String[] arrayString(int N) {
String[] array = new String[N];
for (int i = 0; i < N; i++) {
array[i] = next();
}
return array;
}
public int randomInt() {
Random r = new Random();
int value = r.nextInt((int) 1e6);
System.out.println(value);
return value;
}
public int[] randomInt(int N) {
int[] array = new int[N];
Random r = new Random();
for (int i = 0; i < N; i++) {
array[i] = r.nextInt((int) 1e6);
}
System.out.println(Arrays.toString(array));
return array;
}
}
class My {
public static long lower(long arr[],long key){
int low = 0;
int high = arr.length-1;
while(low < high){
int mid = low + (high - low)/2;
if(arr[mid] >= key){
high = mid;
}
else{
low = mid+1;
}
}
return low;
}
public static int upper(int arr[],int key){
int low = 0;
int high = arr.length-1;
while(low < high){
int mid = low + (high - low+1)/2;
if(arr[mid] <= key){
low = mid;
}
else{
high = mid-1;
}
}
return low;
}
static void ans(boolean b) {
System.out.println(b ? "Yes" : "No");
}
static void ANS(boolean b) {
System.out.println(b ? "YES" : "NO");
}
static String sort(String s) {
char[] ch = s.toCharArray();
Arrays.sort(ch);
return String.valueOf(ch);
}
static String reverse(String s) {
return new StringBuilder(s).reverse().toString();
}
static int[] reverse(int[] array) {
for (int i = 0; i < array.length / 2; i++) {
int temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
return array;
}
static long[] reverse(long[] array) {
for (int i = 0; i < array.length / 2; i++) {
long temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
return array;
}
static double[] reverse(double[] array) {
for (int i = 0; i < array.length / 2; i++) {
double temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
return array;
}
static String[] reverse(String[] array) {
for (int i = 0; i < array.length / 2; i++) {
String temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
return array;
}
static char[] reverse(char[] array) {
for (int i = 0; i < array.length / 2; i++) {
char temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
return array;
}
static long min(long... numbers) {
Arrays.sort(numbers);
return numbers[0];
}
static int min(int... numbers) {
Arrays.sort(numbers);
return numbers[0];
}
static double min(double... numbers) {
Arrays.sort(numbers);
return numbers[0];
}
static long max(long... numbers) {
Arrays.sort(numbers);
return numbers[numbers.length - 1];
}
static int max(int... numbers) {
Arrays.sort(numbers);
return numbers[numbers.length - 1];
}
static double max(double... numbers) {
Arrays.sort(numbers);
return numbers[numbers.length - 1];
}
static int sum(long number) {
int sum = 0;
while (number > 0) {
sum += number % 10;
number /= 10;
}
return sum;
}
}