0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ABC470Dを解いた【逆置換】

0
Posted at

筆者はレート800前後の茶~緑コーダ

ABC470のD問題を解いていく

実装コード

置換 A とその逆置換 B を同時に管理し、2種類のクエリを高速に処理する。

  • N, Q と置換 A を0-indexedで受け取る
  • B[A[i]] = i として、A の逆置換 B を作る
  • クエリ1では、指定された位置 x, y の値を A 上で交換する
  • A の交換後に B[A[x]]B[A[y]] も交換し、逆置換の対応を保つ
  • クエリ2では AB を入れ替え、現在の置換をその逆置換にする
  • すべてのクエリを処理したら、A の各値を1-indexedに戻して出力する
main.py
from bisect import bisect_left, bisect_right, insort_left, insort_right
from collections import defaultdict, Counter, deque
from functools import reduce, lru_cache
from itertools import product, accumulate, groupby, combinations
import sys
import os
def rI(): return int(sys.stdin.readline().rstrip())
def rLI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def rI1(): return int(sys.stdin.readline().rstrip()) - 1
def rLI1(): return list(map(lambda a: int(a) - 1, sys.stdin.readline().rstrip().split()))
def rS(): return sys.stdin.readline().rstrip()
def rLS(): return list(sys.stdin.readline().rstrip().split())
IS_LOCAL = int(os.getenv("ATCODER", "0")) == 0
err = (lambda *args, **kwargs: print(*args, **kwargs, file=sys.stderr)) if IS_LOCAL else (lambda *args, **kwargs: None)

def main():
    N, Q = rLI()
    A = rLI1()

    B = [0] * N
    for i, a in enumerate(A):
        B[a] = i

    for _ in range(Q):
        query = rLI()
        if query[0] == 1:
            x, y = query[1] - 1, query[2] - 1
            A[x], A[y] = A[y], A[x]
            B[A[x]], B[A[y]] = B[A[y]], B[A[x]]
        else:
            A, B = B, A

    print(*[a + 1 for a in A])

if __name__ == '__main__':
    main()

感想

解説の置換と逆置換を両方持つことで、要素の交換後も対応関係を簡単に更新できるのが勉強になった。
AB の入れ替えだけで逆置換のクエリを処理できるのは覚えておきたい。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?