結果

問題 No.2338 Range AtCoder Query
ユーザー ForestedForested
提出日時 2022-08-23 17:08:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 899 ms / 4,000 ms
コード長 2,859 bytes
コンパイル時間 154 ms
コンパイル使用メモリ 82,848 KB
実行使用メモリ 194,896 KB
最終ジャッジ日時 2024-10-04 09:34:34
合計ジャッジ時間 22,739 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
55,208 KB
testcase_01 AC 34 ms
54,376 KB
testcase_02 AC 36 ms
54,424 KB
testcase_03 AC 36 ms
53,164 KB
testcase_04 AC 35 ms
52,752 KB
testcase_05 AC 36 ms
53,912 KB
testcase_06 AC 84 ms
77,036 KB
testcase_07 AC 85 ms
76,892 KB
testcase_08 AC 83 ms
77,008 KB
testcase_09 AC 81 ms
77,040 KB
testcase_10 AC 86 ms
77,104 KB
testcase_11 AC 739 ms
176,224 KB
testcase_12 AC 682 ms
171,620 KB
testcase_13 AC 739 ms
179,256 KB
testcase_14 AC 695 ms
179,296 KB
testcase_15 AC 791 ms
187,076 KB
testcase_16 AC 872 ms
192,588 KB
testcase_17 AC 882 ms
192,296 KB
testcase_18 AC 872 ms
191,808 KB
testcase_19 AC 887 ms
192,500 KB
testcase_20 AC 877 ms
192,584 KB
testcase_21 AC 877 ms
192,600 KB
testcase_22 AC 866 ms
192,540 KB
testcase_23 AC 899 ms
192,504 KB
testcase_24 AC 877 ms
192,448 KB
testcase_25 AC 877 ms
192,544 KB
testcase_26 AC 422 ms
161,100 KB
testcase_27 AC 427 ms
161,044 KB
testcase_28 AC 425 ms
161,092 KB
testcase_29 AC 759 ms
190,332 KB
testcase_30 AC 864 ms
194,896 KB
testcase_31 AC 779 ms
190,848 KB
testcase_32 AC 745 ms
191,464 KB
testcase_33 AC 617 ms
181,148 KB
testcase_34 AC 612 ms
181,228 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

# https://github.com/shakayami/ACL-for-python/blob/master/fenwicktree.py
class fenwick_tree():
    n=1
    data=[0 for i in range(n)]
    def __init__(self,N):
        self.n=N
        self.data=[0 for i in range(N)]
    def add(self,p,x):
        assert 0<=p<self.n,"0<=p<n,p={0},n={1}".format(p,self.n)
        p+=1
        while(p<=self.n):
            self.data[p-1]+=x
            p+=p& -p
    def sum(self,l,r):
        assert (0<=l and l<=r and r<=self.n),"0<=l<=r<=n,l={0},r={1},n={2}".format(l,r,self.n)
        return self.sum0(r)-self.sum0(l)
    def sum0(self,r):
        s=0
        while(r>0):
            s+=self.data[r-1]
            r-=r&-r
        return s

class OfflineRectangleAddPointGet:
    def __init__(self, n):
        self.n = n
        self.add = [[] for _ in range(n + 1)]
        self.sub = [[] for _ in range(n + 1)]
        self.q = 0
        self.queries = [[] for _ in range(n)]
    
    def add_rect(self, xl, xr, yl, yr):
        self.add[xl].append((yl, yr))
        self.sub[xr].append((yl, yr))
    
    def add_query(self, x, y):
        self.queries[x].append((y, self.q))
        self.q += 1
    
    def solve(self):
        ret = [0 for _ in range(self.q)]
        fw = fenwick_tree(self.n + 1)
        for x in range(self.n):
            for (l, r) in self.add[x]:
                fw.add(l, 1)
                fw.add(r, -1)
            for (l, r) in self.sub[x]:
                fw.add(l, -1)
                fw.add(r, 1)
            for (y, qi) in self.queries[x]:
                ret[qi] = fw.sum(0, y + 1)
        return ret

def main():
    import sys
    input = sys.stdin.readline
    
    n, m, q = map(int, input().split())
    p = [0 for _ in range(n)]
    is_ac = [False for _ in range(n)]
    for i in range(n):
        pi, si = input().split()
        p[i] = int(pi) - 1
        is_ac[i] = si == "AC"
    l = [0 for _ in range(q)]
    r = [0 for _ in range(q)]
    for i in range(q):
        li, ri = map(int, input().split())
        l[i] = li - 1
        r[i] = ri
    
    prev_ac = [-1 for _ in range(n)]
    next_ac = [n for _ in range(n)]
    pv = [-1 for _ in range(m)]
    for i in range(n):
        prev_ac[i] = pv[p[i]]
        if is_ac[i]:
            pv[p[i]] = i
    nt = [n for _ in range(m)]
    for i in reversed(range(n)):
        next_ac[i] = nt[p[i]]
        if is_ac[i]:
            nt[p[i]] = i
    
    ac = OfflineRectangleAddPointGet(n + 1)
    pena = OfflineRectangleAddPointGet(n + 1)
    for i in range(n):
        if is_ac[i]:
            ac.add_rect(prev_ac[i] + 1, i + 1, i + 1, n + 1)
        else:
            pena.add_rect(prev_ac[i] + 1, i + 1, next_ac[i] + 1, n + 1)
    for i in range(q):
        ac.add_query(l[i], r[i])
        pena.add_query(l[i], r[i])
    acc = ac.solve()
    penac = pena.solve()
    for i in range(q):
        print(acc[i], penac[i])

main()
0