首页 > 其他分享 >Codeforces Round #344 (Div. 2)-C. Report(单调栈)

Codeforces Round #344 (Div. 2)-C. Report(单调栈)

时间:2023-06-12 17:38:04浏览次数:51  
标签:node Blake int Codeforces 344 report manager Report first


原题链接


C. Report



time limit per test



memory limit per test



input



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 manageri + 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



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 (

Codeforces Round #344 (Div. 2)-C. Report(单调栈)_sed

, 1 ≤ ri ≤ n), meaning that the i-th manager sorts the first ri numbers either in the non-descending (if ti) or non-ascending (if ti) 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. 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. After the second manager the report changed to: 2 4. This report was handed over to Blake.



通过单调递减栈去掉无效操作,而且栈中相邻两个操作的t值相反.对原数组p中的元素按照单调栈的最大区间[0, node[Stack[0]].r]从小到大排序,l = 0, r = node[Stack[0]].r.

d1 = node[Stack[0]].r ,d2 = node[Stack[1]].r

首先判断d1 >d 2, 那么[d2+1, d1]中的元素可以确定,且以后操作中不再变化.若node[Stack[0]].t == 1,那么[d2+1, d1]中元素可以为数组p中[r - (d1-d2)+1, r]中的元素, 更新r = node[Stack[1]].r,以此类推


#include <bits/stdc++.h>
#define maxn 200005
using namespace std;
typedef long long ll;

struct Node{
	int t, r;
}node[maxn];
int num[maxn], p[maxn], Stack[maxn];
int ans[maxn];
int main(){
	
//	freopen("in.txt", "r", stdin);
	int n, m;
	
	scanf("%d%d", &n, &m);
	for(int i = 0; i < n; i++){
		scanf("%d", num+i);
		p[i] = num[i];
		ans[i] = num[i];
	}
	int r = 0;
	for(int j = 0; j < m; j++){
		scanf("%d%d", &node[j].t, &node[j].r);
		node[j].r--;
		while(r != 0 && node[Stack[r-1]].r <= node[j].r)
		 r--;
		if(r == 0){
			Stack[r++] = j;
		}
		else{
			if(node[Stack[r-1]].t != node[j].t)
			 Stack[r++] = j;
		}
	}
	sort(p, p+node[Stack[0]].r+1);
	int k1 = 0, k2 = node[Stack[0]].r;
	int v = k2;
	for(int i = 0; i < r; i++){
		int d;
		if(i + 1 < r)
			d = node[Stack[i]].r - node[Stack[i+1]].r;
	    else
	       d = node[Stack[i]].r + 1;
	       
		while(d--){
			if(node[Stack[i]].t == 1)
			 ans[v--] = p[k2--];
			else
			 ans[v--] = p[k1++];
		}
	}
	printf("%d", ans[0]);
	for(int i = 1; i < n; i++)
	 printf(" %d", ans[i]);
	puts("");
	return 0; 
}




标签:node,Blake,int,Codeforces,344,report,manager,Report,first
From: https://blog.51cto.com/u_16158872/6464256

相关文章