#include #include #include #include #define FOR(i,n) for(int i=0, n_=(n); i VI; class LazySegmentTree { private: int n; VI t, d; void apply(int p, int v) { t[p] += v; if (p < n) d[p] += v; } void build(int p) { while (p > 1) p >>= 1, t[p] = max(t[p<<1], t[p<<1|1]) + d[p]; } void push(int p) { int h = sizeof(int) * 8 - __builtin_clz(n); for (int s = h; s > 0; --s) { int i = p >> s; if (d[i] != 0) { apply(i<<1, d[i]); apply(i<<1|1, d[i]); d[i] = 0; } } } public: LazySegmentTree(int n_): n(n_), t(2*n_+2), d(n_+1) { } void inc(int l, int r, int v) { l += n, r += n; int l0 = l, r0 = r; for (; l < r; l >>= 1, r >>= 1) { if (l&1) apply(l++, v); if (r&1) apply(--r, v); } build(l0); build(r0 - 1); } int query(int l, int r) { l += n, r += n; push(l); push(r - 1); int res = -2e9; for (; l < r; l >>= 1, r >>= 1) { if (l&1) res = max(res, t[l++]); if (r&1) res = max(t[--r], res); } return res; } }; VI dp(const VI &s, const VI &d, const vector &t, int m, int n) { VI C(m+3); vector< map > d_lookup(2); FOR(i, d.size()) if(d[i] >= s[i]) { map::iterator it = d_lookup[t[i]].find(d[i]); if(it == d_lookup[t[i]].end()) d_lookup[t[i]][d[i]] = VI(1, s[i]); else it->second.push_back(s[i]); } LazySegmentTree tree[2] = {LazySegmentTree(m+3), LazySegmentTree(m+3)}; for(int j=1; j<=m; j++) { FOR(jt, 2) { map::iterator it = d_lookup[jt].find(j); if(it != d_lookup[jt].end()) { for(int js: it->second) if(js <= j) { tree[jt].inc(1, js+1, 1); } } } C[j] = max(tree[0].query(1,j+1), tree[1].query(1,j+1)); if(j>1 && C[j]>T; FOR(testcase, T) { int m, n; cin>>m>>n; vector t(n); VI s(n), d(n); char animal; FOR(i, n) { cin>>animal; t[i] = (animal=='E') || (animal=='C'); } FOR(i, n) cin>>s[i]; FOR(i, n) cin>>d[i]; VI x = dp(s,d,t,m,n); FOR(i, n) cout<