結果
問題 | No.404 部分門松列 |
ユーザー |
![]() |
提出日時 | 2025-03-20 20:58:25 |
言語 | PyPy3 (7.3.15) |
結果 |
TLE
|
実行時間 | - |
コード長 | 3,015 bytes |
コンパイル時間 | 717 ms |
コンパイル使用メモリ | 82,516 KB |
実行使用メモリ | 76,984 KB |
最終ジャッジ日時 | 2025-03-20 20:59:02 |
合計ジャッジ時間 | 6,358 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 9 TLE * 1 -- * 21 |
ソースコード
import sysfrom bisect import bisect_left, bisect_rightdef main():input = sys.stdin.read().split()idx = 0N = int(input[idx]); idx +=1a = list(map(int, input[idx:idx+N])); idx += NQ = int(input[idx]); idx +=1queries = []for _ in range(Q):L = int(input[idx])H = int(input[idx+1])queries.append( (L, H) )idx +=2# Precompute all possible tripletsres = []for j in range(N):if j ==0 or j ==N-1:continue # can't be middle# for j as middle elementa_j = a[j]left = a[:j]right = a[j+1:]# Case 1: a_j is max, all a_i <a_j, a_k <a_jleft_less = []for ai in left:if ai <a_j:left_less.append(ai)right_less = []for ak in right:if ak <a_j:right_less.append(ak)same_pairs =0cnt_left = {}for ai in left_less:cnt_left[ai] = cnt_left.get(ai,0) +1cnt_right = {}for ak in right_less:cnt_right[ak] = cnt_right.get(ak,0) +1for ai in cnt_left:if ai in cnt_right:same_pairs += cnt_left[ai] * cnt_right[ai]max_case = len(left_less) * len(right_less) - same_pairs# Case 2: a_j is min, all a_i > a_j, a_k > a_jleft_greater = []for ai in left:if ai >a_j:left_greater.append(ai)right_greater = []for ak in right:if ak >a_j:right_greater.append(ak)same_pairs_min =0cnt_left_min = {}for ai in left_greater:cnt_left_min[ai] = cnt_left_min.get(ai,0) +1cnt_right_min = {}for ak in right_greater:cnt_right_min[ak] = cnt_right_min.get(ak,0) +1for ai in cnt_left_min:if ai in cnt_right_min:same_pairs_min += cnt_left_min[ai] * cnt_right_min[ai]min_case = len(left_greater) * len(right_greater) - same_pairs_minres.append( (a_j, max_case + min_case) )# Collect all j's contributions (including j=0 and j=N-1 with 0)contributions = []for j in range(N):if j < len(res):contributions.append( res[j] )else:contributions.append( (a[j], 0) )# Sort the contributions by a_jcontributions_sorted = sorted(contributions, key=lambda x: x[0])a_sorted = [x[0] for x in contributions_sorted]prefix_sum = [0] * (len(contributions_sorted) +1)for i in range(len(contributions_sorted)):prefix_sum[i+1] = prefix_sum[i] + contributions_sorted[i][1]# Process each queryfor L, H in queries:left = bisect_left(a_sorted, L)right_idx = bisect_right(a_sorted, H)total = prefix_sum[right_idx] - prefix_sum[left]print(total)if __name__ == "__main__":main()