PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN
#SRD0000025. Anton và hoán vị (Anton and Permutation)
HỌC THEO LỘ TRÌNH
2 lộ trìnhBài tập thuộc lộ trình
Mục lục đề ⌄
Đang đọc
Anton và hoán vị (Anton and Permutation)
Anton and Permutation
Source: Codeforces
Version: Phuoc Hung OJ Extended
Problem Statement
Initially is the identity permutation. Each of operations swaps positions and ; the positions may coincide. After every swap, output the number of inversions, i.e. pairs with and .
Input
The first line contains , followed by lines with positions . There is no initial-array line: the initial permutation is always .
Output
Print the inversion count after each swap, one per line.
Subtasks
- Subtask 1 (10 points): ; ; ; is allowed. Inversion counts may require 64-bit integers.
- Subtask 2 (20 points): ; ; ; is allowed. Inversion counts may require 64-bit integers.
- Subtask 3 (30 points): ; ; ; is allowed. Inversion counts may require 64-bit integers.
- Subtask 4 (40 points): ; ; ; is allowed. Inversion counts may require 64-bit integers.
Examples
Input
5 4
4 5
2 4
2 5
2 2
Output
1
4
3
3
Explanation
From , swapping positions and gives one inversion. Swapping and next yields with four. Swapping and leaves three; swapping position with itself changes nothing.