CodeForces 631C Print Check

时间:2022-05-22 14:41:21

排序+构造+预处理

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const int maxn = + ;
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 = ; i <= n; i++) scanf("%d", &a[i]);
for (int i = ; i <= m; i++) scanf("%d%d", &op[i], &e[i]);
memset(ans, , sizeof ans);
tot = ; memset(pos, -, sizeof pos);
pos[m] = m;
for (int i = m - ; i >= ; i--)
{
pos[i] = pos[i + ];
if (e[i] > e[pos[i + ]]) pos[i] = i;
} p = pos[];
if (op[p] == ){sort(a + , a + + e[p]); flag = ;}
else{sort(a + , a + + e[p], cmp);flag = ;}
first = ; last = e[p];
for (int i = e[p] + ; i <= n; i++) ans[i] = a[i], tot = tot + ;
while (tot<n)
{
p = pos[p + ]; if (p == -) break;
if (first>last)
{
for (int i = last; i <= first - e[p]; i++) ans[n - tot] = a[i], tot = tot + ;
last = first - e[p]+;
}
else
{
for (int i = last; i >= first + e[p]; i--) ans[n - tot] = a[i], tot = tot + ;
last = first + e[p] - ;
}
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 + ;
else for (int i = last; i <= first; i++) ans[n - tot] = a[i], tot = tot + ;
} for (int i = ; i <= n; i++) printf("%d ", ans[i]);
printf("\n");
}
return ;
}