64ptd求调
查看原帖
64ptd求调
751855
zhongpeilin楼主2024/4/17 22:36
#include <bits/stdc++.h>
using namespace std;
inline int read(){
	int x = 0, f = 1;
	char op = getchar();
	while(op < '0' || op > '9'){
		if(op == '-') f = -1;
		op = getchar();
	}
	while(op >= '0' && op <= '9'){
		x = x * 10 + (op - '0');
		op = getchar();
	}
	return x * f;
}
int n, q, a[100005], pos[100005], t[100005];
int Ca[100005], Cb[100005], Cc[100005], Cd[100005], Ce[100005];
long long ans[100005];
set<int> s;

void Add(int x, int c){
	for(int i = x; i <= n; i += (i & (-i))) Ca[i] += c;
}
void aDd(int x, int c){
	for(int i = x; i; i -= (i & (-i))) Cb[i] += c;
}
void adD(int x, int c){
	for(int i = x; i; i -= (i & (-i))) Cc[i] = min(Cc[i], c);
}
void ADD(int x, int c){
	for(int i = x; i; i -= (i & (-i))) Cd[i] += c;
}
void ADd(int x, int c){
	for(int i = x; i; i -= (i & (-i))) Ce[i] += c;
}
int Ask(int x){
	int res = 0;
	for(int i = x; i; i -= (i & (-i))) res += Ca[i];
	return res;
}
int aSk(int x){
	int res = 0;
	for(int i = x; i <= n; i += (i & (-i))) res += Cb[i];
	return res;
}
int asK(int x){
	int res = n + 1;
	for(int i = x; i <= n; i += (i & (-i))) res = min(res, Cc[i]);
	return res;
}

int ASK(int x){
	int res = 0;
	for(int i = x; i <= n; i += (i &(-i))) res += Cd[i];
	return res;
}
int ASk(int x){
	int res = 0;
	for(int i = x; i <= n; i += (i & (-i))) res += Ce[i];
	return res;
}
int Suan(int x){
	return aSk(a[x] + 1) - (Ask(n) - Ask(x));
}
int main(){
	n = read(); q = read();
	for(int i = 1; i <= n; i++) a[i] = read(), t[i] = q + 1;
	memset(Cc, 0x3f, sizeof(Cc));
	for(int i = 1; i <= q; i++) t[pos[i] = read()] = i;
	for(int i = 1; i <= n; i++) if(t[i] == q + 1) Add(i, 1), aDd(a[i], 1), adD(i, a[i]);
	for(int i = 1; i <= n; i++){
		if(t[i] == q + 1 && asK(i) == a[i]){
			ans[q] += Suan(i);
			s.insert(i);
			ADD(a[i], 1);
			ADd(i, 1);
		}
		if(t[i] == q + 1)ans[q]++;
	}
	for(int i = q - 1; i>= 0; i--){
		ans[i] = ans[i + 1] + 1;
		if(asK(pos[i + 1]) >= a[pos[i + 1]]){
			ans[i] += Suan(pos[i + 1]);
			auto it = s.lower_bound(pos[i + 1]);
			if(it != s.begin()){
				it--;
				while(it != s.begin() && a[(*(it))] > a[pos[i + 1]]){
					auto p = it;
					p--;
					Add((*(it)), -1);
					aDd(a[(*(it))], -1);
					ADD(a[(*(it))], -1);	
					ADd((*(it)), -1);
					ans[i] = ans[i] - Suan((*(it)));
					s.erase(it);
					it = p;
				}
				if(a[(*(it))] > a[pos[i + 1]]) {
					Add((*(it)), -1);
					aDd(a[(*(it))], -1);
					ADD(a[(*(it))], -1);
					ADd((*(it)), -1);
					ans[i] = ans[i] - Suan((*(it)));
					s.erase(it);
				}
			}
			s.insert(pos[i + 1]);
			ADD(a[pos[i + 1]], 1);
			ADd(pos[i + 1], 1);
		} else {
			ans[i] += ASk(pos[i + 1]) - ASK(a[pos[i + 1]]);
		}
		Add(pos[i + 1], 1), aDd(a[pos[i + 1]], 1), adD(pos[i + 1], a[pos[i + 1]]);
	}
	for(int i = 0; i<= q; i++) printf("%lld ", ans[i]);
	 return 0;
}
2024/4/17 22:36
加载中...