// Miroslaw #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long LL; typedef vector VI; #define REP(i,n) for(int i=0;i<(n);i++) #define FOR(i,a,b) for(int i=(a);i<=(b);i++) #define FORD(i,a,b) for(int i=(a);i>=(b);i--) #define FOREACH(i,c) for(__typeof((c).begin())i = (c).begin();i!=(c).end(); ++i) int cond = 1; #define DB(X) {if(cond){cerr<<"Line:"<<__LINE__<<", "<<#X<<" = "< type; int n, m; static const int MAXN = 50123; #define SZ 220 int GetBucket(int poz) { return poz / SZ; } int PoczBucket(int bucket) { return bucket * SZ; } int KonBucket(int bucket) { return min((bucket + 1) * SZ - 1, m); } struct EE { EE(int m) : v(m + 1, -1), bucket_add(2 + m / SZ), bucket_max(2 + m / SZ, -1000000000) {} int GetMax() { int ma = 0; for (int i = 0; i < bucket_max.size(); ++i) { ma = max(ma, bucket_max[i]);// + bucket_add[i]); } return ma; } void Prop(int b) { bucket_max[b] = -1000000000; FOR(i, PoczBucket(b), KonBucket(b)) { if (v[i] != -1) { v[i] += bucket_add[b]; bucket_max[b] = max(bucket_max[b], v[i]); } } bucket_add[b] = 0; } void SetValue(int i, int val) { int b = GetBucket(i); Prop(b); v[i] = val; bucket_max[b] = -1000000000; FOR(x, PoczBucket(b), KonBucket(b)) { bucket_max[b] = max(bucket_max[b], v[x]); } } void Increase(int i) { int b = GetBucket(i); REP(i, b) { bucket_add[i]++; bucket_max[i]++; } Prop(b); FOR(j, PoczBucket(b), i) if (v[j]!= -1) { v[j]++; bucket_max[b] = max(bucket_max[b], v[j]); } } vector v; vector bucket_add; vector bucket_max; }; struct Hand { Hand() : ee(m) { REP(i, MAXN) a[i] = 0; } void setValue(int i, int val) { mm[i] = val; ee.SetValue(i, val); } int getbest() { #if 0 int ma = -1000000000; for (const auto& x : mm) ma = max(ma, x.second); if (ma != ee.GetMax()) { DB3("------------> ", ma, ee.GetMax()); } return ma; #endif return ee.GetMax(); } void increase(int i) { ee.Increase(i); #if 0 for (auto& e : mm) { if (e.first <= i) { e.second++; } } #endif } void append(int i, int val) { increase(i); if (!pocz.count(i)) { setValue(i, 1 + GetGreaterEqualThan(i) + val); } pocz[i]++; add(i, 1); } int GetGreaterEqualThan(int i) { int b1= sum(m) - sum(i - 1); return b1; } void add(int n, int x) { for (; n < MAXN; n |= n + 1) a[n] += x; } // Returns value[0] + value[1] + ... + value[n] int sum(int n) { int s=0; while (n>=0) { s+=a[n]; n=(n&(n+1))-1; } return s; } EE ee; map mm; map pocz; int a[MAXN]; }; void doit() { scanf("%d%d", &m, &n); pocz.clear(); kon.clear(); type.clear(); pocz.resize(n); kon.resize(n); type.resize(n); char c; for (int i = 0; i < n; ++i) { scanf("%*c%c", &c); type[i] = (c == 'D' || c == 'M'); } for (int i = 0; i < n; ++i) scanf("%d", &pocz[i]); for (int i = 0; i < n; ++i) scanf("%d", &kon[i]); vector> span[2]; REP(i, n) { if (pocz[i] > kon[i]) continue; span[type[i]].push_back({pocz[i], kon[i]}); } vector> koniec[2]; REP(t, 2) { koniec[t].resize(m + 1); for (const auto& e : span[t]) { koniec[t][e.second].push_back(e.first); } } vector best(m + 1, -1); best[0] = 0; map pocz[2]; Hand hh[2]; FOR(i, 1, m) { best[i] = best[i - 1]; REP(t, 2) { if (koniec[t][i].empty()) { continue; } for (const auto& el : koniec[t][i]) { hh[t].append(el, best[el]); } best[i] = max(best[i], hh[t].getbest()); } } vector ans(n + 1, -1); REP(i, best.size()) { if (best[i] != -1 && ans[best[i]] == -1) ans[best[i]] = i; } FORD(i, (int)ans.size() - 2, 0) { if (ans[i] == -1) ans[i] = ans[i + 1]; } for (int i = 1; i < ans.size(); ++i) { printf("%d ", ans[i]); } printf("\n"); } int main() { int T; scanf("%d", &T); while(T--) doit(); return 0; }