1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| #include<bits/stdc++.h> using namespace std; typedef long long ll; #define pii pair<int, int> #define pll pair<ll, ll> #define v1(x) vector<x> #define v2(x) vector<vector<x>> #define v3(x) vector<vector<vector<x>>> #define read_temp(x) int x; cin>>x; #define min(args...) min({args}) #define max(args...) max({args}) #define endl '\n' #ifdef DEBUG template <typename T, enable_if_t<is_same_v<T, char>, bool> = true> void __output(T t) { cout << t << (t == '\n' ? "" : " "); } template <typename T, typename... Args, enable_if_t<not is_same_v<T, char>, bool> = true> void __output(T t) { cout << t << " "; } void __log() { cout << flush; } template <typename T, typename... Args> void __log(T t, Args... rest) { __output(t); __log(rest...); } #define log(args...) if(LOG_OUTPUT) __log(args) #else #define log(args...) 6 #endif #define LOG_OUTPUT 1 #define MULTIPLE_CASES
ll n, k;
void Main(int kase) { cin >> n >> k;
ll all2 = 1; v1(ll) A(2 * n + 1), B(2 * n + 1), mp(2 * n + 1); for (ll i = 1;i <= n;i++) cin >> A[i], A[i + n] = A[i]; for (ll i = 1;i <= n;i++) { cin >> B[i], B[i + n] = B[i]; if (B[i] == 1) all2 = 0; }
for (ll i = 2 * n;i >= 2;i--) A[i] = A[i - 1]; A[1] = 0; for (ll i = 1;i <= 2 * n;i++) A[i] += A[i - 1];
if (all2) { for (ll i = 1;i <= n;i++) { cout << (A[i + n] - A[i]) * 2 << " "; } cout << endl; return; }
v1(ll) V1(1); for (ll i = 1;i <= 2 * n;i++) if (B[i] == 1) mp[i] = V1.size(), V1.push_back(A[i]); ll n1 = V1.size() - 1;
v2(ll) bl(n1, v1(ll)(20)); for (ll i = 1;i < n1;i++) bl[i][0] = V1[i + 1] - V1[i] <= k ? V1[i + 1] - V1[i] : k + (V1[i + 1] - V1[i] - k) * 2; for (ll j = 1, base = 1;j <= 19;j++, base *= 2) for (ll i = 1;i < n1 && i + base < n1 && bl[i][j - 1] != 0 && bl[i + base][j - 1] != 0;i++) bl[i][j] = bl[i][j - 1] + bl[i + base][j - 1];
v1(ll) pre1(2 * n + 1), nxt1(2 * n + 1); ll pos1 = -1; for (ll i = 1;i <= 2 * n;i++) { if (B[i] == 1) pos1 = i; pre1[i] = pos1; }
pos1 = -1; for (ll i = 2 * n;i >= 1;i--) { if (B[i] == 1) pos1 = i; nxt1[i] = pos1; }
for (ll i = 1;i <= n;i++) { ll start1 = nxt1[i], end1 = pre1[i + n]; ll ans = 0; ans += 2 * (A[start1] - A[i]); ans += A[i + n] - A[end1] <= k ? A[i + n] - A[end1] : k + (A[i + n] - A[end1] - k) * 2; if (end1 == start1) { cout << ans << " "; continue; }
ll dis = mp[end1] - mp[start1], j = 0, cur = mp[start1], base = 1;
while (dis) { if (dis % 2) { ans += bl[cur][j]; cur += base; } dis /= 2; base *= 2; j++; }
cout << ans << " "; } cout << endl; }
int main() { #ifdef DEBUG freopen("in.txt", "r", stdin); #endif
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int t = 0; #ifdef MULTIPLE_CASES int T; cin >> T; while (++t <= T) #endif Main(t);
cout << flush; return 0; }
|