-
Notifications
You must be signed in to change notification settings - Fork 15
/
fair-fight.py
68 lines (57 loc) · 2.86 KB
/
fair-fight.py
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
# Copyright (c) 2019 kamyu. All rights reserved.
#
# Google Code Jam 2019 Round 1B - Problem C. Fair Fight
# https://codingcompetitions.withgoogle.com/codejam/round/0000000000051706/0000000000122838
#
# Time: O(NlogN)
# Space: O(N)
#
def update_descending_stack(A, curr_max_idxs, i, keep_the_same=False):
while curr_max_idxs and A[curr_max_idxs[-1]] <= A[i]-int(keep_the_same):
curr_max_idxs.pop()
curr_max_idxs.append(i)
def lower_bound(A, curr_max_idxs, target):
left, right = 0, len(curr_max_idxs)-1
while left <= right:
mid = left + (right-left)//2
if A[curr_max_idxs[mid]] <= target:
right = mid-1
else:
left = mid+1
return left
def fair_fight():
N, K = map(int, raw_input().strip().split())
C = map(int, raw_input().strip().split())
D = map(int, raw_input().strip().split())
R_lookup = []
C_curr_max_idxs, D_curr_max_idxs = [], [] # descending stack
for i in reversed(xrange(N)):
update_descending_stack(C, C_curr_max_idxs, i)
update_descending_stack(D, D_curr_max_idxs, i)
if D[i]-C[i] > K: # skip impossible intervals to save time and space
continue
D_R_good_it = lower_bound(D, D_curr_max_idxs, C[i]+K)
D_R_bad_it = lower_bound(D, D_curr_max_idxs, C[i]-K-1)
D_R_good = D_curr_max_idxs[D_R_good_it-1]-1 if D_R_good_it >= 1 else N-1 # rightmost idx of max_D s.t. max_D-Ci <= K
D_R_bad = D_curr_max_idxs[D_R_bad_it-1]-1 if D_R_bad_it >= 1 else N-1 # rightmost idx of max_D s.t. max_D-Ci <= -K-1
C_R = C_curr_max_idxs[-2]-1 if len(C_curr_max_idxs) >= 2 else N-1 # rightmost idx of C s.t. Ci >= C[idx]
R_good, R_bad = min(D_R_good, C_R), min(D_R_bad, C_R)
R_lookup.append((R_good, R_bad))
result = 0
C_curr_max_idxs, D_curr_max_idxs = [], [] # descending stack
for i in xrange(N):
update_descending_stack(C, C_curr_max_idxs, i, True) # keep the idx where C[idx] == Ci
update_descending_stack(D, D_curr_max_idxs, i)
if D[i]-C[i] > K: # skip impossible intervals to save time and space
continue
D_L_good_it = lower_bound(D, D_curr_max_idxs, C[i]+K)
D_L_bad_it = lower_bound(D, D_curr_max_idxs, C[i]-K-1)
D_L_good = D_curr_max_idxs[D_L_good_it-1]+1 if D_L_good_it >= 1 else 0 # leftmost idx of max_D s.t. max_D-Ci <= K
D_L_bad = D_curr_max_idxs[D_L_bad_it-1]+1 if D_L_bad_it >= 1 else 0 # leftmost idx of max_D s.t. max_D-Ci <= -K-1
C_L = C_curr_max_idxs[-2]+1 if len(C_curr_max_idxs) >= 2 else 0 # leftmost idx of C s.t. C[idx] < Ci
L_good, L_bad = max(D_L_good, C_L), max(D_L_bad, C_L)
R_good, R_bad = R_lookup.pop()
result += (i-L_good+1)*(R_good-i+1)-(i-L_bad+1)*(R_bad-i+1)
return result
for case in xrange(input()):
print 'Case #%d: %s' % (case+1, fair_fight())