Codeforces 631C-Report

来源:互联网 发布:一首歌火了网络歌手 编辑:程序博客网 时间:2024/06/08 00:07
C. Report
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Each month Blake gets the report containing main economic indicators of the company "Blake Technologies". There aren 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 ofm 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 manageri + 1, or directly to Blake (if this manager has numberi = m).

Employees of the "Blake Technologies" are preparing the report right now. You know the initial sequenceai of lengthn and the description of each manager, that is valueri 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 andm (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. Thei-th of these lines contains two integers ti andri (,1 ≤ ri ≤ n), meaning that thei-th manager sorts the first ri numbers either in the non-descending (ifti = 1) or non-ascending (ifti = 2) order.

Output

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

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.

题意:已知一段无序的数字,求经过m次操作后的序列,每次操作把前ri个数按不严格递增(ti=1)或不严格递减(ti=2)排序。

思路:如果ri>rj并且i>j的话,第j次操作就没有意义可以去除了,那么最后有意义的操作剩下ri<rj并且 i>j,这样的话可以用数组t和r记录有效操作,用数组b把数组a中0~最大的ri的数字copy下来(因为这个ri之后的数字的顺序都不会改),然后遍历数组r,根据ti把数组b从左读或者从右读进数组a的ri-1~ri的位置。

#include <stdio.h>#include <algorithm>#include <string.h>using namespace std;#define maxn 200005int t[maxn], r[maxn], a[maxn], b[maxn];int main(){    int i, j, ti, ri, n, m, s, bl, br;    scanf("%d %d", &n, &m);    for(i = 0;i < n;i++) scanf("%d", &a[i]);    s = 0;    for(i = 0;i < m;i++){        scanf("%d %d", &ti, &ri);        while(s > 0&&ri >= r[s-1]) s--;        t[s] = ti, r[s] = ri, s++;    }    bl = 0, br = r[0], r[s++] = 0;    for(i = 0;i < br;i++) b[i] = a[i];    sort(b, b+br);    for(i = 1;i < s;i++)        for(j = r[i-1];j > r[i];j--)            a[j-1] = (t[i-1]==1)?b[--br]:b[bl++];    for(i = 0;i < n;i++) printf("%d%c", a[i], i==n-1?'\n':' ');}

0 0
原创粉丝点击