|
| 1 | +/** |
| 2 | + * @file 9327.cpp |
| 3 | + * @author Macesuted (i@macesuted.moe) |
| 4 | + * @date 2025-10-31 |
| 5 | + * |
| 6 | + * @copyright Copyright (c) 2025 |
| 7 | + * |
| 8 | + */ |
| 9 | + |
| 10 | +#include <bits/stdc++.h> |
| 11 | +using namespace std; |
| 12 | + |
| 13 | +#define endl '\n' |
| 14 | + |
| 15 | +#define maxn 100005 |
| 16 | + |
| 17 | +int a[maxn], p[maxn], sum[maxn], cnt[maxn]; |
| 18 | +map<int, int> S; |
| 19 | + |
| 20 | +void solve(void) { |
| 21 | + int n, q; |
| 22 | + cin >> n >> q; |
| 23 | + int B = sqrt(n); |
| 24 | + |
| 25 | + for (int i = 1; i <= n; i++) cin >> a[i], p[a[i]] = i; |
| 26 | + |
| 27 | + auto add = [&](int x, int y, int delt) -> void { |
| 28 | + int64_t v = abs(x - y) * abs(a[x] - a[y]); |
| 29 | + if (v > maxn) return; |
| 30 | + sum[v / B] += delt, cnt[v] += delt; |
| 31 | + return; |
| 32 | + }; |
| 33 | + auto getAns = [&](void) -> int { |
| 34 | + int ans = 0; |
| 35 | + while (!sum[ans / B]) ans += B; |
| 36 | + while (!cnt[ans]) ans++; |
| 37 | + return ans; |
| 38 | + }; |
| 39 | + |
| 40 | + for (int i = 1; i <= n; i++) |
| 41 | + for (int j = i + 1; j <= min(n, i + B); j++) add(i, j, +1), add(p[i], p[j], +1); |
| 42 | + |
| 43 | + cout << getAns() << endl; |
| 44 | + while (q--) { |
| 45 | + int x, y; |
| 46 | + cin >> x >> y; |
| 47 | + |
| 48 | + auto calc = [&](int delt) -> void { |
| 49 | + for (int i = max(1, x - B); i <= min(n, x + B); i++) |
| 50 | + if (i != x) add(x, i, delt); |
| 51 | + for (int i = max(1, y - B); i <= min(n, y + B); i++) |
| 52 | + if (i != x && i != y) add(y, i, delt); |
| 53 | + for (int i = max(1, a[x] - B); i <= min(n, a[x] + B); i++) |
| 54 | + if (i != a[x]) add(x, p[i], delt); |
| 55 | + for (int i = max(1, a[y] - B); i <= min(n, a[y] + B); i++) |
| 56 | + if (i != a[x] && i != a[y]) add(y, p[i], delt); |
| 57 | + return; |
| 58 | + }; |
| 59 | + |
| 60 | + calc(-1); |
| 61 | + swap(a[x], a[y]); |
| 62 | + swap(p[a[x]], p[a[y]]); |
| 63 | + calc(+1); |
| 64 | + |
| 65 | + cout << getAns() << endl; |
| 66 | + } |
| 67 | + return; |
| 68 | +} |
| 69 | + |
| 70 | +int main() { |
| 71 | + ios::sync_with_stdio(false), cin.tie(nullptr); |
| 72 | + |
| 73 | + int _ = 1; |
| 74 | + while (_--) solve(); |
| 75 | + |
| 76 | + return 0; |
| 77 | +} |
0 commit comments