#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include //#include //#include #include //#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define forn(i, n) for(int i = 0; i < int(n); i++) #define forn1(i, n) for(int i = 1; i <= int(n); i++) #define sz(a) int((a).size()) #define all(a) (a).begin(), (a).end() #define mp make_pair #define pb push_back #define x first #define y second typedef long long li; typedef long double ld; typedef pair pt; const int INF = int(1e9); const li INF64 = li(1e18); const ld PI = acosl(ld(-1)); const ld EPS = 1e-9; template inline T sqr(const T& x) { return x * x; } template inline T abs(const T& x) { return x > 0 ? x : -x; } inline bool inside(int x, int y, int n, int m) { return x >= 0 && x < n && y >= 0 && y < m; } inline int rnd() { return abs(rand() ^ (rand() << 15)); } inline int rnd(int n) { assert(n > 0); return rnd() % n; } inline int rnd(int lf, int rg) { return lf + rnd(rg - lf + 1); } inline li rndLL() { return rnd() * 1LL * rnd() + rnd(); } const int dx[4] = { -1, 0, +1, 0 }; const int dy[4] = { 0, +1, 0, -1 }; const int dx8[8] = { -1, -1, 0, +1, +1, +1, 0, -1 }; const int dy8[8] = { 0, +1, +1, +1, 0, -1, -1, -1 }; const int N = int(1e5) + 555; int n, m; char t[N]; int s[N], d[N]; void gen() { } bool read() { if (!(cin >> n >> m)) return false; forn1(i, m) assert(scanf(" %c ", &t[i]) == 1); forn1(i, m) assert(scanf("%d", &s[i]) == 1); forn1(i, m) assert(scanf("%d", &d[i]) == 1); return true; } int dp[N][2]; vector v[N][2]; int ptr[N][2]; int ans[N]; bool us[N][2]; vector used[2]; int mx[2]; int start[N][2]; int maxSuf[2]; vector szs = { 100 }; void solve() { //forn(i, N) forn(j, 2) mx[i][j] = 0; forn(j, 2) maxSuf[j] = 0; forn(i, N) forn(j, 2) start[i][j] = 0; forn(j, 2) used[j].clear(); forn(i, N) forn(j, 2) us[i][j] = false; forn(i, N) forn(j, 2) v[i][j].clear(); forn1(i, m) { if (s[i] > d[i]) continue; int tp = ((t[i] == 'E' || t[i] == 'C') ? 0 : 1); v[d[i]][tp].push_back(s[i]); us[d[i]][tp] = true; start[s[i]][tp]++; maxSuf[tp] = max(maxSuf[tp], d[i]); } for (int i = n; i >= 1; i--) forn(j, 2) start[i][j] += start[i + 1][j]; forn(i, N) forn(j, 2) assert(start[i][j] >= 0); forn(i, N) forn(j, 2) if (us[i][j]) used[j].push_back(i); forn(j, 2) { // forn1(i, n) cerr << start[i][j] << ' '; cerr << endl; } //forn(i, N) forn(j, 2) sort(all(v[i][j])); /* forn(j, 2) { cerr << sz(used[j]) << endl; forn(k, sz(used[j])) cerr << used[j][k] << ' '; cerr << endl; } */ forn(i, N) forn(j, 2) dp[i][j] = -1; dp[0][0] = 0; dp[0][1] = 0; forn(j, 2) mx[j] = -1; int c1 = 0, c2 = 0; forn1(i, n) { c1 += sz(v[i][0]); c2 += sz(v[i][1]); dp[i][0] = c1; dp[i][1] = c2; } forn(i, N) forn(j, 2) ptr[i][j] = 0; forn(i, N) forn(j, 2) sort(all(v[i][j])); forn1(i, n - 1) forn(j, 2) { if (i % 1000 == 0 && ld(clock()) / CLOCKS_PER_SEC > 1.8) break; //if (i % 10 == wtf) continue; int cur = dp[i][j]; if (cur <= 0) continue; if (cur <= mx[j]) continue; mx[j] = cur; int cnt = 0; int it = int(lower_bound(all(used[1 - j]), i) - used[1 - j].begin()); //cerr << "i j == " << i << ' ' << j << endl; //int rg = sz(used[1 - j]); int rg = sz(used[1 - j]); rg = min(rg, it + 30000); //if (i % 10 == 0) rg = min(it + 2000, sz(used[1 - j])); //if (i + 3000 >= n) rg = sz(used[1 - j]); for (int jj = it; jj < rg; jj++) { int nj = used[1 - j][jj]; //cerr << "nj == " << nj << endl; //cerr << "szv == " << sz(v[nj][1 - j]) << endl; //forn(k, sz(v[nj][1 - j])) if (v[nj][1 - j][k] >= i) cnt++; while (ptr[nj][1 - j] < sz(v[nj][1 - j]) && v[nj][1 - j][ptr[nj][1 - j]] < i) ptr[nj][1 - j]++; cnt += sz(v[nj][1 - j]) - ptr[nj][1 - j]; int val = cur + cnt; if (dp[nj][1 - j] < val) dp[nj][1 - j] = val; else break; //cerr << "dp == " << dp[nj][1 - j] << endl; //mx[1 - j] = } if (start[i][1 - j] >= i) { dp[maxSuf[1 - j]][1 - j] = max(dp[maxSuf[1 - j]][1 - j], cur + start[i][1 - j]); } } forn(i, N) ans[i] = INF; forn(i, N) forn(j, 2) { int cur = dp[i][j]; //assert(cur <= dp[n][1 - j]); //int cur2 = dp[i][j]; //cerr << "i j == " << i << ' ' << j << endl; //cerr << "cur cur2 == " << cur << ' ' << cur2 << endl; //assert(cur == cur2); if (cur != -1) ans[cur] = min(ans[cur], i); } for (int i = m - 1; i >= 1; i--) ans[i] = min(ans[i], ans[i + 1]); forn(i, N) if (ans[i] == INF) ans[i] = -1; forn1(i, m) { if (i > 1) putchar(' '); printf("%d", ans[i]); } puts(""); } //#undef _DEBUG int main() { #ifdef _DEBUG assert(freopen("777.txt", "rt", stdin)); assert(freopen("output.txt", "wt", stdout)); #endif cout << setprecision(10) << fixed; cerr << setprecision(10) << fixed; int T = 1; srand(int(time(NULL))); assert(scanf("%d", &T) == 1); forn(i, T) { #ifdef _DEBUG cerr << "TEST == " << i << endl; #endif assert(read()); //cout << "Case #" << i + 1 << ": "; solve(); //if(i == 1) break; #ifdef _DEBUG cerr << "curTime == " << clock() << " ms" << endl; #endif } #ifdef _DEBUG cerr << "TIME == " << clock() << " ms" << endl; #endif return 0; }