1 条题解
-
0
Guest
-
0
#include<bits/stdc++.h> #define io(x); freopen(x".in", "r", stdin), freopen(x".out", "w", stdout); using namespace std; const int N = 8010; int n, m; int a[N], b[N], c[N]; bool cmp(int x, int y) { if (a[x] != a[y]) return a[x] < a[y]; return x < y; } void upd(int k) { while (k > 1 && (a[b[k - 1]] > a[b[k]] || a[b[k - 1]] == a[b[k]] && b[k - 1] > b[k])) { swap(b[k - 1], b[k]); swap(c[b[k - 1]], c[b[k]]); k -- ; } while (k < n && (a[b[k + 1]] < a[b[k]] || a[b[k + 1]] == a[b[k]] && b[k + 1] < b[k])) { swap(b[k + 1], b[k]); swap(c[b[k + 1]], c[b[k]]); k ++ ; } } int main() { io("sort"); cin >> n >> m; for (int i = 1; i <= n; i ++ ) { cin >> a[i]; b[i] = i; // 存a[i]的下标 } sort(b + 1, b + n + 1, cmp); for (int i = 1; i <= n; i ++ ) c[b[i]] = i; // 存原数组第 k 个位置的数在排完序后的位置 while (m -- ) { int op, x, v; cin >> op >> x; if (op == 1) { cin >> v; a[x] = v; // 修改 int k = c[x]; // 获取排完序后的位置 upd(k); } else cout << c[x] << endl; } return 0; }
- 1
信息
- ID
- 42
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 8
- 标签
- (无)
- 递交数
- 71
- 已通过
- 12
- 上传者