fork(1) download
  1. #include <bits/stdc++.h>
  2. #define pi 3.14159265359
  3. using namespace std;
  4.  
  5. int main() {
  6. int N,L,R,M;
  7. string s;
  8. cin >> N >> L >> R >> s >> M;
  9. L--, R--;
  10. deque<char> Dl,Dr,Dc;
  11. for(int i =0; i < L; i++) Dl.push_back(s[i]);
  12. for(int i =L; i <= R; i++) Dc.push_back(s[i]);
  13. for(int i =R+1; i < s.length(); i++) Dr.push_back(s[i]);
  14. bool inv =false;
  15. for(int i =0; i < M; i++) {
  16. string t,s1,s2;
  17. cin >> t;
  18. if(t == "Q") {
  19. cin >> s1;
  20. if((s1 == "L")^inv) cout << Dc.front();
  21. else cout << Dc.back();
  22. continue;}
  23. if(t == "S") {
  24. cin >> s1 >> s2;
  25. if(!inv) {
  26. if(s1 == "L") {
  27. if(s2 == "L") {Dc.push_front(Dl.back()); Dl.pop_back();}
  28. else {Dl.push_back(Dc.front()); Dc.pop_front();}
  29. }
  30. else {
  31. if(s2 == "L") {Dr.push_front(Dc.back()); Dc.pop_back();}
  32. else {Dc.push_back(Dr.front()); Dr.pop_front();}
  33. }
  34. }
  35. else {
  36. if(s1 == "L") {
  37. if(s2 == "L") {Dc.push_back(Dl.back()); Dl.pop_back();}
  38. else {Dl.push_back(Dc.back()); Dc.pop_back();}
  39. }
  40. else {
  41. if(s2 == "L") {Dr.push_front(Dc.front()); Dc.pop_front();}
  42. else {Dc.push_front(Dr.front()); Dr.pop_front();}
  43. }
  44. }
  45. continue;}
  46. inv =1-inv;}
  47. return 0;}
Success #stdin #stdout 0s 3484KB
stdin
11 2 6
abracadabra
12
Q L
Q R
R
Q L
Q R
S L R
S R R
Q L
Q R
R
Q L
Q R
stdout
baabcddc