1
+ #include < bits/stdc++.h>
2
+ using namespace std ;
3
+
4
+ vector<pair<int , int >> merge (vector<pair<int , int >> must) {
5
+ vector<pair<int , int >> ans;
6
+ sort (must.begin (), must.end ());
7
+ if (must.empty ()) return ans;
8
+ int curl = must[0 ].first , curr = must[1 ].second ;
9
+ for (auto [l, r] : must) {
10
+ if (l > curr + 1 )
11
+ ans.push_back ({curl, curr}), curl = l, curr = r;
12
+ else
13
+ curr = max (curr, r);
14
+ }
15
+ ans.push_back ({curl, curr});
16
+ return ans;
17
+ }
18
+
19
+ void solve () {
20
+ int n, q;
21
+ cin >> n >> q;
22
+ vector<int > a (n);
23
+ for (auto &i : a) cin >> i;
24
+ string s;
25
+ cin >> s;
26
+
27
+ vector<pair<int , int >> must;
28
+ for (int i = 0 ; i < n; i++)
29
+ if (a[i] != i + 1 )
30
+ must.push_back ({min (a[i] - 1 , i), max (a[i] - 1 , i) - 1 });
31
+ must = merge (must);
32
+
33
+ auto needindex = [&](int idx) {
34
+ auto it = lower_bound (must.begin (), must.end (), make_pair (idx, idx));
35
+ if (it == must.end () || it->first > idx) {
36
+ if (it == must.begin ()) return false ;
37
+ it--;
38
+ if (it->second < idx)
39
+ return false ;
40
+ else
41
+ return true ;
42
+ } else {
43
+ return true ;
44
+ }
45
+ };
46
+
47
+ set<int > toremove;
48
+ for (int i = 0 ; i < n - 1 ; i++) {
49
+ if (s[i] == ' L' && s[i + 1 ] == ' R' && needindex (i)) toremove.insert (i);
50
+ }
51
+
52
+ for (int i = 0 ; i < q; i++) {
53
+ int idx;
54
+ cin >> idx;
55
+ idx--;
56
+ s[idx] = s[idx] == ' L' ? ' R' : ' L' ;
57
+
58
+ if (idx + 1 < n)
59
+ if (s[idx] == ' L' && s[idx + 1 ] == ' R' && needindex (idx))
60
+ toremove.insert (idx);
61
+ else
62
+ toremove.erase (idx);
63
+
64
+ if (idx - 1 >= 0 )
65
+ if (s[idx - 1 ] == ' L' && s[idx] == ' R' && needindex (idx - 1 ))
66
+ toremove.insert (idx - 1 );
67
+ else
68
+ toremove.erase (idx - 1 );
69
+
70
+ cout << (toremove.empty () ? " YES" : " NO" ) << " \n " ;
71
+ }
72
+ }
73
+
74
+ int main () {
75
+ ios::sync_with_stdio (false );
76
+ cin.tie (0 );
77
+ cout.tie (0 );
78
+
79
+ int t = 1 ;
80
+ cin >> t;
81
+ while (t--) solve ();
82
+ }
0 commit comments