# CodeForces - 631C ——（思维题）

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 mmanagers. Each of them may reorder the elements in some order. Namely, the i-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 ti and 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 11 2 32 2
Output
2 1 3
Input
4 21 2 4 32 31 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.

1 5

2 8

————————

——————

————

——

1 7

2 4

 1 #include<iostream>
2 #include<cstring>
3 #include<cstdio>
4 #include<string>
5 #include<cmath>
6 #include<climits>
7 #include<algorithm>
8 #include<stack>
9 #include<queue>
10 #define eps 1e-7
11 #define ll long long
12 #define inf 0x3f3f3f3f
13 #define pi 3.141592653589793238462643383279
14 using namespace std;
15 struct node{
16     int oper,end;
17 }b[200007]; //用来存操作
18 int n,m,a[200007],ans[200007];
19 int main()
20 {
21     cin>>n>>m;
22     for(int i=1; i<=n; ++i)
23         scanf("%d",&a[i]);
24
25     int sum=1,t[200007];
26     scanf("%d%d",&b[1].oper,&b[1].end);
27     t[sum++] = 1; //初始化关键操作为第一个操作
28     for(int i=2; i<=m; ++i)
29     {
30         scanf("%d%d",&b[i].oper,&b[i].end); //2降序，1升序
31         while( b[t[sum-1]].end <= b[i].end && sum > 1) //如果当前操作比存下的关键操作中的最后一个还关建，那就删去
32         {
33             sum--;
34         }
35         t[sum++] = i; //将所有不如自己关键的操作删去后，将自己存入
36     }
37     for(int i=n; i>=b[t[1]].end+1; i--) ans[i] = a[i]; //将最大操作区间之后的数直接存入ans数组
38
39     sort(a+1,a+1+b[t[1]].end); //个最大操作区间内的数排好序
40
41     int low = 1,high = b[t[1]].end; //用来确定剩下的数的范围
42     for(int i=2; i<sum; ++i)//根据两个关键操作的区间差来选择数放入ans数组
43     {
44         if(b[t[i-1]].oper==1)
45         {
46             for(int j=b[t[i-1]].end; j>=b[t[i]].end+1; --j)
47                 ans[j] = a[high--];
48         }
49         else
50         {
51             for(int j=b[t[i-1]].end; j>=b[t[i]].end+1; --j)
52                 ans[j] = a[low++];
53         }
54     }
55     for(int i=1; i<=b[t[sum-1]].end; ++i) //在剩下的最后一个操作的区间内放入剩下的数
56     {
57         if(b[t[sum-1]].oper==2)
58             ans[i] = a[high--];
59         else
60             ans[i] = a[low++];
61     }
62
63     for(int i=1; i<=n; ++i)
64         printf("%d%c",ans[i],i==n ? '\n' : ' ');
65     return 0;
66 }
67 /*
68 10 5
69 1 6 9 4 3 5 12 11 2 7
70 1 9
71 2 4
72 2 6
73 1 7
74 2 5
75 */
View Code

posted @ 2018-07-19 12:19  特务依昂  阅读(405)  评论(0编辑  收藏  举报