#include #include #include #include #include #include #include using namespace std; typedef struct animal { bool t; int u; int v; } animal; bool sort_on_v(animal &a, animal &b) { return a.v < b.v; } typedef struct tree_node { int max; int delta; } tree_node; int iCeilLog2(int x) { int y = 1; while (y < x) { y *= 2; } return y; } class tree { public: vector heap; int half; tree(int size) { size = max(size, 4); half = iCeilLog2(size); heap = vector(2 * half); for (int i = 0; i < size; i++) { auto &n = heap[half + i]; n.delta = i; n.max = i; } for (int i = size; i < half; i++) { auto &n = heap[half + i]; n.delta = 0; n.max = 0; } for (int i = half - 1; i > 0; i--) { recomputeMax(i); } } void recomputeMax(int i) { auto &n = heap[i]; auto &a = heap[2 * i]; auto &b = heap[2 * i + 1]; if (a.max < b.max) { n.max = b.max + n.delta; } else { n.max = a.max + n.delta; } } void incrementUntil(int x) { if (x >= half) { heap[1].delta += 1; } else { int n = half + x; for (; n != 1; n >>= 1) { if (n < half) { recomputeMax(n); } if (n % 2 == 1) { auto &node = heap[n - 1]; node.delta += 1; node.max += 1; } } } recomputeMax(1); } int maxUntil(int x) { if (x >= half) { return heap[1].max; } int z = INT_MIN; int n = half + x; for (; n != 1; n >>= 1) { if (n % 2 == 1) { auto &node = heap[n - 1]; auto max = node.max; for (int w = (n - 1) >> 1; w != 0; w >>= 1) { max += heap[w].delta; } if (z < max) { z = max; } } } return z; } }; int main() { int t_count; cin >> t_count; for (int ti = 0; ti < t_count; ti++) { int m, n; cin >> m >> n; vector as2(n); for (int i = 0; i < n; i++) { char x; cin >> x; as2[i].t = x == 'E' || x == 'C'; } for (int i = 0; i < n; i++) { cin >> as2[i].u; as2[i].u -= 1; } for (int i = 0; i < n; i++) { cin >> as2[i].v; as2[i].v -= 1; } /* tree zz(1); for (int k = -1; k <= 100; k++) zz.incrementUntil(k); for (int k = -1; k <= 100; k++) cout << zz.maxUntil(k) << endl; return 0; if (ti != 3) continue; */ vector as; for (auto a : as2) { if (a.u < a.v) { as.push_back(a); } } as2.clear(); sort(as.begin(), as.end(), sort_on_v); vector ds; ds.push_back(0); vector trees; trees.push_back(tree(n + 10)); trees.push_back(tree(n + 10)); for (auto a : as) { auto k = upper_bound(ds.begin(), ds.end(), a.u) - ds.begin(); trees[a.t].incrementUntil(k); auto z = trees[a.t].maxUntil(k); //cout << "a=" << a.u << " " << a.v << " " << a.t << " " << k << " " << z.first << " " << z.second << " " << endl; if (ds.size() == z) { ds.push_back(a.v); //cout << "pushed" << endl; } } for (int i = 1; i <= n; i++) { if (i < ds.size()) { cout << ds[i] + 1 << " "; } else { cout << -1 << " "; } } cout << endl; } return 0; }