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
| #include<bits/stdc++.h> #define int long long using namespace std;
inline int read() { int x = 0, f = 1; char ch = getchar(); while (ch < '0' || ch > '9') { if (ch == '-') f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - 48; ch = getchar(); } return x * f; }
const int N = 2e5 + 10; int n, m, s; int a[N], b[N]; int c[N], d[N]; int sma[N], smb[N]; int suma[N], sumb[N];
int lowbit(int x) { return x & -x; }
void add(int tr[], int x, int a) { for (int i = x; i <= s; i += lowbit(i)) tr[i] += a; }
int sum(int tr[], int x) { int res = 0; for (int i = x; i; i -= lowbit(i)) res += tr[i]; return res; }
void solve() { memset(c, 0, sizeof c); memset(d, 0, sizeof d);
n = read(), m = read(), s = n + m + 1; for (int i = 1; i <= s; i++) a[i] = read(); for (int i = 1; i <= s; i++) b[i] = read(); for (int i = 1; i <= s; i++) { if (a[i] > b[i]) { add(c, i, 1); sma[i] = a[i] + sma[i - 1]; smb[i] = smb[i - 1]; } else { add(d, i, 1); smb[i] = b[i] + smb[i - 1]; sma[i] = sma[i - 1]; } suma[i] = suma[i - 1] + a[i]; sumb[i] = sumb[i - 1] + b[i]; }
for (int i = 1; i <= s; i++) { add(c, i, -(a[i] > b[i])); add(d, i, -(b[i] > a[i]));
int l = 0, r = s; while (l < r) { int mid = l + r >> 1; if (sum(c, mid) >= n) r = mid; else l = mid + 1; } int posn = l;
l = 0, r = s; while (l < r) { int mid = l + r >> 1; if (sum(d, mid) >= m) r = mid; else l = mid + 1; } int posm = l;
int res = 0; if (posn > posm) { res = sma[posm] + smb[posm] + (suma[s] - suma[posm]); if (i <= posm) { if (a[i] > b[i]) res -= a[i]; else res -= b[i]; } else res -= a[i]; } else { res = sma[posn] + smb[posn] + (sumb[s] - sumb[posn]); if (i <= posn) { if (a[i] > b[i]) res -= a[i]; else res -= b[i]; } else res -= b[i]; }
cout << res << ' ';
add(c, i, a[i] > b[i]); add(d, i, b[i] > a[i]); } puts(""); }
signed main() { int t = read(); while (t--) solve(); return 0; }
|