#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
//#pragma GCC target ("avx2")
//#pragma GCC optimize ("O3")
//#pragma GCC optimize ("unroll-loops")
//#pragma comment (linker, "/STACK: 16777216")
#define f first
#define s second
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define sz(x) ((int)(x).size())
#define pb push_back
#define mp make_pair
#define int long long
using namespace std;
using namespace __gnu_pbds;
template <typename T> inline bool umax(T &a, const T &b) { if(a < b) { a = b; return 1; } return 0; }
template <typename T> inline bool umin(T &a, const T &b) { if(a > b) { a = b; return 1; } return 0; }
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
template <typename T> using oset = tree<T, null_type, less <T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll mod = 998244353;
const ll base = 1e6 + 5;
const ll inf = 1e18;
const int MAX = 1e6;
const int lg = 20;
random_device rd;
mt19937 gen(rd());
uniform_int_distribution<ll> dis(1, inf);
const int buben = 1000;
void solve() {
int n, q;
cin >> n >> q;
int nb = (n + buben - 1) / buben;
vector<deque<char>> dq(nb + 1);
string s;
cin >> s;
vector<int> bl(n), lb(nb), rb(nb);
for(int i = 0; i < n; i++) {
bl[i] = i / buben;
lb[bl[i]] = bl[i] * buben;
rb[bl[i]] = min(lb[bl[i]] + buben - 1, n - 1);
}
for(int i = 0; i < n; i++) {
dq[bl[i]].pb(s[i]);
}
auto norm = [&]() {
for(int i = 0; i < nb; i++) {
int sz = rb[bl[i]] - lb[bl[i]];
while(sz(dq[i]) > sz) {
dq[i + 1].push_front(dq[i].back());
dq[i].pop_back();
}
while(sz(dq[i]) < sz) {
dq[i].pb(dq[i + 1].front());
dq[i + 1].pop_front();
}
}
};
while(q--) {
char t;
cin >> t;
if(t == 'a') {
int i, j;
cin >> i >> j;
i--, j--;
int idx = 0;
while(idx < nb && i - sz(dq[idx]) >= 0) i -= sz(dq[idx++]);
char add = dq[idx][i];
dq[idx].erase(dq[idx].begin() + i);
idx = 0;
while(idx < nb && j - sz(dq[idx]) >= 0) j -= sz(dq[idx++]);
dq[idx].insert(dq[idx].begin() + j, add);
norm();
}
else {
int i;
cin >> i;
i--;
int idx = 0;
while(i - sz(dq[idx]) >= 0) i -= sz(dq[idx++]);
cout << dq[idx][i] << '\n';
}
}
}
signed main() {
// freopen("collider.in", "r", stdin); freopen("collider.out", "w", stdout);
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int ttt = 1;
// cin >> ttt;
while(ttt--) {
solve();
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
3 ms |
444 KB |
Output is correct |
3 |
Correct |
6 ms |
1576 KB |
Output is correct |
4 |
Correct |
41 ms |
9604 KB |
Output is correct |
5 |
Correct |
52 ms |
9660 KB |
Output is correct |
6 |
Correct |
70 ms |
10860 KB |
Output is correct |
7 |
Correct |
76 ms |
12012 KB |
Output is correct |
8 |
Correct |
41 ms |
11852 KB |
Output is correct |
9 |
Correct |
89 ms |
11992 KB |
Output is correct |
10 |
Correct |
67 ms |
11972 KB |
Output is correct |