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

Bài tập thuộc lộ trình

2 lộ trình

Anton và hoán vị (Anton and Permutation)

Anton and Permutation

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Initially a=[1,2,…,n]a=[1,2,\ldots,n] is the identity permutation. Each of qq operations swaps positions ll and rr; the positions may coincide. After every swap, output the number of inversions, i.e. pairs (i,j)(i,j) with 1≤i<j≤n1\le i<j\le n and ai>aja_i>a_j.

Input

The first line contains n,qn,q, followed by qq lines with positions l,rl,r. There is no initial-array line: the initial permutation is always 1,2,…,n1,2,\ldots,n.

Output

Print the inversion count after each swap, one per line.

Subtasks

  • Subtask 1 (10 points): 1≤n≤601\le n\le 60; 1≤q≤801\le q\le 80; 1≤l,r≤n1\le l,r\le n; l=rl=r is allowed. Inversion counts may require 64-bit integers.
  • Subtask 2 (20 points): 1≤n≤20001\le n\le 2000; 1≤q≤20001\le q\le 2000; 1≤l,r≤n1\le l,r\le n; l=rl=r is allowed. Inversion counts may require 64-bit integers.
  • Subtask 3 (30 points): 1≤n≤200001\le n\le 20000; 1≤q≤120001\le q\le 12000; 1≤l,r≤n1\le l,r\le n; l=rl=r is allowed. Inversion counts may require 64-bit integers.
  • Subtask 4 (40 points): 1≤n≤2000001\le n\le 200000; 1≤q≤500001\le q\le 50000; 1≤l,r≤n1\le l,r\le n; l=rl=r 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 [1,2,3,4,5][1,2,3,4,5], swapping positions 44 and 55 gives one inversion. Swapping 22 and 44 next yields [1,5,3,2,4][1,5,3,2,4] with four. Swapping 22 and 55 leaves three; swapping position 22 with itself changes nothing.