排序+构造+预处理
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; const int maxn = 200000 + 10; int n, m; int a[maxn], ans[maxn]; int op[maxn], e[maxn]; int pos[maxn]; int first, last; int flag; int p; int tot; bool cmp(const int &a, const int &b) { return a>b; } int main() { while (~scanf("%d%d", &n, &m)) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); for (int i = 1; i <= m; i++) scanf("%d%d", &op[i], &e[i]); memset(ans, 0, sizeof ans); tot = 0; memset(pos, -1, sizeof pos); pos[m] = m; for (int i = m - 1; i >= 1; i--) { pos[i] = pos[i + 1]; if (e[i] > e[pos[i + 1]]) pos[i] = i; } p = pos[1]; if (op[p] == 1){sort(a + 1, a + 1 + e[p]); flag = 1;} else{sort(a + 1, a + 1 + e[p], cmp);flag = 2;} first = 1; last = e[p]; for (int i = e[p] + 1; i <= n; i++) ans[i] = a[i], tot = tot + 1; while (tot<n) { p = pos[p + 1]; if (p == -1) break; if (first>last) { for (int i = last; i <= first - e[p]; i++) ans[n - tot] = a[i], tot = tot + 1; last = first - e[p]+1; } else { for (int i = last; i >= first + e[p]; i--) ans[n - tot] = a[i], tot = tot + 1; last = first + e[p] - 1; } if (op[p] != flag) { swap(first, last); flag = op[p]; } } if (tot < n) { if (first <= last) for (int i = last; i >= first; i--) ans[n - tot] = a[i], tot = tot + 1; else for (int i = last; i <= first; i++) ans[n - tot] = a[i], tot = tot + 1; } for (int i = 1; i <= n; i++) printf("%d ", ans[i]); printf(" "); } return 0; }