Educational Codeforces Round 80 (Rated for Div. 2)E(树状数组,模拟,思维)

 1 #define HAVE_STRUCT_TIMESPEC
 2 #include<bits/stdc++.h>
 3 using namespace std;
 4 int mn[600007],mx[600007],a[600007],pos[600007],sum[600007];
 5 int n,m;
 6 int lowbit(int x){
 7     return x&(-x);
 8 }
 9 void add(int x,int val){//单点更新
10     while(x<600007){
11         sum[x]+=val;
12         x+=lowbit(x);
13     }
14 }
15 int ask(int x){//区间查询[1~n]的和
16     int res=0;
17     while(x){
18         res+=sum[x];
19         x-=lowbit(x);
20     }
21     return res;
22 }
23 int main(){
24     ios::sync_with_stdio(false);
25     cin.tie(NULL);
26     cout.tie(NULL);
27     cin>>n>>m;
28     for(int i=1;i<=n;++i){
29         mn[i]=mx[i]=i;//初始最前最后都是i
30         pos[i]=i+m;//前面预留m个位置以便插入
31         add(pos[i],1);//当前位置有人了,所以给当前位置+1
32     }
33     for(int i=0;i<m;++i){
34         cin>>a[i];
35         mn[a[i]]=1;//它的最前更新为1
36         int p=a[i];
37         mx[p]=max(mx[p],ask(pos[p]));//更新最大值,是否更新为它之前位置前面有多少个数字(空位置不算,贡献是0)
38         add(pos[p],-1);//原来位置没有了
39         pos[p]=m-i;//新位置
40         add(pos[p],1);//数字换到了现在的位置上
41     }
42     for(int i=1;i<=n;++i)
43         mx[i]=max(mx[i],ask(pos[i]));//更新最大值,是否更新为它之前位置前面有多少个数字(空位置不算,贡献是0)
44     for(int i=1;i<=n;++i)
45         cout<<mn[i]<<' '<<mx[i]<<'\n';
46     return 0;
47 }

猜你喜欢

转载自www.cnblogs.com/ldudxy/p/12203364.html