ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 31222 |
문제명 | 수열과 어렵지 않은 쿼리 |
레벨 | 플래티넘 5 |
분류 |
구간합 쿼리 |
시간복잡도 | O(n+qlogn) |
인풋사이즈 | n<=2*10^5, q<=2*10^5 |
사용한 언어 | Python 3.11 |
제출기록 | 51032KB / 864ms |
최고기록 | 864ms |
해결날짜 | 2024/01/08 |
"""Solution code for "BOJ 31222. 수열과 어렵지 않은 쿼리".
- Problem link: https://www.acmicpc.net/problem/31222
- Solution link: http://www.teferi.net/ps/problems/boj/31222
Tags: [fenwick tree]
"""
import itertools
import sys
from teflib import fenwicktree
def main():
n, q = [int(x) for x in sys.stdin.readline().split()]
A = sys.stdin.readline().split()
diff = [0] + [0 if x == y else 1 for x, y in itertools.pairwise(A)]
fenwick = fenwicktree.FenwickTree(diff)
for _ in range(q):
match sys.stdin.readline().split():
case '1', i, x:
i = int(i) - 1
A[i] = x
if i > 0:
fenwick.set(i, 1 if A[i] != A[i - 1] else 0)
if i < n - 1:
fenwick.set(i + 1, 1 if A[i] != A[i + 1] else 0)
case '2', l, r:
l, r = int(l) - 1, int(r) - 1
print(fenwick.query(l + 1, r + 1) + 1)
if __name__ == '__main__':
main()