d1 = node[Stack[0]].r ,d2 = node[Stack[1]].r
首先判断d1 >d 2, 那么[d2+1, d1]中的元素可以确定,且以后操作中不再变化.若node[Stack[0]].t == 1,那么[d2+1, d1]中元素可以为数组p中[r - (d1-d2)+1, r]中的元素, 更新r = node[Stack[1]].r,以此类推
#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
typedef long long ll;
struct Node{
int t, r;
}node[maxn];
int num[maxn], p[maxn], Stack[maxn];
int ans[maxn];
int main(){
// freopen("in.txt", "r", stdin);
int n, m;
scanf("%d%d", &n, &m);
for(int i = 0; i < n; i++){
scanf("%d", num+i);
p[i] = num[i];
ans[i] = num[i];
}
int r = 0;
for(int j = 0; j < m; j++){
scanf("%d%d", &node[j].t, &node[j].r);
node[j].r--;
while(r != 0 && node[Stack[r-1]].r <= node[j].r)
r--;
if(r == 0){
Stack[r++] = j;
}
else{
if(node[Stack[r-1]].t != node[j].t)
Stack[r++] = j;
}
}
sort(p, p+node[Stack[0]].r+1);
int k1 = 0, k2 = node[Stack[0]].r;
int v = k2;
for(int i = 0; i < r; i++){
int d;
if(i + 1 < r)
d = node[Stack[i]].r - node[Stack[i+1]].r;
else
d = node[Stack[i]].r + 1;
while(d--){
if(node[Stack[i]].t == 1)
ans[v--] = p[k2--];
else
ans[v--] = p[k1++];
}
}
printf("%d", ans[0]);
for(int i = 1; i < n; i++)
printf(" %d", ans[i]);
puts("");
return 0;
}