CF 631C report

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.

分析

  这题其实不是很难,蒟蒻的我都一眼找出了规律,首先,它不管是顺着排序还是倒着排序,如果某次操作比前边的一次操作范围大,那么前边的那次操作其实是无效的,另外,如果操作中最大的右端点到r,那么r再往后的区间是不会被修改的。因为操作的时候是修改1~r,所以前边的区间会被多次修改,而后边的区间则只会被最后那次不重叠的修改,如下图。

CF 631C report_第1张图片

 

 假如从1到i有很多次操作,从i到j有且仅有一次操作,那么我区间(i,j]的值就能确定,如果是由大到小排,那么j的位置一定是1,反之亦然,所以定义一个头指针和尾指针,倒着扫描一遍整个序列就好。

 1 #include
 2 #include
 3 using namespace std;
 4 const int N=2e5+10;
 5 int a[N],ans[N],idx[N],typ[N];
 6 int main(){
 7     int n,m;
 8     cin>>n>>m;
 9     for(int i=1;i<=n;i++)
10         cin>>a[i];
11     int mx=0;
12     for(int i=1;i<=m;i++){
13         int t,r;
14         cin>>t>>r;
15         idx[r]=i;
16         typ[r]=t;
17         mx=max(mx,r);
18     }
19     sort(a+1,a+mx+1);
20     int flag,now=0,hh=1,tt=mx;
21     for(int i=mx;i;i--){
22         if(now<idx[i]){
23             now=idx[i];
24             flag=typ[i];
25         }
26         if(flag==1)
27             ans[i]=a[tt--];
28         else ans[i]=a[hh++];
29     }
30     for(int i=1;i<=mx;i++)
31         cout<" ";
32     for(int i=mx+1;i<=n;i++)
33         cout<" ";
34     return 0;
35 }

 

你可能感兴趣的:(CF 631C report)