求大佬帮调(优良码风)
查看原帖
求大佬帮调(优良码风)
490978
小超手123楼主2023/11/20 14:48
#include<bits/stdc++.h>
#define int long long
#define N 100005
using namespace std;
int n, m;
struct node{
	int id, a, t, ans;
}p[N];
int num[N];
bool CMP(node x, node y) {
	return x.id < y.id;
}
bool cmp1(node x, node y) {
	return x.a > y.a;
}
bool cmp2(node x, node y) {
	return x.a < y.a;
}
int lowbit(int x) {
	return x & (-x); 
}
int c[N];
int query(int x) {
	x++;
	int res = 0;
	for(int i = x; i; i -= lowbit(i)) {
		res += c[i];
	}
	return res;
}
void add(int x, int y) {
	x++;
	for(int i = x; i <= n + 1; i += lowbit(i)) {
		c[i] += y;
	} 
} 
int ask(int L, int R) {
	return query(R) - query(L - 1);
}
void Get(int L, int R) {
	if(L == R) return;
	int mid = (L + R) / 2;
	Get(L, mid); Get(mid + 1, R);
	//for(int i = L; i <= R; i++) cout << p[i].a << " ";
	//cout << endl;
	sort(p + L, p + mid + 1, cmp1);
	sort(p + mid + 1, p + R + 1, cmp1);
	int j = L;
	for(int i = mid + 1; i <= R; i++) {
		while(p[j].a > p[i].a && j <= mid) {
			add(p[j].t, 1);
			j++;
		}
		p[i].ans += ask(p[i].t, n);
	}
	for(int i = L; i < j; i++) {
		add(p[i].t, -1);
	}
	sort(p + L, p + mid + 1, cmp2);
	sort(p + mid + 1, p + R + 1, cmp2);
	j = mid + 1;
	for(int i = L; i <= mid; i++) {
		while(p[i].a > p[j].a && j <= R) {
			add(p[j].t, 1);
			j++;
		}
		p[i].ans += ask(p[i].t, n);
	}
	for(int i = mid + 1; i < j; i++){
		add(p[i].t, -1);
	} 
	
	//for(int i = L; i <= R; i++) cout << p[i].a << " ";
	//cout << endl;
}
signed main() {
	scanf("%lld %lld", &n, &m);
	for(int i = 1; i <= n; i++) {
		scanf("%lld", &p[i].a);
		p[i].id = i;
	} 
	for(int i = 1; i <= m; i++) {
		scanf("%lld", &num[i]);
		p[num[i]].t = i;
	}
	for(int i = 1; i <= n; i++) {
		if(p[i].t == 0) p[i].t = m + 1;
	}
	int res = 0;
	for(int i = 1; i <= n; i++) {
		res += ask(p[i].a + 1, n);
		add(p[i].a, 1);
	}
	for(int i = 1; i <= n; i++) add(p[i].a, -1);
	Get(1, n); 
	sort(p + 1, p + n + 1, CMP);
	for(int i = 1; i <= m; i++) {
		printf("%lld\n", res);
		res -= p[num[i]].ans;
	}
	return 0;
}
2023/11/20 14:48
加载中...