Report CodeForces - 631C(单调栈)

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 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, 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
Copy
3 1
1 2 3
2 2
output
Copy
2 1 3 
input
Copy
4 2
1 2 4 3
2 3
1 2
output
Copy
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.

给你一个初始的序列a[1],a[2],...,a[n] ,请输出经过m次操作后的序列。
每个操作是以下两种之一:
    1 r,将子序列a[1]~a[r] 从小到大排序
    2 r,将子序列a[1]~a[r] 从大到小排序
 
对于每次操作,如果后面的r比前面的某一个r’大,那么这个r’有个luan用?
所以就是把r保持一个单调递减栈就好啦
处理的时候 肯定从大的开始
所以我们要用数组开栈 从底开始
对于每次操作i, (Node[i].r, Node[i - 1].r]这段区间是不变的
所以用双端下标。。。emm 看代码就知道啥意思了
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <list>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 210000, INF = 0x7fffffff;

LL a[maxn], b[maxn];

struct node
{
    int t, r;
}stak[maxn], tmp;


bool cmp(int a, int b)
{
    return a > b;
}

int main()
{
    int n, m, cnt = 0;
    cin >> n >> m;
    for(int i = 1; i <= n; i++)
        cin >> a[i];

    for(int i = 1; i <= m; i++)
    {
        cin >> tmp.t >> tmp.r;
        while(cnt && stak[cnt].r < tmp.r)
        {
            cnt--;
        }
        stak[++cnt] = tmp;
    }
 //   cout << cnt << endl;
    if(stak[1].t == 1)
        sort(a + 1, a + stak[1].r + 1);
    else sort(a + 1, a + stak[1].r + 1, cmp);
//    for(int i = 1; i <= n; i++)
//        cout << a[i] << endl;


    for(int i = stak[1].r + 1; i <= n; i++)
        b[i] = a[i];
    int l = 1, r = stak[1].r;
    for(int i = 2; i <= cnt; i++)
    {
        if(stak[1].t == 1)
            for(int j = stak[i - 1].r; j > stak[i].r; j--)
                if(stak[i - 1].t == 1) b[j] = a[r--];
                else b[j] = a[l++];
        else
            for(int j = stak[i - 1].r; j > stak[i].r; j--)
                if(stak[i - 1].t == 1) b[j] = a[l++];
                else b[j] = a[r--];
    }
   // cout << l << endl;
  // cout << stak[cnt].r << endl;

    if(stak[1].t == 1)
        for(int j = stak[cnt].r; j > 0; j--)
            if(stak[cnt].t == 1) b[j] = a[r--];
            else b[j] = a[l++];
    else
        for(int j = stak[cnt].r; j > 0; j--)
            if(stak[cnt].t == 1) b[j] = a[l++];
            else b[j] = a[r--];
    for(int i = 1; i <= n; i++)
    {
        if(i != 1) printf(" ");
        cout << b[i];

    }
    cout << endl;

    return 0;
}
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 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, 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
Copy
3 1
1 2 3
2 2
output
Copy
2 1 3 
input
Copy
4 2
1 2 4 3
2 3
1 2
output
Copy
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.

猜你喜欢

转载自www.cnblogs.com/WTSRUVF/p/10820149.html