Report
limit 2s,256M
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 ≤ 200000) — 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.
example
input
4 2
1 2 4 3
2 3
1 2
output
2 4 1 3
题意
给定一个序列,进行m次操作,每一次都会把区间[1-r]内的数升序或者降序排列求m次操作之后的序列
题解(此处照搬我校卿神题解)
/*
对于每个端点,其实就最后一次操作有用
于是我们就只用看这个数最后是什么操作就好了
然后依旧记录一下时间戳,倒着跑一遍就好了
*/
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=2e5;
struct Struct
{
int time;
int x;
int op;
};
Struct opr[maxn+5];
int arr[maxn+5];
int ans[maxn+5];
bool cmp(const Struct &x,const Struct &y)
{
return x.x<y.x;
}
bool op2(const int &a,const int &b)
{
return a>b;
}
int main(void)
{
#ifdef ex
freopen ("in.txt","r",stdin);
//freopen ("1.txt","w",stdout);
#endif
int n,m;
int t1,t2;
scanf("%d%d",&n,&m);
for (int i=1;i<=n;++i)
{
scanf("%d",&arr[i]);
}
for (int i=1;i<=m;++i)
{
scanf("%d%d",&t1,&t2);
opr[i].op=t1;
opr[i].x=t2;
opr[i].time=i;
}
sort(opr+1,opr+m+1,cmp);
int cnt=m;
int now_op;
if (opr[cnt].op==1)
{
sort(arr+1,arr+opr[cnt].x+1);
now_op=1;
}
else
{
sort(arr+1,arr+opr[cnt].x+1,op2);
now_op=2;
}
for (int i=opr[m].x+1;i<=n;++i)
{
ans[i]=arr[i];
}
int s=1;
int t=opr[m].x;
int k=t;
opr[0]=(Struct){m+1,0,3};
for (int i=cnt;i>=0;--i)
{
//if (opr[i].time>opr[cnt].time && opr[i].op!=opr[cnt].op)
if (opr[i].time>opr[cnt].time )
{
int len=opr[cnt].x-opr[i].x;
if (now_op==opr[m].op)
{
for (int j=1;j<=len;++j)
{
ans[k]=arr[t];
--k;
--t;
}
}
else
{
for (int j=1;j<=len;++j)
{
ans[k]=arr[s];
++s;
--k;
}
}
cnt=i;
now_op=opr[cnt].op;
}
}
for (int i=1;i<=n;++i)
{
printf("%d ",ans[i]);
}
printf("\n");
}
有一处细节
if (opr[i].time>opr[cnt].time && opr[i].op!=opr[cnt].op)是错误的。虽然同样的op不会影响当前序列的复制,但是如果不更新time就会导致后面出错
if (opr[i].time>opr[cnt].time )才是正确的