結果

問題 No.2338 Range AtCoder Query
ユーザー ForestedForested
提出日時 2022-08-23 17:08:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,029 ms / 4,000 ms
コード長 2,859 bytes
コンパイル時間 377 ms
コンパイル使用メモリ 82,292 KB
実行使用メモリ 194,896 KB
最終ジャッジ日時 2024-04-15 01:50:58
合計ジャッジ時間 25,826 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
53,692 KB
testcase_01 AC 39 ms
53,472 KB
testcase_02 AC 39 ms
53,652 KB
testcase_03 AC 41 ms
53,488 KB
testcase_04 AC 40 ms
53,380 KB
testcase_05 AC 39 ms
53,596 KB
testcase_06 AC 99 ms
77,484 KB
testcase_07 AC 95 ms
77,376 KB
testcase_08 AC 95 ms
77,316 KB
testcase_09 AC 94 ms
76,944 KB
testcase_10 AC 96 ms
77,200 KB
testcase_11 AC 835 ms
176,484 KB
testcase_12 AC 792 ms
171,728 KB
testcase_13 AC 862 ms
179,108 KB
testcase_14 AC 812 ms
179,428 KB
testcase_15 AC 927 ms
187,080 KB
testcase_16 AC 1,024 ms
192,520 KB
testcase_17 AC 1,007 ms
192,156 KB
testcase_18 AC 996 ms
191,796 KB
testcase_19 AC 1,017 ms
192,120 KB
testcase_20 AC 1,003 ms
192,288 KB
testcase_21 AC 1,012 ms
192,472 KB
testcase_22 AC 1,010 ms
192,096 KB
testcase_23 AC 1,026 ms
192,632 KB
testcase_24 AC 1,014 ms
192,060 KB
testcase_25 AC 1,012 ms
192,536 KB
testcase_26 AC 461 ms
160,708 KB
testcase_27 AC 468 ms
161,292 KB
testcase_28 AC 459 ms
161,164 KB
testcase_29 AC 901 ms
190,220 KB
testcase_30 AC 1,029 ms
194,896 KB
testcase_31 AC 902 ms
190,672 KB
testcase_32 AC 872 ms
191,596 KB
testcase_33 AC 724 ms
180,952 KB
testcase_34 AC 728 ms
181,236 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