Algorithm/insert-sort/main.cc

38 lines
941 B
C++
Raw Normal View History

2024-10-10 13:15:37 +00:00
#include <iostream>
using namespace std;
const int N = 10005;
2024-10-10 13:15:37 +00:00
pair<int, int> arr[N];
2024-10-22 14:03:21 +00:00
int beta[N];
int main() {
int n, m;
scanf("%d%d", &n, &m);
2024-10-10 13:15:37 +00:00
for (int i = 1; i <= n; i++) {
scanf("%d", &arr[i].first);
arr[i].second = i;
2024-10-10 13:15:37 +00:00
}
sort(arr + 1, arr + n + 1);
2024-10-22 14:03:21 +00:00
for (int i = 1; i <= n; i++) beta[arr[i].second] = i;
while (m--) {
int op, x, y;
scanf("%d", &op);
2024-10-10 13:15:37 +00:00
if (op == 1) {
scanf("%d%d", &x, &y);
2024-10-22 14:03:21 +00:00
arr[beta[x]].first = y;
for (int i = 1; i < n; i++) {
if (arr[i] > arr[i + 1]) swap(arr[i], arr[i + 1]);
2024-10-10 13:15:37 +00:00
}
for (int i = n - 1; i >= 1; i--) {
if (arr[i] > arr[i + 1]) swap(arr[i], arr[i + 1]);
2024-10-10 13:15:37 +00:00
}
2024-10-22 14:03:21 +00:00
for (int i = 1; i <= n; i++) beta[arr[i].second] = i;
} else {
scanf("%d", &x);
2024-10-22 14:03:21 +00:00
printf("%d\n", beta[x]);
2024-10-10 13:15:37 +00:00
}
}
return 0;
2024-10-10 13:15:37 +00:00
}