Codeforces Round #754 (Div. 2) E题 Array Equalizer (数学+思维)

传送门

/*************************************************************************
	> File Name: 1.cpp
	> Author: Knowledge_llz
	> Mail: 925538513@qq.com 
	> Blog: https://www.cnblogs.com/Knowledge-Pig/ 
	> Created Time: 2021/11/13 10:24:12
 ************************************************************************/

#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define LL long long
#define pb push_back
#define fi first
#define se second
#define pr pair<int,int>
#define mk(a,b) make_pair(a,b)
#define endl '\n'
using namespace std;
const int maxx = 2e5 + 10;
LL n, Q, a[maxx], b[maxx], c[maxx], f[maxx], q[2][maxx], cnt[2];
LL sum[2][maxx];
LL count(LL id, LL x){
	if(!x) return 0;
	LL l, r; LL tmp = 0;
	if(x > 0){
		l = upper_bound(q[id] + 1, q[id] + cnt[id] + 1, -x) - q[id];
		r = lower_bound(q[id] + 1, q[id] + cnt[id] + 1, 0) - q[id] - 1;
		tmp -= (l - 1) * x; tmp += (cnt[id] - l + 1) * x;
		if(r >= l) tmp += (sum[id][r] - sum[id][l - 1]) * 2;
	}
	else{
		l = upper_bound(q[id] + 1, q[id] + cnt[id] + 1, 0) - q[id];
		r = lower_bound(q[id] + 1, q[id] + cnt[id] + 1, -x) - q[id] - 1;
		tmp -= r * x; tmp += (cnt[id] - r) * x;
		if(r >= l) tmp -= (sum[id][r] - sum[id][l - 1]) * 2;
	}
	return tmp;
}
int main(){
	ios::sync_with_stdio(false); cin.tie(0);
#ifndef ONLINE_JUDGE
	freopen("input.in", "r", stdin);
	freopen("output.out", "w", stdout);
#endif
	cin >> n;
	for(int i = 1; i <= n; ++i){ cin >> a[i]; c[i] = a[i]; }
	for(int i = 1; i <= n; ++i) cin >> b[i];
	f[1] = 1;
	for(int i = 1; i <= n; ++i){
		f[i] = -f[i];
		for(int j = i * 2; j <= n; j += i) f[j] += f[i];
	}
	LL s = 0;
	for(int i = 2; i <= n; ++i){
		int delta = b[i] - c[i];
		s += abs(delta);
		if(f[i] != 0){
			int x = (f[i] + 1) / 2;
			q[x][++cnt[x]] = delta;
		}
		for(int j = i; j <= n; j += i) c[j] += delta;
	}
	sort(q[0] + 1, q[0] + cnt[0] + 1);
	sort(q[1] + 1, q[1] + cnt[1] + 1);
	for(int i = 1; i <= cnt[0]; ++i){
		sum[0][i] = sum[0][i - 1] + q[0][i];
	}
	for(int i = 1; i <= cnt[1]; ++i){
		sum[1][i] = sum[1][i - 1] + q[1][i];
	}
	cin >> Q;
	while(Q--){
		cin >> b[1];
		LL x = a[1] - b[1], ans = s + abs(x);
		ans += count(1, x);
		ans += count(0, -x);
		cout << ans << endl;
	}
	return 0;
}
posted @ 2021-11-13 15:11  Knowledge-Pig  阅读(33)  评论(0)    收藏  举报