提出 #70612858


ソースコード 拡げる

import sys
import bisect

input = sys.stdin.readline

class FenwickTree:
    def __init__(self, n):
        self._n = n
        self.data = [0] * n

    def add(self, k, v):
        k += 1
        while k <= self._n:
            self.data[k - 1] += v
            k += k & -k

    def query(self, k):
        k += 1
        s = 0
        while k > 0:
            s += self.data[k - 1]
            k -= k & -k
        return s


N, A, B = map(int, input().split())
S = input().strip()

count_a = [0] * (N + 1)
for i in range(N):
    count_a[i + 1] = count_a[i] + (1 if S[i] == 'a' else 0)

X = count_a
Y = [count_a[i] - i for i in range(N + 1)]


sorted_Y = sorted(list(set(Y)))
M = len(sorted_Y) 
events = []

for j in range(N + 1):
    x_j = X[j]
    y_j = Y[j]
    y_idx = bisect.bisect_left(sorted_Y, y_j)
    events.append((x_j, j, 1, y_idx))

for r in range(1, N + 1):
    x_q = X[r] - A
    y_q_val = Y[r] + B - 1
    
    y_q_idx = bisect.bisect_right(sorted_Y, y_q_val) - 1
    
    events.append((x_q, r, 0, y_q_idx))


events.sort()

#平面走査 (BIT を使って処理)
bit = FenwickTree(M)
ans = 0

for x, t, type, y_idx in events:
    if type == 1:

        bit.add(y_idx, 1)
    else:
        if y_idx >= 0:
            #区間 [0, y_q_idx] の和を求める
            ans += bit.query(y_idx)

print(ans)

提出情報

提出日時
問題 C - Truck Driver
ユーザ exophia
言語 Python (CPython 3.13.7)
得点 300
コード長 1396 Byte
結果 AC
実行時間 954 ms
メモリ 138572 KiB

ジャッジ結果

セット名 Sample All
得点 / 配点 0 / 0 300 / 300
結果
AC × 2
AC × 25
セット名 テストケース
Sample sample_01.txt, sample_02.txt
All hand.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, sample_01.txt, sample_02.txt
ケース名 結果 実行時間 メモリ
hand.txt AC 11 ms 9496 KiB
random_01.txt AC 476 ms 110692 KiB
random_02.txt AC 465 ms 108764 KiB
random_03.txt AC 472 ms 110284 KiB
random_04.txt AC 469 ms 109204 KiB
random_05.txt AC 480 ms 112372 KiB
random_06.txt AC 489 ms 112020 KiB
random_07.txt AC 476 ms 107608 KiB
random_08.txt AC 466 ms 109268 KiB
random_09.txt AC 877 ms 135988 KiB
random_10.txt AC 887 ms 136024 KiB
random_11.txt AC 875 ms 135932 KiB
random_12.txt AC 896 ms 135968 KiB
random_13.txt AC 933 ms 135932 KiB
random_14.txt AC 892 ms 135912 KiB
random_15.txt AC 896 ms 135900 KiB
random_16.txt AC 954 ms 135988 KiB
random_17.txt AC 944 ms 136928 KiB
random_18.txt AC 940 ms 138572 KiB
random_19.txt AC 382 ms 106460 KiB
random_20.txt AC 906 ms 127300 KiB
random_21.txt AC 407 ms 106364 KiB
random_22.txt AC 909 ms 117836 KiB
sample_01.txt AC 11 ms 9480 KiB
sample_02.txt AC 12 ms 9460 KiB