[codeforces 1288E] Messenger Simulator 镜像+树状数组

[codeforces 1288E] Messenger Simulator 镜像+树状数组

总目录详见https://blog.csdn.net/mrcrack/article/details/103564004

在线测评地址https://codeforces.com/contest/1288/problem/E

Problem Lang Verdict Time Memory
E - Messenger Simulator GNU C++11 Accepted 217 ms 8000 KB

以样例1为例
Input
5 4
3 5 1 4
Output
1 3
2 5
1 4
1 5
1 5

[1,2,3,4,5]          (),(),(),(),(1),(2),(3),(4),(5)         镜像          (5),(4),(3),(2),(1),(),(),(),()
[3,1,2,4,5]          (),(),(),(3),(1),(2),(),(4),(5)         镜像          (5),(),(4),(3),(2),(1),(),(),()
[5,3,1,2,4]          (),(),(5),(3),(1),(2),(),(4),()         镜像          (),(4),(),(2),(1),(3),(5),(),()
[1,5,3,2,4]          (),(1),(5),(3),(),(2),(),(4),()         镜像          (),(4),(),(2),(),(3),(5),(1),()
[4,1,5,3,2]          (4),(1),(5),(3),(),(2),(),(),()         镜像          (),(),(),(2),(),(3),(5),(1),(4)

[1,2,3,4,5]          (5),(4),(3),(2),(1),(),(),(),()
树状数组              1,  1,   1,  1,   1,0,0,0,0      

[3,1,2,4,5]          (5),(),(4),(3),(2),(1),(),(),()
树状数组              1,0,   1,   1,  1,  1,0,0,0

[5,3,1,2,4]          (),(4),(),(2),(1),(3),(5),(),()
树状数组            0,  1,0,   1,    1,  1,  1,0,0

[1,5,3,2,4]          (),(4),(),(2),(),(3),(5),(1),()
树状数组            0,  1, 0,  1, 0,  1,  1,  1,0

[4,1,5,3,2]          (),(),(),(2),(),(3),(5),(1),(4)
树状数组            0,0,0,   1,0,  1,   1,  1,  1

解释如下

 建议结合代码进行学习。

#include <stdio.h>
#define maxn 300010
int c[maxn<<2],pos[maxn],mx[maxn],mn[maxn],n,m;
int lowbit(int x){
	return x&-x;
}
void update(int x,int delta){
	for(int i=x;i<=n+m;i+=lowbit(i))c[i]+=delta;
}
int query(int x){
	int ret=0;
	for(int i=x;i>0;i-=lowbit(i))ret+=c[i];
	return ret;
}
int max(int a,int b){
	return a>b?a:b;
}
int main(){
	int i,x;
	scanf("%d%d",&n,&m);
	for(i=1;i<=n;i++)pos[i]=n-i+1,update(pos[i],1),mx[i]=mn[i]=i;
	for(i=n+1;i<=n+m;i++){
		scanf("%d",&x);
		mn[x]=1;
		mx[x]=max(mx[x],query(n+m)-query(pos[x]-1));
		update(pos[x],-1);
		pos[x]=i;
		update(pos[x],1);
	}
	for(i=1;i<=n;i++)mx[i]=max(mx[i],query(n+m)-query(pos[i]-1));
	for(i=1;i<=n;i++)printf("%d %d\n",mn[i],mx[i]);
	return 0;
}
发布了487 篇原创文章 · 获赞 516 · 访问量 43万+

猜你喜欢

转载自blog.csdn.net/mrcrack/article/details/104013722