题目:http://codeforces.com/problemset/problem/631/C
题意:给定序列和操作,操作(x, y)表示对前y个进行非增或非减排序,求最后得到的序列。
思路:对一个小的y,将会被后面的大y覆盖,所以将操作处理成y递减的形式。每次操作之后后面的数字不再改变,数字从后往前放即可。
代码:
#include <bits/stdc++.h>
#define fi first
#define se second
using namespace std;
typedef pair<int, int> P;
const int maxn = 2e5+5;
const int inf = 0x3f3f3f3f;
int n, m, a[maxn], ans[maxn];
P p[maxn];
priority_queue<int>que1; //Big
priority_queue<int, vector<int>, greater<int> >que2; //Small
int main()
{
cin >> n >> m;
for(int i=1; i<=n; i++){cin >> a[i];}
int x, y, top = 0;
for(int i=1; i<=m; i++) {
cin >> x >> y;
if(i==1) p[++top].fi = x, p[top].se = y;
else {
while(top && y >= p[top].se) top --;
p[++top].fi = x, p[top].se = y;
}
}
int pos = n;
while(pos && pos > p[1].se) ans[pos] = a[pos], pos--;
for(int i=1; i<=pos; i++) que1.push(a[i]), que2.push(a[i]);
p[top+1].fi = 1; p[top+1].se = 0;
for(int i=2; i<=top+1; i++){
if(p[i-1].fi == 1) while(pos && pos>p[i].se) ans[pos] = que1.top(), que1.pop(), pos --;
else while(pos && pos>p[i].se) ans[pos] = que2.top(), que2.pop(), pos --;
}
for(int i=1; i<=n; i++) printf("%d%c", ans[i], " \n"[i==n]);
}