-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCollecting_Numbers_II.cpp
89 lines (87 loc) · 2.23 KB
/
Collecting_Numbers_II.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#include <iostream>
#include <vector>
#include <unistd.h>
#include <unistd.h>
#include <algorithm>
#include <map>
#include <cmath>
#include <cstring>
#include <sstream>
#include <set>
#include <unordered_set>
#include <cstring>
#include <string.h>
#include <queue>
using namespace std;
#define mid(s, e) (s + (e - s) / 2)
#define ALL(x) (x).begin(), (x).end()
#define endl '\n'
#define MAXE(vec) *max_element(ALL(vec))
#define MINE(vec) *min_element(ALL(vec))
#define valid(a, b, c) (a >= b && a < c ? 1 : 0)
int dx[4] = {0, 0, 1, -1};
int dy[4] = {1, -1, 0, 0};
#define ll long long
void solve()
{
ll n, m, x, y;
cin >> n >> m;
vector<ll> numbers(n + 1);
map<ll, ll> position;
for (int i = 1; i <= n; i++)
{
cin >> numbers[i];
position[numbers[i]] = i;
}
ll ans = 1;
for (int i = 2; i <= n; i++)
{
if (position[i] < position[i - 1])
ans++;
}
for (int i = 0; i < m; i++)
{
vector<pair<ll, ll>> inversion(4);
cin >> x >> y;
if (numbers[x] != n)
inversion[0] = {numbers[x], numbers[x] + 1};
if (numbers[x] != 1)
inversion[1] = {numbers[x] - 1, numbers[x]};
if (numbers[y] != n)
inversion[2] = {numbers[y], numbers[y] + 1};
if (numbers[y] != 1)
inversion[3] = {numbers[y] - 1, numbers[y]};
sort(ALL(inversion));
for(int i = 1; i < 4; i++){
if(inversion[i].first == inversion[i - 1].first){
inversion[i] = {0, 0};
}
}
for(int i =0 ; i < 4;i++)
{
if(inversion[i].first != 0 && inversion[i].second != 0)
{
ans-= position[inversion[i].second] < position[inversion[i].first];
}
}
swap(numbers[x], numbers[y]);
position[numbers[x]] = x;
position[numbers[y]] = y;
for(int i =0 ; i < 4;i++)
{
if(inversion[i].first != 0 && inversion[i].second != 0)
{
ans+= position[inversion[i].second] < position[inversion[i].first];
}
}
cout << ans << endl;
}
}
int main(void)
{
ll t;
t = 1;
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}