#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; int N,Q; string S; int main() { cin >> N >> Q >> S; vector hsum(2*N+1,0), wsum(2*N+1,0); for(int i = 0; i < 2*N; ++i) { hsum[i+1] = hsum[i]; wsum[i+1] = wsum[i]; if(S[i%N] == 'D') hsum[i+1]++; else wsum[i+1]++; } int hcnt = hsum[N]; int wcnt = wsum[N]; while(Q--) { int H, W, P; cin >> H >> W >> P; ll h = 1e9; ll w = 1e9; if(hcnt != 0) { ll a = (H-1)/hcnt*N; int b = lower_bound(hsum.begin(),hsum.end(),hsum[P]+H-(H-1)/hcnt*hcnt)-hsum.begin(); b -= P; h = a+b; // cout << a << " " << b << endl; } if(wcnt != 0) { ll a = (W-1)/wcnt*N; int b = lower_bound(wsum.begin(),wsum.end(),wsum[P]+W-(W-1)/wcnt*wcnt)-wsum.begin(); b -= P; w = a+b; // cout << a << " " << b << endl; } // cout << "h w :" << h << " " << w << endl; ll mn = min(h,w)-1; cout << (mn+P+1)%N << endl; } }