CodeForces

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 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.

题意:给出n个无序的数以及m个操作,每个操作由两个数组成,第一个数是操作的方式,第二个数 i 是操作的范围,若第一个数是1,则给 1-i 个数按升序排序,若第二个数是2,则给 1-i 个数按降序排列。输出所有操作完成后的序列。

思路:一道思维题,肯定不能直接一个个排序,重复操作次数太多肯定超时。

首先我们可以想到,如果下一个操作的区间比上一个要大,那么上一个操作将会是无效的,如下面两个操作:
1 5

2 8

先给前5个数按升序排列,再给前8个数按降序排列,那么第一个的升序排列就被第二个降序排列给覆盖了,所以第一个操作实际上是多余的。

所以我们可以删除那些不必要的操作来节约时间。那怎么删除呢?首先我们要找出所有操作中区间最大的操作,因为在这之前的操作都是不必要的;然后再找出这步操作之后区间最大的操作,一直如此,直到找完所有的操作。

最后我们筛选出的操作的区间就如下面的线段所示:

————————

——————

————

——

操作的区间逐渐递减,且都是不会被完全覆盖的关键操作。那具体如何筛选出关键操作:

我们首先声明一个数组t【20000】来存关键操作,数组初始化的第一个元素就是输入的第一个操作,然后遍历所有操作,如果你当前遍历到的操作,他的区间小于t数组中存下的最后一个操作,就把当前操作存入数组;否则就删除所有数组中区间比当前小的操作。因为你存储操作的过程中,只会存下比数组中操作区间小的,数组中比当前遍历到的操作小的都被删除了,所以你最后得到的一定是一个递减的关键操作序列。

但是,筛选出了关键操作还不够,还要继续优化:

假如总共给出了10个数,而最大的操作是:1 5,那么我们可以知道,答案序列的最后5个数一定和初始序列相同,因为没有操作会对后5个数进行操作,所以可以直接将后五个数存到ans数组的后5位。

接下来,看下面两个操作:

1 7

2 4

对于第一个操作,我们只需要确定5,6,7三个位置的值,因为前四个数会被下一个操作所影响,排了也是无效的。、

且确定5,6,7三个位置的值也很简单,我们先将最大操作区间内的所有数按升序排好,然后根据上面两个操作,第一个是1 7,第二个数2 4,因为第一个操作是升序,且只有5,6,7位置有效,那我们就可以在之前排好序的序列中选出最大的三个数放到ans中5,6,7的位置,然后在序列中删去这三个最大的数;如果是降序排也同理,选出最小的几个数放入ans即可。

下面看代码:

 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 ? '
' : ' ');
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
原文地址:https://www.cnblogs.com/tuyang1129/p/9335088.html