{-# LANGUAGE BangPatterns #-} {-# LANGUAGE BinaryLiterals #-} {-# LANGUAGE CPP #-} {-# LANGUAGE DefaultSignatures #-} {-# LANGUAGE DeriveDataTypeable #-} {-# LANGUAGE DerivingVia #-} {-# LANGUAGE DerivingStrategies #-} {-# LANGUAGE FlexibleContexts #-} {-# LANGUAGE FlexibleInstances #-} {-# LANGUAGE GeneralizedNewtypeDeriving #-} {-# LANGUAGE KindSignatures #-} {-# LANGUAGE LambdaCase #-} {-# LANGUAGE MagicHash #-} {-# LANGUAGE MultiParamTypeClasses #-} {-# LANGUAGE MultiWayIf #-} {-# LANGUAGE NumericUnderscores #-} {-# LANGUAGE OverloadedStrings #-} {-# LANGUAGE RankNTypes #-} {-# LANGUAGE RecordWildCards #-} {-# LANGUAGE TupleSections #-} {-# LANGUAGE TypeApplications #-} {-# LANGUAGE TypeFamilies #-} {-# LANGUAGE UnboxedTuples #-} {-# LANGUAGE UnicodeSyntax #-} {-# LANGUAGE ViewPatterns #-} import Control.Monad import Control.Monad.Cont import Control.Monad.Fix import Control.Monad.ST import Control.Monad.State import Data.Bits import Data.Bool import Data.Char import Data.Coerce import qualified Data.Foldable as F import qualified Data.List as L import Data.IORef import Data.STRef import Data.Word import GHC.Exts import System.IO import Unsafe.Coerce import qualified Data.ByteString as BS import qualified Data.ByteString.Builder as BSB import qualified Data.ByteString.Char8 as BSC8 import qualified Data.ByteString.Unsafe as BSU import qualified Data.Vector as V import qualified Data.Vector.Fusion.Stream.Monadic as VFSM import qualified Data.Vector.Generic as VG import qualified Data.Vector.Generic.Mutable as VGM import qualified Data.Vector.Mutable as VM import qualified Data.Vector.Unboxed as VU import qualified Data.Vector.Unboxed.Mutable as VUM main :: IO () main = do [n, m] <- map (read @Int) . words <$> getLine ax <- seqInput' n sx <- seqInput m cnt <- VUM.replicate (n + 2 * m + 10) (0 :: Int) cur <- newIORef (m + 5) rep n $ \i -> VUM.unsafeWrite cnt (m + 5 + i) (ax VU.! i) rep m $ \i -> do cu <- readIORef cur if sx VU.! i == 'L' then do item <- VUM.unsafeRead cnt cu VUM.unsafeModify cnt (+ item) (cu + 1) modifyIORef' cur succ else do item <- VUM.unsafeRead cnt (cu + n - 1) VUM.unsafeModify cnt (+ item) (cu + n - 2) VUM.unsafeWrite cnt (cu + n - 1) 0 modifyIORef' cur pred rep n $ \i -> do when (i /= 0) $ putStr " " cu <- readIORef cur ans <- VUM.unsafeRead cnt (cu + i) putStr $ show ans putStr "\n" type CParser a = StateT BSC8.ByteString Maybe a runCParser :: CParser a -> BSC8.ByteString -> Maybe (a, BSC8.ByteString) runCParser = runStateT {-# INLINE runCParser #-} int :: CParser Int int = coerce $ BSC8.readInt . BSC8.dropWhile isSpace {-# INLINE int #-} char :: CParser Char char = coerce BSC8.uncons {-# INLINE char #-} seqInput :: Int -> IO (VU.Vector Char) seqInput n = VU.unfoldrN n (runCParser char) <$> BSC8.getLine {-# INLINE seqInput #-} seqInput' :: Int -> IO (VU.Vector Int) seqInput' n = VU.unfoldrN n (runCParser int) <$> BSC8.getLine {-# INLINE seqInput' #-} stream :: Monad m => Int -> Int -> VFSM.Stream m Int stream !l !r = VFSM.Stream step l where step x | x < r = return $ VFSM.Yield x (x + 1) | otherwise = return $ VFSM.Done {-# INLINE [0] step #-} {-# INLINE [1] stream #-} rep :: Monad m => Int -> (Int -> m ()) -> m () rep n = flip VFSM.mapM_ (stream 0 n) {-# INLINE rep #-}