PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000027. Nghịch thế đoạn trực tuyến (Inversions query 3)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Nghịch thế đoạn trực tuyến (Inversions query 3)

Inversions query 3

Source: MarisaOJ

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a permutation a1,…,ana_1,\ldots,a_n of 1,…,n1,\ldots,n and qq online queries. Each query contains x,yx,y. Let lastans be the preceding answer, initially 00. Compute

$$l=((\mathrm{lastans}+x)\bmod n)+1,\qquad r=((\mathrm{lastans}+y)\bmod n)+1.$$

Swap l,rl,r when l>rl>r. Output the number of pairs l≤i<j≤rl\le i<j\le r with ai>aja_i>a_j, then update lastans. Query reordering is not valid because the decoded range depends on previous results.

Input

The first line contains n,qn,q, the next line contains the permutation, and the following qq lines each contain x,yx,y.

Output

Print one inversion count per query, in the original order.

Subtasks

  • Subtask 1 (10 points): 1≤n≤601\le n\le 60; 1≤q≤801\le q\le 80; The array is a permutation of 1,2,…,n1,2,\ldots,n; 1≤x,y≤1051\le x,y\le10^5. Inversion counts and additions involving lastans require 64-bit integers.
  • Subtask 2 (20 points): 1≤n≤20001\le n\le 2000; 1≤q≤20001\le q\le 2000; The array is a permutation of 1,2,…,n1,2,\ldots,n; 1≤x,y≤1051\le x,y\le10^5. Inversion counts and additions involving lastans require 64-bit integers.
  • Subtask 3 (30 points): 1≤n≤120001\le n\le 12000; 1≤q≤200001\le q\le 20000; The array is a permutation of 1,2,…,n1,2,\ldots,n; 1≤x,y≤1051\le x,y\le10^5. Inversion counts and additions involving lastans require 64-bit integers.
  • Subtask 4 (40 points): 1≤n≤1000001\le n\le 100000; 1≤q≤1000001\le q\le 100000; The array is a permutation of 1,2,…,n1,2,\ldots,n; 1≤x,y≤1051\le x,y\le10^5. Inversion counts and additions involving lastans require 64-bit integers.

Examples

Input

8 5
6 5 2 3 8 1 4 7
6 4
5 4
5 4
3 8
8 5

Output

2
0
1
2
5

Explanation

The first query uses lastans=0 and decodes to [5,7][5,7], which has two inversions. That answer is used to decode the second range, whose answer is zero. The remaining outputs are 1,2,51,2,5.