-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
class RSQFenwick: | ||
def __init__(self, N): | ||
self.N = N | ||
self.f = [0] * self.N | ||
|
||
def query(self, i): | ||
res = 0 | ||
while i >= 0: | ||
res += self.f[i] | ||
i -= ~i & (i + 1) | ||
return res | ||
|
||
def update(self, i, delta): | ||
while i < self.N: | ||
self.f[i] += delta | ||
i += ~i & (i + 1) | ||
|
||
def rsq(self, left, right): | ||
return self.query(right - 1) - self.query(left - 1) | ||
|
||
|
||
if __name__ == '__main__': | ||
N = int(input()) | ||
s = RSQFenwick(N) | ||
while True: | ||
a, b, c = input().split() | ||
if a == '+': | ||
s.update(int(b), int(c)) | ||
elif a == '?': | ||
print(s.rsq(int(b), int(c))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
class RSQPrefixSum: | ||
def __init__(self, a): | ||
self.s = [0] | ||
for v in a: | ||
self.s.append(self.s[-1] + v) | ||
|
||
def rsq(self, l, r): | ||
return self.s[r] - self.s[l] | ||
|
||
|
||
if __name__ == '__main__': | ||
a = list(map(int, input().split())) | ||
s = RSQPrefixSum(a) | ||
while True: | ||
l, r = map(int, input().split()) | ||
print(s.rsq(l, r)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
from math import log2, floor, ceil | ||
|
||
|
||
class RSQSegmentTree: | ||
neutral_value = 0 | ||
|
||
def __init__(self, a): | ||
self.N = 2 ** int(ceil(log2(len(a)))) | ||
self.s = [None] * (self.N - 1) + list(a) + ([self.neutral_value] * (self.N - len(a))) | ||
for i in range(self.N - 2, -1, -1): | ||
self.refresh_s(i) | ||
|
||
def refresh_s(self, i): | ||
self.s[i] = self.s[2 * i + 1] + self.s[2 * i + 2] | ||
|
||
def rsq_i(self, l, r, i, li, ri): | ||
if (r <= li) or (ri <= l): | ||
return self.neutral_value | ||
if (l <= li) and (ri <= r): | ||
return self.s[i] | ||
middle = li + (ri - li) // 2 | ||
return (self.rsq_i(l, r, i * 2 + 1, li, middle) + | ||
self.rsq_i(l, r, i * 2 + 2, middle, ri)) | ||
|
||
def update(self, i, v): | ||
i += self.N - 1 | ||
self.s[i] = v | ||
while i > 0: | ||
i = (i - 1) // 2 | ||
self.refresh_s(i) | ||
|
||
def rsq(self, l, r): | ||
return self.rsq_i(l, r, 0, 0, self.N) | ||
|
||
|
||
if __name__ == '__main__': | ||
a = list(map(int, input().split())) | ||
#N = int(input()) | ||
s = RSQSegmentTree(a) | ||
while True: | ||
a, b, c = input().split() | ||
if a == '+': | ||
s.update(int(b), int(c)) | ||
elif a == '?': | ||
print(s.rsq(int(b), int(c))) | ||
elif a == '!': | ||
print(' '.join(map(str, s.a))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from math import log2, floor, ceil | ||
|
||
inf = 10**9 | ||
|
||
|
||
class RMQSparseTable: | ||
def __init__(self, a): | ||
N = len(a) | ||
K = ceil(log2(N)) + 1 | ||
|
||
self.ST = [[None] * N for k in range(K)] | ||
self.ST[0] = list(a) | ||
|
||
for k in range(1, K): | ||
for i in range(N): | ||
if i + 2 ** (k - 1) < N: | ||
self.ST[k][i] = min(self.ST[k - 1][i], | ||
self.ST[k - 1][i + 2 ** (k - 1)]) | ||
else: | ||
self.ST[k][i] = self.ST[k - 1][i] | ||
for k in range(K): | ||
print(' '.join(map(str, self.ST[k]))) | ||
|
||
def rmq(self, l, r): | ||
if l == r: | ||
return inf | ||
k = floor(log2(r - l)) | ||
return min(self.ST[k][l], | ||
self.ST[k][r - 2 ** k]) | ||
|
||
|
||
if __name__ == '__main__': | ||
a = list(map(int, input().split())) | ||
s = RMQSparseTable(a) | ||
while True: | ||
l, r = map(int, input().split()) | ||
print(s.rmq(l, r)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
from math import sqrt, floor, ceil | ||
|
||
|
||
class RSQSqrtDecomposition: | ||
def __init__(self, a): | ||
N = len(a) | ||
self.k = max(int(ceil(sqrt(N))), 1) | ||
self.a = a | ||
self.b = [0] * (self.k + 1) | ||
|
||
for i, v in enumerate(a): | ||
self.b[i // self.k] += v | ||
|
||
def rsq(self, l, r): | ||
l_block = self.k * int(ceil(l / self.k)) | ||
r_block = self.k * int(floor(r / self.k)) | ||
if l_block > r_block: | ||
return sum(self.a[l:r]) | ||
return (sum(self.a[l:l_block]) + | ||
sum(self.b[l_block // self.k:r_block // self.k]) + | ||
sum(self.a[r_block:r])) | ||
|
||
def update(self, i, v): | ||
delta = v - self.a[i] | ||
self.a[i] = v | ||
self.b[i // self.k] += delta | ||
|
||
|
||
if __name__ == '__main__': | ||
a = list(map(int, input().split())) | ||
s = RSQSqrtDecomposition(a) | ||
while True: | ||
a, b, c = input().split() | ||
if a == '+': | ||
s.update(int(b), int(c)) | ||
elif a == '?': | ||
print(s.rsq(int(b), int(c))) | ||
elif a == '!': | ||
print(' '.join(map(str, s.a))) |