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 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
| #include <bits/stdc++.h> #define debug(...) fprintf(stderr, __VA_ARGS__) #define RI register int typedef long long LL;
#define FILEIO(name) freopen(name".in", "r", stdin), freopen(name".out", "w", stdout);
using namespace std;
namespace IO { char buf[1000000], *p1 = buf, *p2 = buf; inline char gc() { if (p1 == p2) p2 = (p1 = buf) + fread(buf, 1, 1000000, stdin); return p1 == p2 ? EOF : *(p1++); } template <class T> inline void read(T &n) { n = 0; RI ch = gc(), f; while ((ch < '0' || ch > '9') && ch != '-') ch = gc(); f = (ch == '-' ? ch = gc(), -1 : 1); while (ch >= '0' && ch <= '9') n = n * 10 + (ch ^ 48), ch = gc(); n *= f; } char Of[105], *O1 = Of, *O2 = Of; template <class T> inline void print(T n, char ch = '\n') { if (n < 0) putchar('-'), n = -n; if (n == 0) putchar('0'); while (n) *(O1++) = (n % 10) ^ 48, n /= 10; while (O1 != O2) putchar(*(--O1)); putchar(ch); } }
using IO :: read; using IO :: print;
int const MAXN = 2e5 + 5; struct Node { int v, id; bool operator < (const Node &A) const { return v ^ A.v ? v < A.v : id < A.id; } bool operator == (const Node &A) const { return v == A.v && id == A.id; } } a[MAXN], b[MAXN]; struct ErasableHeap { priority_queue <Node> I, O; inline void flush() { while (!O.empty() && I.top() == O.top()) I.pop(), O.pop(); } inline void clear() { while (!I.empty()) I.pop(); while (!O.empty()) O.pop(); } inline void insert(Node x) { I.push(x); } inline void erase(Node x) { O.push(x); } inline bool empty() { flush(); return I.empty(); } inline Node top() { flush(); return I.top(); } } q1, q2, q3, q4, q5;
int visa[MAXN], visb[MAXN]; int stkQ[MAXN], stkO[MAXN];
int cntO, cntQ;
void DeleteAll(int x) { if (visa[x] == 0 && visb[x] == 0) q1.erase((Node){a[x].v + b[x].v, x}); if (visa[x] == 0) q2.erase(a[x]); if (visb[x] == 0) q3.erase(b[x]); if (visa[x] == 0 && visb[x] == 1) q4.erase(a[x]); if (visa[x] == 1 && visb[x] == 0) q5.erase(b[x]); if (visa[x] == 1 && visb[x] == 1) --cntQ; if (visa[x] == 2 && visb[x] == 2) --cntO; } void InsertAll(int x) { if (visa[x] == 0 && visb[x] == 0) q1.insert((Node){a[x].v + b[x].v, x}); if (visa[x] == 0) q2.insert(a[x]); if (visb[x] == 0) q3.insert(b[x]); if (visa[x] == 0 && visb[x] == 1) q4.insert(a[x]); if (visa[x] == 1 && visb[x] == 0) q5.insert(b[x]); if (visa[x] == 1 && visb[x] == 1) stkQ[++cntQ] = x; if (visa[x] == 2 && visb[x] == 2) stkO[++cntO] = x; } void PushInQ(int *vis, int x) { DeleteAll(x); vis[x] = 1; InsertAll(x); } void PushInO(int x) { DeleteAll(x); visa[x] = visb[x] = 2; InsertAll(x); }
int main() {
int T; read(T); while (T--) { int n, K, L; read(n), read(K), read(L); int res = K - L; for (RI i = 1; i <= n; ++i) read(a[i].v), a[i].id = i; for (RI i = 1; i <= n; ++i) read(b[i].v), b[i].id = i;
for (RI i = 1; i <= n; ++i) visa[i] = visb[i] = 0; cntO = 0, cntQ = 0; q1.clear(); q2.clear(); q3.clear(); q4.clear(); q5.clear();
sort(a + 1, a + 1 + n, [](Node x, Node y){ return x.v > y.v; }); sort(b + 1, b + 1 + n, [](Node x, Node y){ return x.v > y.v; }); LL ans = 0; for (RI i = 1; i <= res; ++i) { ans += a[i].v, visa[a[i].id] = 1; ans += b[i].v, visb[b[i].id] = 1; } sort(a + 1, a + 1 + n, [](Node x, Node y){ return x.id < y.id; }); sort(b + 1, b + 1 + n, [](Node x, Node y){ return x.id < y.id; }); for (RI i = 1; i <= n; ++i) InsertAll(i);
int tmp = L; while (L--) { LL val1 = q1.top().v; LL val2 = !q2.empty() && !q5.empty() ? q2.top().v + q5.top().v : -0x7f7f7f7f7f7f7f7f; LL val3 = !q3.empty() && !q4.empty() ? q3.top().v + q4.top().v : -0x7f7f7f7f7f7f7f7f; LL val4 = cntQ && !q2.empty() && !q3.empty() ? q2.top().v + q3.top().v : -0x7f7f7f7f7f7f7f7f; LL val5 = cntO && !q4.empty() && !q5.empty() ? q4.top().v + q5.top().v : -0x7f7f7f7f7f7f7f7f; if (val1 > val2 && val1 > val3 && val1 > val4 && val1 > val5) { ans += val1; PushInO(q1.top().id); } else if (val2 > val3 && val2 > val4 && val2 > val5) { ans += val2; int tmp = q5.top().id; PushInQ(visa, q2.top().id); PushInO(tmp); } else if (val3 > val4 && val3 > val5) { ans += val3; int tmp = q4.top().id; PushInQ(visb, q3.top().id); PushInO(tmp); } else if (val4 > val5) { ans += val4; int t1 = q2.top().id, t2 = q3.top().id; PushInO(stkQ[cntQ]); PushInQ(visa, t1); PushInQ(visb, t2); } else { ans += val5; int t1 = q4.top().id, t2 = q5.top().id, t3 = stkO[cntO]; PushInQ(visa, t3), PushInQ(visb, t3); PushInO(t1); PushInO(t2); } } print(ans); }
cerr << (double)(clock()) / CLOCKS_PER_SEC << " ms " << endl;
return 0; }
|