Codeforces 1288E Messenger Simulator(BIT)

Topic links:

codeforces 1288E Messenger Simulator

ideas:

1. For each number, its minimum either be moved to the far left is 1, or is the initial position;
2. As for calculating the maximum, greedy thought is: if a number is to be move, before we look at the number of mobile computing how many left; at the same time after the end of all mobile computing should also look at; maximum take all the historical maximum calculated value;
left 3. how quickly calculate how many number of it? This dynamic prefix and of course with Fenwick tree, how to simulate the situation to the left of it? We may initially located in the leftmost point of the m bit array value is 0, each movement is from right to left to fill the m position;

Code:

#include<bits/stdc++.h>

using namespace std;

const int maxn = 3e5 + 5;
int n, m, N, a[maxn], bit[maxn << 1];
int mn[maxn], mx[maxn], pos[maxn];
#define lowbit(i) (i&-i)
inline void add(int i, int x){
	while(i <= N) bit[i] += x, i += lowbit(i);	
}
inline int sum(int i){
	int ans = 0;
	while(i) ans += bit[i], i -= lowbit(i);
	return ans;	
}

int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif	
	scanf("%d %d", &n, &m);
	N = n + m;
	for(int i = 1; i <= m; i++) scanf("%d", a + i);
	for(int i = 1; i <= n; i++){
		mn[i] = mx[i] = i;
		add(i + m, 1);
		pos[i] = i + m;
	}
	for(int i = 1; i <= m; i++){
		int now = a[i];
		mn[now] = 1;
		mx[now] = max(mx[now], sum(pos[now]));
		add(pos[now], -1);
		pos[now] = m - i + 1;
		add(pos[now], 1);
	}
	for(int i = 1; i <= n; i++) mx[i] = max(mx[i], sum(pos[i]));
	for(int i = 1; i <= n; i++) printf("%d %d\n", mn[i], mx[i]);
	return 0;
}
Published 281 original articles · won praise 7 · views 6711

Guess you like

Origin blog.csdn.net/qq_45228537/article/details/103995558