Report(单调栈+思维)

Report

Each month Blake gets the report containing main economic indicators of the company “Blake Technologies”. There are n commodities produced by the company. For each of them there is exactly one integer in the final report, that denotes corresponding revenue. Before the report gets to Blake, it passes through the hands of m managers. Each of them may reorder the elements in some order. Namely, thei-th manager either sorts first ri numbers in non-descending or non-ascending order and then passes the report to the manager i + 1, or directly to Blake (if this manager has number i = m).

Employees of the “Blake Technologies” are preparing the report right now. You know the initial sequence ai of length n and the description of each manager, that is value ri and his favourite order. You are asked to speed up the process and determine how the final report will look like.

Input

The first line of the input contains two integers n and m (1 ≤ n, m ≤ 200 000) — the number of commodities in the report and the number of managers, respectively.

The second line contains n integers ai (|ai| ≤ 109) — the initial report before it gets to the first manager.

Then follow m lines with the descriptions of the operations managers are going to perform. The i-th of these lines contains two integers tiand ri (, 1 ≤ ri ≤ n), meaning that the i-th manager sorts the first ri numbers either in the non-descending (if ti = 1) or non-ascending (if ti = 2) order.

Output

Print n integers — the final report, which will be passed to Blake by manager number m.

Examples

input

3 1
1 2 3
2 2

output

2 1 3

input

4 2
1 2 4 3
2 3
1 2

output

2 4 1 3

Note

In the first sample, the initial report looked like: 1 2 3. After the first manager the first two numbers were transposed: 2 1 3. The report got to Blake in this form.

In the second sample the original report was like this: 1 2 4 3. After the first manager the report changed to: 4 2 1 3. After the second manager the report changed to: 2 4 1 3. This report was handed over to Blake.

题意:

给你一个数组,然后有m个操作,1 2表示将数组的前两个数进行非递减排序,2 2表示将数组的前两个数进行非递增排序,给出全部操作完成后的序列

思路:

如果第一个操作的边界比第二个操作的边界小的话,第一个操作会被覆盖,那么再进行第一个操作就没有意义了,所以说咱们可以维护一个单调递减栈,栈里面存放的是边界,因为前面小的那些边界再怎么排,都是排那些数,后面的一个比他大的边界一操作就会让前面的操作付之东流,前面的排序时间就白浪费了,所以说可以用单调栈来进行操作,然后筛掉了一部分操作,这时候的复杂度还是比较大的,这时候就要用点思维了,九十八好比说有两组操作的话分别是 1 7 , 2 4,那么咱们先进行的是1到7的排序,进行非递减排序,然后再对前4个数进行非递增排序,这样一看只需要把最大的三个数保存下来,然后只对前四个进行操作就行了,这又节省了一部分时间,具体看代码吧

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int N = 200010;
typedef pair<int,int> PII;
int tt,a[N],ans[N];
PII s[N];
bool cmp1(int a,int b){
	return a > b;
}
bool cmp2(int a,int b){
	return a < b;
}
int main(){
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	for(int i=1;i<=m;i++){
		int op,r;
		scanf("%d%d",&op,&r);
		while(tt && s[tt].first <= r){
			tt--;
		}
		s[++tt] = {r,op};
	}
	for(int i=s[tt].first+1;i<=n;i++) ans[i] = a[i];
	sort(a+1,a+1+s[1].first);
	int l = 1,r = s[1].first;
	for(int i=1;i<tt;i++){
		if(s[i].second == 1){
			for(int j=s[i].first;j>s[i+1].first;j--) ans[j] = a[r--];
		}
		else{
			for(int j=s[i].first;j>s[i+1].first;j--) ans[j] = a[l++];
		}
	}
	for(int i=1;i<=s[tt].first;i++){
		if(s[tt].second == 2){
			ans[i] = a[r--];
		}
		else ans[i] = a[l++];
	}
	for(int i=1;i<=n;i++) printf("%d ",ans[i]);
	return 0;
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

宇智波一打七~

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值