Problem:
Doubly Linked List

Your task is to implement a double linked list.

Write a program which performs the following operations:

insert x: insert an element with key x into the front of the list.

delete x: delete the first element which has the key of x from the list. If there is not such element, you need not do anything.

deleteFirst: delete the first element from the list.

deleteLast: delete the last element from the list.

Input

The input is given in the following format:

n

command1

command2

...

commandn

In the first line, the number of operations n is given. In the following n lines, the above mentioned operations are given in the following format:

insert x

delete x

deleteFirst

deleteLast

Output

Print all the element (key) in the list after the given operations. Two consequtive keys should be separated by a single space.

Constraints

The number of operations ≤ 2,000,000

The number of delete operations ≤ 20

0 ≤ value of a key ≤ 109

The number of elements in the list does not exceed 106

For a delete, deleteFirst or deleteLast operation, there is at least one element in the list.

Sample Input 1

7
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5

Sample Output 1

6 1 2

Sample Input 2

9
insert 5
insert 2
insert 3
insert 1
delete 3
insert 6
delete 5
deleteFirst
deleteLast

Sample Output 2

1

Solution in Haskell (judge verdict: Time Limit Exceeded)

import Control.Applicative
import Data.Array.IO

main :: IO ()
main = do
  n <- read <$> getLine :: IO Int
  key  <- newArray(0, 2100000) 0 :: IO(IOUArray Int Int)
  next <- newArray(0, 2100000) 0 :: IO(IOUArray Int Int)
  prev <- newArray(0, 2100000) 0 :: IO(IOUArray Int Int)
  solve n key next prev

solve :: Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
solve n key next prev = do
  if n == 0 then
    do {
      printanswer key next prev;
      return ();
    }
    else do {
    ss <- words <$> getLine;
    process n ss key next prev;
    solve (n - 1) key next prev;
    }

process :: Int -> [String] -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO()
process n ss key next prev
  | command == "insert" = insertx n x key next prev
  | command == "delete" = deletex n x key next prev
  | command == "deleteFirst" = deleteFirst n key next prev
  | command == "deleteLast" = deleteLast n key next prev
  where command = ss !! 0
        x = (read (ss !! 1) :: Int)

printanswer :: IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
printanswer key next prev = do
  t <- readArray next 0;
  if t == 0 then do {
    return ();
    }
    else do {
    nextindex <- (readArray next 0);
    x <- readArray key nextindex;
    nextnextindex <- readArray next nextindex;
    putStr (show x);
    printanswer2 nextnextindex key next prev;
    }

printanswer2 :: Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
printanswer2 index key next prev = do
  if index == 0 then
    do {
      putStrLn "";
      return ();
    }
    else do {
    x <- readArray key index;
    putStr (" " ++ (show x));
    nextindex <- (readArray next index);
    printanswer2 nextindex key next prev;
    }

insertx :: Int -> Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
insertx n x key next prev = do
  nextnext <- (readArray next 0);
  writeArray key (n + 1) x;
  writeArray next (n + 1) nextnext;
  writeArray prev (n + 1) 0;
  writeArray prev nextnext (n + 1);
  writeArray next 0 (n + 1);  
  return ();

deletex :: Int -> Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
deletex n x key next prev = do
  nextnext <- readArray next 0;
  deletex2 n nextnext x key next prev;
  return ();

deletex2 :: Int -> Int -> Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
deletex2 n index x key next prev = do
  t <- readArray key index;
  if index == 0 then
    do {
      return ();
      }
    else do {
    if t == x then
      do {
        pn <- readArray prev index;
        nn <- readArray next index;
        writeArray next pn nn;
        writeArray prev nn pn;
        return ();
        }
      else do {
      nextindex <- readArray next index;
      deletex2 n nextindex x key next prev;
      }
    }

deleteFirst :: Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
deleteFirst n key next prev = do
  nextindex <- readArray next 0;
  nextnextindex <- readArray next nextindex;
  writeArray next 0 nextnextindex;
  writeArray prev nextnextindex 0;

deleteLast :: Int -> IOUArray Int Int -> IOUArray Int Int -> IOUArray Int Int -> IO ()
deleteLast n key next prev = do
  previndex <- readArray prev 0;
  prevprevindex <- readArray prev previndex;
  writeArray prev 0 prevprevindex;
  writeArray next prevprevindex 0;


  
        
{--
solve :: Int -> IO(IOUArray Int Int) -> IO(IOUArray Int Int) -> IO(IOUArray Int Int) -> IO ()
solve n key next prev = do
  if n == 0 then
    do {
      printresult key next prev;
      return ();
    }
    else do {
    ss <- words <$> getLine;    
    }
--}

{--
process :: Int -> IO(IOUArray Int Int) -> IO(IOUArray Int Int) -> IO(IOUArray Int Int) -> [String] -> IO ()
process key next prev ss
  | ss !! 0 == "insert" = (read (ss !! 1) :: Int) : xs
  | ss !! 0 == "delete" = deletex 0 (read (ss !! 1) :: Int) xs
  | ss !! 0 == "deleteFirst" = tail xs
  | ss !! 0 == "deleteLast" = init xs
--}

{--
insertx2 :: Int -> Int-> IO(IOUArray Int Int) -> IO(IOUArray Int Int) -> IO(IOUArray Int Int) -> IO()
insertx2 :: n i key next prev = ()
  --}