Skip to content

Commit cd6c2a4

Browse files
committed
🥺chore
1 parent 17d319c commit cd6c2a4

File tree

1 file changed

+42
-34
lines changed

1 file changed

+42
-34
lines changed

abc174/f/main.py

Lines changed: 42 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,48 +29,56 @@ def LR(n): return [L() for _ in range(n)]
2929
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
3030
MOD = 1000000007
3131

32-
# reffer to: https://qiita.com/dn6049949/items/afa12d5d079f518de368
33-
class SegmentTree:
34-
# 初期化処理
35-
# f : SegmentTreeにのせるモノイド
36-
# default : fに対する単位元
37-
def __init__(self, size, f=lambda x,y : x+y, default=0):
38-
self.size = 2**(size-1).bit_length() # 簡単のため要素数Nを2冪にする
39-
self.default = default
40-
self.data = [default for _ in range(self.size * 2)] # 要素を単位元で初期化
41-
self.f = f
42-
43-
def update(self, i, x):
44-
i += self.size
45-
self.data[i] = x
32+
# 0-indexed BIT
33+
class BIT:
34+
def __init__(self, li):
35+
self.n, self.data = len(li) + 1, [0] + li
36+
for i in range(1, self.n):
37+
if i + (i & -i) < self.n: self.data[i + (i & -i)] += self.data[i]
38+
39+
def add(self, i, a):
40+
i += 1
41+
while i < self.n:
42+
self.data[i] += a
43+
i += i & -i
44+
45+
# sum of [0, i)
46+
def acc(self, i):
47+
res = 0
4648
while i > 0:
47-
i >>= 1
48-
self.data[i] = self.f(self.data[i*2], self.data[i*2+1])
49-
50-
def query(self, l, r):
51-
l += self.size
52-
r += self.size
53-
lres, rres = self.default, self.default
54-
while l < r:
55-
if l & 1:
56-
lres = self.f(lres, self.data[l])
57-
l += 1
58-
59-
if r & 1:
60-
r -= 1
61-
rres = self.f(self.data[r], rres) # モノイドでは可換律は保証されていないので演算の方向に注意
62-
l >>= 1
63-
r >>= 1
64-
res = self.f(lres, rres)
49+
res += self.data[i]
50+
i -= i & -i
6551
return res
6652

53+
# sum of [l, r)
54+
def get(self, l, r = None):
55+
if r is None: r = l+1
56+
return self.acc(r) - self.acc(l)
57+
6758

6859
def main():
6960
N, Q = LI()
7061
C = LI()
71-
lr = sorted(enumerate(LIR1(Q)), key=lambda x: x[1][1])
72-
print(lr)
62+
lr = sorted(enumerate(LIR(Q)), key=lambda x: x[1][1])
63+
rightmostIndex = [-1]*(N+1)
64+
tree = BIT([0]*N)
65+
left, right = 0, 0
66+
ans = [None] * Q
67+
68+
for i, (l, r) in lr:
69+
while right < r:
70+
if rightmostIndex[C[right]] < 0:
71+
rightmostIndex[C[right]] = right
72+
else:
73+
tree.add(rightmostIndex[C[right]], -1)
74+
rightmostIndex[C[right]] = right
75+
tree.add(right, 1)
76+
right += 1
77+
78+
ans[i] = tree.get(l-1, r)
79+
7380

81+
print(*ans, sep="\n")
7482

7583

7684
if __name__ == '__main__':

0 commit comments

Comments
 (0)