fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define endl '\n'
  5. #define int long long
  6.  
  7. const int N = 2e5, oo = 2e18, MOD = 1e9+7;
  8.  
  9.  
  10. void solve() {
  11. int n, l, r; cin >> n >> l >> r;
  12. l--, r--;
  13. string s; cin >> s;
  14. deque<char> a[3];
  15. for (int i = 0; i < l; i++) a[0].push_back(s[i]);
  16. for (int i = l; i <= r; i++) a[1].push_back(s[i]);
  17. for (int i = r+1; i < n; i++) a[2].push_back(s[i]);
  18. bool flag = 0;
  19. int q; cin >> q;
  20. auto front = [&] {
  21. return (flag ? a[1].back(): a[1].front());
  22. };
  23. auto push_front = [&](char c) {
  24. (flag ? a[1].push_back(c): a[1].push_front(c));
  25. };
  26. auto pop_front = [&] {
  27. (flag ? a[1].pop_back(): a[1].pop_front());
  28. };
  29. auto back = [&] {
  30. return (flag ? a[1].front(): a[1].back());
  31. };
  32. auto pop_back = [&] {
  33. (flag ? a[1].pop_front(): a[1].pop_back());
  34. };
  35. auto push_back = [&](char c) {
  36. (flag ? a[1].push_front(c): a[1].push_back(c));
  37. };
  38. while (q--) {
  39. char t; cin >> t;
  40. if (t == 'S') {
  41. char b, c; cin >> b >> c;
  42. if (b == 'L' && c == 'L') {
  43. char c = a[0].back();
  44. a[0].pop_back();
  45. push_front(c);
  46. } else if (b == 'L' && c == 'R') {
  47. char c = front();
  48. pop_front();
  49. a[0].push_back(c);
  50. } else if (b == 'R' && c == 'L') {
  51. char c = back();
  52. pop_back();
  53. a[2].push_front(c);
  54. } else if (b == 'R' && c == 'R') {
  55. char c = a[2].front();
  56. a[2].pop_front();
  57. push_back(c);
  58. }
  59. } else if (t == 'R') {
  60. flag ^= 1;
  61. } else {
  62. char c; cin >> c;
  63. if (c == 'L') cout << front();
  64. else cout << back();
  65. }
  66. }
  67. }
  68.  
  69.  
  70. signed main() {
  71. ios_base::sync_with_stdio(false);
  72. cin.tie(NULL); cout.tie(NULL);
  73. // #ifndef ONLINE_JUDGE
  74. // freopen("input.txt", "r", stdin);
  75. // freopen("output.txt", "w", stdout);
  76. // #endif
  77. int t; t = 1;
  78. // cin >> t;
  79. while (t--) solve();
  80. return 0;
  81. }
  82.  
Success #stdin #stdout 0.01s 5316KB
stdin
Standard input is empty
stdout
Standard output is empty