FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java-Competitive-Programming/Algorithms/HLD_Nodes.java at master · joney000/Java-Competitive-Programming · GitHub
joney000
/
Java-Competitive-Programming
Public
Notifications
You must be signed in to change notification settings
Fork
30
Star
123
Code
Issues
0
Pull requests
1
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java-Competitive-Programming
/
Algorithms
/
HLD_Nodes.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
executable file
·
709 lines (651 loc) · 17.2 KB
Breadcrumbs
Java-Competitive-Programming
/
Algorithms
/
HLD_Nodes.java
Copy path
File metadata and controls
executable file
·
709 lines (651 loc) · 17.2 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
//pakage joney_000[let_me_start]
//
import
java
.
util
.*;
import
java
.
lang
.*;
import
java
.
io
.*;
import
java
.
math
.*;
/*
* Author : joney_000[let_me_start]
* Algorithm : Not Specified
* Platform : Codechef[https://www.codechef.com/problems/CHEFTREE]
*
*/
/* The Main Class */
class
Node
{
int
cnt
=
0
;
}
class
A
{
private
InputStream
inputStream
;
private
OutputStream
outputStream
;
private
FastReader
in
;
private
PrintWriter
out
;
/*
Overhead [Additional Temporary Strorage] but provides memory reusibility for multiple test cases.
Size Limit : 10^5 + 4
*/
private
final
int
BUFFER
=
100005
;
private
int
tempints
[] =
new
int
[
BUFFER
];
private
long
templongs
[] =
new
long
[
BUFFER
];
private
double
tempdoubles
[] =
new
double
[
BUFFER
];
private
char
tempchars
[] =
new
char
[
BUFFER
];
private
final
long
mod
=
1000000000
+
7
;
private
final
int
INF
=
Integer
.
MAX_VALUE
/
10
;
private
final
long
INF_L
=
Long
.
MAX_VALUE
/
10
;
public
A
(){}
public
A
(
boolean
stdIO
)
throws
FileNotFoundException
{
if
(
stdIO
){
inputStream
=
System
.
in
;
outputStream
=
System
.
out
;
}
else
{
inputStream
=
new
FileInputStream
(
"input.txt"
);
outputStream
=
new
FileOutputStream
(
"output.txt"
);
}
in
=
new
FastReader
(
inputStream
);
out
=
new
PrintWriter
(
outputStream
);
}
// fine place for Global Variables
final
int
N
=
101005
;
int
LOGN
=
20
;
int
w
[] =
new
int
[
N
];
//weight on i'th node
ArrayList
adj
[] =
new
ArrayList
[
N
];
long
A
=
0
;
long
B
=
0
;
int
root
=
1
;
int
ptr
=
0
;
//total no of element in base array
int
chainNo
=
0
;
int
baseArray
[] =
new
int
[
N
];
//Base Array For Seg Tree
int
chainIndex
[] =
new
int
[
N
];
//chainIndex[v] = chainNo
int
chainHead
[] =
new
int
[
N
];
//chainHead[chainNo] = v
int
posInBase
[] =
new
int
[
N
];
//posInBase[v] = ptr
int
depth
[] =
new
int
[
N
];
// depth of node i in dfs
int
f
[][] =
new
int
[
LOGN
][
N
];
//father sparse array
int
subsize
[] =
new
int
[
N
];
//subtree size of node i
Node
st
[] =
new
Node
[
4
*
N
+
50000
];
// The Segment Tree
int
log
[] =
new
int
[
N
+
5
];
void
run
()
throws
Exception
{
// int tests = i();
once
();
// for(int t = 1 ; t<= tests ; t++){
int
n
=
i
();
// A = l(); B = l();
for
(
int
i
=
1
;
i
<=
n
;
i
++){
w
[
i
] =
0
;
}
clear
(
n
);
int
e
=
n
-
1
;
for
(
int
i
=
1
;
i
<=
e
;
i
++){
int
u
=
i
();
int
v
=
i
();
adj
[
u
].
add
(
v
);
adj
[
v
].
add
(
u
);
}
dfs
(
root
, -
1
,
0
);
// We set up subsize, parent and depth for each node before sparse table
// out.write("here"+"\n");
// out.flush();
HLD
(
root
, -
1
);
// We decomposed the tree and created baseArray
// out.write("here1"+"\n");
// out.flush();
make_tree
(
1
,
1
,
ptr
-
1
);
// We use baseArray and construct the needed segment tree
// out.write("here2"+"\n");
// out.flush();
make_sparseTable
(
n
);
// sparse table
// out.write("here3"+"\n");
// out.flush();
// for(int i = 1;i<=n;i++){
// out.write("vtx ="+i+" posInbase="+posInBase[i]+"\n");
//
// }
// for(int i = 1;i<=chainNo;i++){
// out.write("vtx ="+i+" head="+chainHead[i]+"\n");
//
// }
// out.flush();
int
q
=
i
();
for
(
int
qq
=
1
;
qq
<=
q
;
qq
++){
char
type
=
c
();
// out.write("here qry"+qq+"\n");
// out.flush();
if
(
type
==
'I'
){
int
v
=
i
();
int
wt
=
i
();
change
(
v
,
wt
);
//node update
}
else
{
int
u
=
i
() ;
int
v
=
i
();
int
ans
=
query
(
u
,
v
);
//int ans = query_up(v,u);
//if(ans == INF)out.write("-1"+"\n");
out
.
write
(
""
+
ans
+
"
\n
"
);
// out.flush();
}
}
// }//end tests
}
//end run
void
once
(){
for
(
int
i
=
2
;
i
<=
N
;
i
++){
log
[
i
] =
log
[
i
/
2
] +
1
;
}
for
(
int
i
=
0
;
i
<=
N
-
1
;
i
++){
adj
[
i
] =
new
ArrayList
<
Integer
>();
}
for
(
int
i
=
0
;
i
<=
4
*
N
+
50000
-
1
;
i
++ ){
st
[
i
] =
new
Node
();
}
}
void
clear
(
int
n
){
chainNo
=
ptr
=
root
=
1
;
LOGN
=
log
[
n
]+
1
;
for
(
int
i
=
0
;
i
<=
n
+
10
;
i
++ ){
adj
[
i
].
clear
();
chainHead
[
i
] = -
1
;
depth
[
i
] =
0
;
subsize
[
i
] =
0
;
posInBase
[
i
] =
0
;
chainIndex
[
i
] =
0
;
baseArray
[
i
] =
0
;
for
(
int
j
=
0
;
j
<
LOGN
;
j
++)
f
[
j
][
i
] = -
1
;
}
for
(
int
i
=
1
;
i
<=
4
*
n
+
500
-
1
;
i
++ ){
st
[
i
].
cnt
=
0
;
}
}
void
dfs
(
int
cur
,
int
prev
,
int
_depth
) {
f
[
0
][
cur
] =
prev
;
depth
[
cur
] =
_depth
;
subsize
[
cur
] =
1
;
for
(
int
i
=
0
;
i
<
adj
[
cur
].
size
();
i
++)
if
((
int
)
adj
[
cur
].
get
(
i
) !=
prev
) {
//otherEnd[(int)idx[cur].get(i)] = (int)adj[cur].get(i);
dfs
((
int
)
adj
[
cur
].
get
(
i
),
cur
,
_depth
+
1
);
subsize
[
cur
] += (
int
)
subsize
[(
int
)
adj
[
cur
].
get
(
i
)];
}
}
void
HLD
(
int
curNode
,
int
prev
) {
if
(
chainHead
[
chainNo
] == -
1
) {
chainHead
[
chainNo
] =
curNode
;
// Assign chain head
}
chainIndex
[
curNode
] =
chainNo
;
posInBase
[
curNode
] =
ptr
;
// Position of this node in baseArray which we will use in Segtree
baseArray
[
ptr
] =
curNode
;
// baseArray contains the weights in the path / chain
ptr
++;
int
sc
= -
1
;
// Loop to find special child
for
(
int
i
=
0
;
i
<
adj
[
curNode
].
size
();
i
++)
if
((
int
)
adj
[
curNode
].
get
(
i
) !=
prev
) {
if
(
sc
== -
1
||
subsize
[
sc
] < (
int
)
subsize
[(
int
)
adj
[
curNode
].
get
(
i
)]) {
sc
= (
int
)
adj
[
curNode
].
get
(
i
);
}
}
if
(
sc
!= -
1
) {
// Expand the chain
HLD
(
sc
,
curNode
);
}
for
(
int
i
=
0
;
i
<
adj
[
curNode
].
size
();
i
++)
if
((
int
)
adj
[
curNode
].
get
(
i
) !=
prev
) {
if
(
sc
!= (
int
)
adj
[
curNode
].
get
(
i
)) {
// New chains at each normal node
chainNo
++;
HLD
((
int
)
adj
[
curNode
].
get
(
i
),
curNode
);
}
}
}
void
make_tree
(
int
curr
,
int
s
,
int
e
) {
if
(
s
==
e
) {
st
[
curr
].
cnt
=
w
[
s
];
return
;
}
make_tree
(
2
*
curr
,
s
, (
s
+
e
)/
2
);
make_tree
(
2
*
curr
+
1
, ((
s
+
e
)/
2
) +
1
,
e
);
st
[
curr
].
cnt
=
Math
.
max
(
st
[
2
*
curr
].
cnt
,
st
[
2
*
curr
+
1
].
cnt
);
}
void
make_sparseTable
(
int
n
){
// Below Dynamic programming code is ST for LCA.
for
(
int
i
=
1
;
i
<
LOGN
;
i
++)
for
(
int
j
=
1
;
j
<=
n
;
j
++)
if
(
f
[
i
-
1
][
j
] != -
1
)
f
[
i
][
j
] =
f
[
i
-
1
][
f
[
i
-
1
][
j
]];
}
int
query
(
int
u
,
int
v
)
throws
IOException
{
int
lca
=
LCA
(
u
,
v
);
// out.write("here4 lca("+u+","+v+")="+lca+"\n");
// out.flush();
int
a
=
query_up
(
u
,
lca
);
// One part of path
// out.write("here5 a(u,lca) = "+a+"\n");
// out.flush();
int
b
=
query_up
(
v
,
lca
);
// another part of path
// out.write("here6 b(v,lca) = "+b+"\n");
// out.flush();
int
ans
=
Math
.
max
(
a
,
b
);
// take the minimum of both paths
return
ans
;
}
int
LCA
(
int
u
,
int
v
) {
if
(
depth
[
u
] <
depth
[
v
]) {
int
temp
=
u
;
u
=
v
;
v
=
temp
;
/*swap*/
}
int
diff
=
depth
[
u
] -
depth
[
v
];
for
(
int
i
=
0
;
i
<
LOGN
;
i
++)
if
( ((
diff
>>
i
)&
1
) ==
1
)
u
=
f
[
i
][
u
];
if
(
u
==
v
)
return
u
;
for
(
int
i
=
LOGN
-
1
;
i
>=
0
;
i
--)
if
(
f
[
i
][
u
] !=
f
[
i
][
v
]) {
u
=
f
[
i
][
u
];
v
=
f
[
i
][
v
];
}
return
f
[
0
][
u
];
}
int
query_up
(
int
u
,
int
v
) {
//if(u == v) return query_tree(1, 1, ptr-1, posInBase[v], posInBase[u]); // Trivial soln for node
int
uchain
=
0
;
int
vchain
=
chainIndex
[
v
];
int
ans
=
0
;
// uchain and vchain are chain numbers of u and v
while
(
true
) {
uchain
=
chainIndex
[
u
];
if
(
uchain
==
vchain
) {
// Both u and v are in the same chain, so we need to query from u to v, update answer and break.
// We break because we came from u up till v, we are done
// out.write("qtree1: ptr="+(ptr-1)+" "+posInBase[v]+" "+ posInBase[u]+" u="+u+" v="+v+" ans = "+ans+"\n");
// out.flush();
//
int
a
=
query_tree
(
1
,
1
,
ptr
-
1
,
posInBase
[
v
],
posInBase
[
u
]);
ans
=
Math
.
max
(
a
,
ans
);
// Update answer
break
;
}
// out.write("qtree2: ptr="+(ptr-1)+" "+posInBase[chainHead[uchain]]+" "+ posInBase[u]+" u="+u+" chainHead[uchain]= "+chainHead[uchain]+" v="+v+" uchain="+uchain+" ans = "+ans+"\n");
// out.flush();
int
b
=
query_tree
(
1
,
1
,
ptr
-
1
,
posInBase
[
chainHead
[
uchain
]],
posInBase
[
u
]);
// Above is call to segment tree query function. We do from chainHead of u till u. That is the whole chain from
// start till head. We then update the answer
ans
=
Math
.
max
(
ans
,
b
);
u
=
chainHead
[
uchain
];
// move u to u's chainHead
u
=
f
[
0
][
u
];
//Then move to its parent, that means we changed chains
}
return
ans
;
}
/*
* query_tree:
* Given S and E, it will return the maximum value in the range [S,E]
*/
int
query_tree
(
int
curr
,
int
s
,
int
e
,
int
S
,
int
E
) {
if
(
s
>
E
||
e
<
S
||
s
>
e
) {
//invalid condition
return
0
;
}
if
(
s
>=
S
&&
e
<=
E
) {
return
st
[
curr
].
cnt
;
}
int
a
=
query_tree
(
2
*
curr
,
s
, (
s
+
e
)/
2
,
S
,
E
);
int
b
=
query_tree
(
2
*
curr
+
1
, ((
s
+
e
)/
2
) +
1
,
e
,
S
,
E
);
return
Math
.
max
(
a
,
b
);
}
void
change
(
int
v
,
int
wt
) {
w
[
v
] +=
wt
;
//point update
update_tree
(
1
,
1
,
ptr
-
1
,
posInBase
[
v
] ,
posInBase
[
v
]);
}
/*
* update_tree:
* Point update. Update a single element of the segment tree.
*/
void
update_tree
(
int
curr
,
int
s
,
int
e
,
int
S
,
int
E
) {
if
(
s
>
E
||
e
<
S
||
s
>
e
)
return
;
if
(
s
>=
S
&&
e
<=
E
) {
st
[
curr
].
cnt
=
w
[
baseArray
[
s
]];
//pos in base array is different
return
;
}
update_tree
(
2
*
curr
,
s
, (
s
+
e
)/
2
,
S
,
E
);
update_tree
(
2
*
curr
+
1
, ((
s
+
e
)/
2
) +
1
,
e
,
S
,
E
);
st
[
curr
].
cnt
=
Math
.
max
(
st
[
2
*
curr
].
cnt
,
st
[
2
*
curr
+
1
].
cnt
);
}
//****************************** My Utilities ***********************//
void
var_dump
(
Object
...
o
) {
out
.
write
(
"
\n
"
+
Arrays
.
deepToString
(
o
)+
"
\n
"
);
out
.
flush
();
}
boolean
isPrime
(
long
n
)
throws
Exception
{
if
(
n
==
1
)
return
false
;
if
(
n
<=
3
)
return
true
;
if
(
n
%
2
==
0
)
return
false
;
for
(
int
i
=
2
;
i
<=
Math
.
sqrt
(
n
);
i
++){
if
(
n
%
i
==
0
)
return
false
;
}
return
true
;
}
// sieve
int
[]
primes
(
int
n
)
throws
Exception
{
// for(int i=1;i<=arr.length-1;i++)out.write(""+arr[i]+" ");
boolean
arr
[] =
new
boolean
[
n
+
1
];
Arrays
.
fill
(
arr
,
true
);
arr
[
1
]=
false
;
for
(
int
i
=
2
;
i
<=
Math
.
sqrt
(
n
);
i
++){
if
(!
arr
[
i
])
continue
;
for
(
int
j
=
2
*
i
;
j
<=
n
;
j
+=
i
){
arr
[
j
]=
false
;
}
}
LinkedList
<
Integer
>
ll
=
new
LinkedList
<
Integer
>();
for
(
int
i
=
1
;
i
<=
n
;
i
++){
if
(
arr
[
i
])
ll
.
add
(
i
);
}
n
=
ll
.
size
();
int
primes
[] =
new
int
[
n
+
1
];
for
(
int
i
=
1
;
i
<=
n
;
i
++){
primes
[
i
]=
ll
.
removeFirst
();
}
return
primes
;
}
long
gcd
(
long
a
,
long
b
)
throws
Exception
{
if
(
b
==
0
)
return
a
;
return
gcd
(
b
,
a
%
b
);
}
long
lcm
(
long
a
,
long
b
)
throws
Exception
{
if
(
a
==
0
||
b
==
0
)
return
0
;
return
(
a
*
b
)/
gcd
(
a
,
b
);
}
long
mulmod
(
long
a
,
long
b
,
long
mod
)
throws
Exception
{
if
(
a
==
0
||
b
==
0
)
return
0
;
if
(
b
==
1
)
return
a
;
long
ans
=
mulmod
(
a
,
b
/
2
,
mod
);
ans
= (
ans
*
2
)%
mod
;
if
(
b
%
2
==
1
)
ans
= (
a
+
ans
)%
mod
;
return
ans
;
}
long
pow
(
long
a
,
long
b
,
long
mod
)
throws
Exception
{
if
(
b
==
0
)
return
1
;
if
(
b
==
1
)
return
a
;
long
ans
=
pow
(
a
,
b
/
2
,
mod
);
ans
= (
ans
*
ans
)%
mod
;
if
(
b
%
2
==
1
)
ans
= (
a
*
ans
)%
mod
;
return
ans
;
}
// 20*20 nCr Pascal Table
long
[][]
ncrTable
()
throws
Exception
{
long
ncr
[][] =
new
long
[
21
][
21
];
for
(
int
i
=
0
;
i
<=
20
;
i
++){
ncr
[
i
][
0
]=
1
;
ncr
[
i
][
i
]=
1
;}
for
(
int
j
=
0
;
j
<=
20
;
j
++){
for
(
int
i
=
j
+
1
;
i
<=
20
;
i
++){
ncr
[
i
][
j
] =
ncr
[
i
-
1
][
j
]+
ncr
[
i
-
1
][
j
-
1
];
}
}
return
ncr
;
}
//*******************************I/O******************************//
int
i
()
throws
Exception
{
//return Integer.parseInt(br.readLine().trim());
return
in
.
nextInt
();
}
int
[]
is
(
int
n
)
throws
Exception
{
//int arr[] = new int[n+1];
for
(
int
i
=
1
;
i
<=
n
;
i
++)
tempints
[
i
] =
in
.
nextInt
();
return
tempints
;
}
long
l
()
throws
Exception
{
return
in
.
nextLong
();
}
long
[]
ls
(
int
n
)
throws
Exception
{
for
(
int
i
=
1
;
i
<=
n
;
i
++)
templongs
[
i
] =
in
.
nextLong
();
return
templongs
;
}
double
d
()
throws
Exception
{
return
in
.
nextDouble
();
}
double
[]
ds
(
int
n
)
throws
Exception
{
for
(
int
i
=
1
;
i
<=
n
;
i
++)
tempdoubles
[
i
] =
in
.
nextDouble
();
return
tempdoubles
;
}
char
c
()
throws
Exception
{
return
in
.
nextCharacter
();
}
char
[]
cs
(
int
n
)
throws
Exception
{
for
(
int
i
=
1
;
i
<=
n
;
i
++)
tempchars
[
i
] =
in
.
nextCharacter
();
return
tempchars
;
}
String
s
()
throws
Exception
{
return
in
.
nextLine
();
}
BigInteger
bi
()
throws
Exception
{
return
in
.
nextBigInteger
();
}
//***********************I/O ENDS ***********************//
//*********************** 0.3%f [precision]***********************//
/* roundoff upto 2 digits
double roundOff = Math.round(a * 100.0) / 100.0;
or
System.out.printf("%.2f", val);
*/
/*
print upto 2 digits after decimal
val = ((long)(val * 100.0))/100.0;
*/
private
void
closeResources
(){
out
.
flush
();
out
.
close
();
return
;
}
public
static
void
main
(
String
[]
args
)
throws
java
.
lang
.
Exception
{
//let_me_start Shinch Returns
/*
// Old Reader Writer
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out=new BufferedWriter(new OutputStreamWriter(System.out));
BufferedReader br=new BufferedReader(new FileReader("input.txt"));
BufferedWriter out=new BufferedWriter(new FileWriter("output.txt"));
*/
A
driver
=
new
A
(
true
);
long
start
=
System
.
currentTimeMillis
();
driver
.
run
();
long
end
=
System
.
currentTimeMillis
();
//out.write(" Total Time : "+(end - start)+"\n");
driver
.
closeResources
();
return
;
}
}
class
FastReader
{
private
boolean
finished
=
false
;
private
InputStream
stream
;
private
byte
[]
buf
=
new
byte
[
8
*
1024
];
private
int
curChar
;
private
int
numChars
;
private
SpaceCharFilter
filter
;
public
FastReader
(
InputStream
stream
){
this
.
stream
=
stream
;
}
public
int
read
(){
if
(
numChars
== -
1
){
throw
new
InputMismatchException
();
}
if
(
curChar
>=
numChars
){
curChar
=
0
;
try
{
numChars
=
stream
.
read
(
buf
);
}
catch
(
IOException
e
){
throw
new
InputMismatchException
();
}
if
(
numChars
<=
0
){
return
-
1
;
}
}
return
buf
[
curChar
++];
}
public
int
peek
(){
if
(
numChars
== -
1
){
return
-
1
;
}
if
(
curChar
>=
numChars
){
curChar
=
0
;
try
{
numChars
=
stream
.
read
(
buf
);
}
catch
(
IOException
e
){
return
-
1
;
}
if
(
numChars
<=
0
){
return
-
1
;
}
}
return
buf
[
curChar
];
}
public
int
nextInt
(){
int
c
=
read
();
while
(
isSpaceChar
(
c
))
c
=
read
();
int
sgn
=
1
;
if
(
c
==
'-'
){
sgn
= -
1
;
c
=
read
();
}
int
res
=
0
;
do
{
if
(
c
==
','
){
c
=
read
();
}
if
(
c
<
'0'
||
c
>
'9'
){
throw
new
InputMismatchException
();
}
res
*=
10
;
res
+=
c
-
'0'
;
c
=
read
();
}
while
(!
isSpaceChar
(
c
));
return
res
*
sgn
;
}
public
long
nextLong
(){
int
c
=
read
();
while
(
isSpaceChar
(
c
))
c
=
read
();
int
sgn
=
1
;
if
(
c
==
'-'
){
sgn
= -
1
;
c
=
read
();
}
long
res
=
0
;
do
{
if
(
c
<
'0'
||
c
>
'9'
){
throw
new
InputMismatchException
();
}
res
*=
10
;
res
+=
c
-
'0'
;
c
=
read
();
}
while
(!
isSpaceChar
(
c
));
return
res
*
sgn
;
}
public
String
nextString
(){
int
c
=
read
();
while
(
isSpaceChar
(
c
))
c
=
read
();
StringBuilder
res
=
new
StringBuilder
();
do
{
res
.
appendCodePoint
(
c
);
c
=
read
();
}
while
(!
isSpaceChar
(
c
));
return
res
.
toString
();
}
public
boolean
isSpaceChar
(
int
c
){
if
(
filter
!=
null
){
return
filter
.
isSpaceChar
(
c
);
}
return
isWhitespace
(
c
);
}
public
static
boolean
isWhitespace
(
int
c
){
return
c
==
' '
||
c
==
'\n'
||
c
==
'\r'
||
c
==
'\t'
||
c
== -
1
;
}
private
String
readLine0
(){
StringBuilder
buf
=
new
StringBuilder
();
int
c
=
read
();
while
(
c
!=
'\n'
&&
c
!= -
1
){
if
(
c
!=
'\r'
){
buf
.
appendCodePoint
(
c
);
}
c
=
read
();
}
return
buf
.
toString
();
}
public
String
nextLine
(){
String
s
=
readLine0
();
while
(
s
.
trim
().
length
() ==
0
)
s
=
readLine0
();
return
s
;
}
public
String
nextLine
(
boolean
ignoreEmptyLines
){
if
(
ignoreEmptyLines
){
return
nextLine
();
}
else
{
return
readLine0
();
}
}
public
BigInteger
nextBigInteger
(){
try
{
return
new
BigInteger
(
nextString
());
}
catch
(
NumberFormatException
e
){
throw
new
InputMismatchException
();
}
}
public
char
nextCharacter
(){
int
c
=
read
();
while
(
isSpaceChar
(
c
))
c
=
read
();
return
(
char
)
c
;
}
public
double
nextDouble
(){
int
c
=
read
();
while
(
isSpaceChar
(
c
))
c
=
read
();
int
sgn
=
1
;
if
(
c
==
'-'
){
sgn
= -
1
;
c
=
read
();
}
double
res
=
0
;
while
(!
isSpaceChar
(
c
) &&
c
!=
'.'
){
if
(
c
==
'e'
||
c
==
'E'
){
return
res
*
Math
.
pow
(
10
,
nextInt
());
}
if
(
c
<
'0'
||
c
>
'9'
){
throw
new
InputMismatchException
();
}
res
*=
10
;
res
+=
c
-
'0'
;
c
=
read
();
}
if
(
c
==
'.'
){
c
=
read
();
double
m
=
1
;
while
(!
isSpaceChar
(
c
)){
if
(
c
==
'e'
||
c
==
'E'
){
return
res
*
Math
.
pow
(
10
,
nextInt
());
}
if
(
c
<
'0'
||
c
>
'9'
){
throw
new
InputMismatchException
();
}
m
/=
10
;
res
+= (
c
-
'0'
) *
m
;
c
=
read
();
}
}
return
res
*
sgn
;
}
public
boolean
isExhausted
(){
int
value
;
while
(
isSpaceChar
(
value
=
peek
()) &&
value
!= -
1
)
read
();
return
value
== -
1
;
}
public
String
next
(){
return
nextString
();
}
public
SpaceCharFilter
getFilter
(){
return
filter
;
}
public
void
setFilter
(
SpaceCharFilter
filter
){
this
.
filter
=
filter
;
}
public
interface
SpaceCharFilter
{
public
boolean
isSpaceChar
(
int
ch
);
}
}
/******************** Pair class ***********************/
class
Pair
implements
Comparable
<
Pair
>{
public
int
a
;
public
int
b
;
public
Pair
(){
this
.
a
=
0
;
this
.
b
=
0
;
}
public
Pair
(
int
a
,
int
b
){
this
.
a
=
a
;
this
.
b
=
b
;
}
public
int
compareTo
(
Pair
p
){
if
(
this
.
a
==
p
.
a
){
return
this
.
b
-
p
.
b
;
}
return
this
.
a
-
p
.
a
;
}
public
String
toString
(){
return
"a="
+
this
.
a
+
" b="
+
this
.
b
;
}
}
Back
|
FazBrowse Home
|
New Git URL