목차

수열과 어렵지 않은 쿼리

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()