Блог пользователя Lemur95

Автор Lemur95, история, 6 лет назад, По-английски

Hello again Codeforces community!

I'm back with another problem, which I'm not able to solve. I don't have any link, it isn't from any site.

You are given a permutation of size N and Q queries. In 1 query, you are given x and y, and you need to swap the values on position x and y (formally, you need to swap v[x] and v[y]). After every query, you have to print the number of prefixes of the permutation, that are permutations.

For example, if you have array A = [2, 1, 3, 4], the answer is 3 (permutation at index 2, 3 and 4).

Example:

Input:
4 3
1 4 3 2
2 3
2 4 1 2

Output:

2 3 2

1 <= N, Q <= 100000.

The only approach I could come up with was brute force. Could anyone help me?

  • Проголосовать: нравится
  • +2
  • Проголосовать: не нравится

»
6 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

What is input format? It’s not clear from the example.

»
6 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится
  • i think you have to use range sum queries
  • assuming x<y, when you swap v[x] and v[y], only validity at indecies x,x+1,...,y-1 will change
  • at index n, it will be valid only if prefix_sum(1, n) == n*(n+1)/2 so all numbers from 1 to n exists
  • let ans[i] = prefix_sum(1, i) — (i*(i+1)/2), this means that i is valid only if ans[i] == 0
  • i didn't reach a full solution, but what left is how to count number of zeros in ans after performing range sum query which make prefix_sum valid after every swap