C. Report
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
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.
此题重点在于排序的优化,化简,还有编程思维。写了一晚上。编程思维很重要啊!
//
// Created by 王若璇 on 16/3/5.
//
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
const int max_n = 200100;
int a[max_n];
pair<int,int> op[max_n];
vector<pair<int,int> > v;
/*template<class BidirIt> reverse sourse code
void reverse(BidirIt first, BidirIt last)
{
while ((first != last) && (first != --last)) {
std::iter_swap(first++, last);
}
}*/
vector<int> ans;
int main(){
ios::sync_with_stdio(false);
cin.tie(NULL);
int n,m;
while (cin>>n>>m){
v.clear();
ans.clear();
for(int i = 0;i<n;i++){
cin>>a[i];
}
for(int i = 0;i<m;i++){
cin>>op[i].first>>op[i].second;
}
int pos = -1;
int op_num = 0;
for(int i = m-1;i>=0;i--){
if(op[i].second>pos){
v.push_back(op[i]);
pos = op[i].second;
op_num++;
}
}
sort(a,a+v[op_num-1].second);
//cout<<right<<"right"<<endl;
int pos_ans = 0;
reverse(v.begin(),v.end());
v.push_back(make_pair(0,0));
for(int i = n-1;i>=v[0].second;i--){
ans.push_back(a[i]);
}
int left = 0,right = v[0].second-1;
for(int i = 0;i<v.size();i++){
int cnt = v[i].second-v[i+1].second;
// cout<<"v[i] "<<v[i].second<<" v[i+1] "<<v[i+1].second<<endl;
// cout<<cnt<<" *"<<endl;
for(int j = 0;j<cnt;j++){
if (v[i].first==1){
ans.push_back(a[right--]);
} else{
ans.push_back(a[left++]);
// cout<<" *****";
}
}
}
reverse(ans.begin(),ans.end());
for(int i = 0;i<n;i++){
cout<<ans[i]<<" ";
}
cout<<'\n';
}
return 0;
}