There are n frogs sitting on the coordinate axis Ox. For each frog two values xi, ti are known — the position and the initial length of the tongue of the i-th frog (it is guaranteed that all positions xi are different). m mosquitoes one by one are landing to the coordinate axis. For each mosquito two values are known pj — the coordinate of the position where the j-th mosquito lands and bj — the size of the j-th mosquito. Frogs and mosquitoes are represented as points on the coordinate axis.The frog can eat mosquito if mosquito is in the same position with the frog or to the right, and the distance between them is not greater than the length of the tongue of the frog.If at some moment several frogs can eat a mosquito the leftmost frog will eat it (with minimal xi). After eating a mosquito the length of the tongue of a frog increases with the value of the size of eaten mosquito. It's possible that after it the frog will be able to eat some other mosquitoes (the frog should eat them in this case).For each frog print two values — the number of eaten mosquitoes and the length of the tongue after landing all mosquitoes and after eating all possible mosquitoes by frogs.Each mosquito is landing to the coordinate axis only after frogs eat all possible mosquitoes landed before. Mosquitoes are given in order of their landing to the coordinate axis.

First line contains two integers n, m (1 ≤ n, m ≤ 2·105) — the number of frogs and mosquitoes. Each of the next n lines contains two integers xi, ti (0 ≤ xi, ti ≤ 109) — the position and the initial length of the tongue of the i-th frog. It is guaranteed that all xi are different. Next m lines contain two integers each pj, bj (0 ≤ pj, bj ≤ 109) — the position and the size of the j-th mosquito.

Print n lines. The i-th line should contain two integer values ci, li — the number of mosquitoes eaten by the i-th frog and the length of the tongue of the i-th frog.

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es
              where
                es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
                fs = sort $ zip frogsIn [1..]

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing =  process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) = out ++ (process frogs'' rest'' mosquitos)
              where
                (frog', rest'') = eat frog rest' mosqs
                  where
                    rest' = mosquito `Set.insert` rest

                    mosqs = mosq:(Set.elems . snd $ Set.split mosq rest')
                      where
                        mosq = fromJust $ Set.lookupGE (Mosq x 0 0) rest'

                    eat frog@(Frog x y i c t) rest mosqs
                      | null mosqs || y < p = (frog, rest)
                      | otherwise = eat frog' rest' $ tail mosqs
                      where
                        mosq@(Mosq p b _) = head mosqs
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    Frog _ y _ _ _ = frog'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sortBy (comparing (fst . fst)) (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup = process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise = out ++ (process frogs'' rest' mosquitos)
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O3 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

import Control.Monad (when)
import Data.Array (elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs -- :: ST s (STArray s Int (Int, Int))
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        s <- readArray a $ (l + r) `div` 2

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort gen (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random
import System.IO hiding (getContents, getLine)

import Data.ByteString.Lazy.Builder  -- requires bytestring-0.10.x
import Data.ByteString.Lazy.Builder.ASCII -- omit for bytestring-0.10.2.x
import Data.Monoid

data Frog = Frog { frogX :: !Int, frogY :: !Int, frogI :: !Int, frogC :: !Int, frogT :: !Int } deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        g <- readSTRef gr
        let (x, g') = randomR (l, r-1) g
        writeSTRef gr g'

        s <- readArray a x

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            z = zipWith (\frog y -> frog { frogX = max (frogX frog) (y+1) }) fs ys
              where
                fs = sort gen $ zipWith (\(x, t) i -> Frog x (x+t) i 0 t) frogsIn [1..]
                ys = scanl max (-1) $ map frogY fs

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing = process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) =  out ++ (process frogs'' rest' mosquitos)
              where
                (frog', rest') = eat frog rest (mosquito:mosqs)
                  where
                    frog@(Frog x _ _ _ _) = fromJust lookup

                    mosqs = (case lookup of
                                    Nothing -> []
                                    Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                            )
                      where lookup = Set.lookupGE (Mosq x 0 0) rest

                    eat frog@(Frog x y i c t) rest mosqs
                      | null mosqs || y < p = (frog, rest)
                      | otherwise = eat frog' rest' $ tail mosqs
                      where
                        mosq@(Mosq p b _) = head mosqs
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    Frog _ y _ _ _ = frog'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  let a = elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result

  let unlines_ = mconcat . map (<> charUtf8 '\n')
  hPutBuilder stdout $ unlines_ $ map (\(c, t) -> intDec c <> charUtf8 ' ' <> intDec t) a

{-# OPTIONS_GHC -O3 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy, sort)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random
import System.IO hiding (getContents, getLine)

import Data.ByteString.Lazy.Builder  -- requires bytestring-0.10.x
import Data.ByteString.Lazy.Builder.ASCII -- omit for bytestring-0.10.2.x
import Data.Monoid

data Frog = Frog { frogX :: !Int, frogY :: !Int, frogI :: !Int, frogC :: !Int, frogT :: !Int } deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort2 gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        g <- readSTRef gr
        let (x, g') = randomR (l, r-1) g
        writeSTRef gr g'

        s <- readArray a x

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            z = zipWith (\frog y -> frog { frogX = max (frogX frog) (y+1) }) fs ys
              where
                fs = sort $ zipWith (\(x, t) i -> Frog x (x+t) i 0 t) frogsIn [1..]
                ys = scanl max (-1) $ map frogY fs

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing = process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) =  out ++ (process frogs'' rest' mosquitos)
              where
                (frog', rest') = eat frog rest (mosquito:mosqs)
                  where
                    frog@(Frog x _ _ _ _) = fromJust lookup

                    mosqs = (case lookup of
                                    Nothing -> []
                                    Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                            )
                      where lookup = Set.lookupGE (Mosq x 0 0) rest

                    eat frog@(Frog x y i c t) rest mosqs
                      | null mosqs || y < p = (frog, rest)
                      | otherwise = eat frog' rest' $ tail mosqs
                      where
                        mosq@(Mosq p b _) = head mosqs
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    Frog _ y _ _ _ = frog'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  let a = elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result

  let unlines_ = mconcat . map (<> charUtf8 '\n')
  hPutBuilder stdout $ unlines_ $ map (\(c, t) -> intDec c <> charUtf8 ' ' <> intDec t) a

{-# OPTIONS_GHC -O3 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

import Control.Monad (when)
import Data.Array (elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs -- :: ST s (STArray s Int (Int, Int))
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        s <- readArray a $ (l + r) `div` 2

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort gen (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromDistinctAscList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)
  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog { frogX :: !Int, frogY :: !Int, frogI :: !Int, frogC :: !Int, frogT :: !Int } deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        g <- readSTRef gr
        let (x, g') = randomR (l, r-1) g
        writeSTRef gr g'

        s <- readArray a x

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            z = zipWith (\frog y -> frog { frogX = max (frogX frog) (y+1) }) fs ys
              where
                fs = sort gen $ zipWith (\(x, t) i -> Frog x (x+t) i 0 t) frogsIn [1..]
                ys = scanl max (-1) $ map frogY fs

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing = process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) =  out ++ (process frogs'' rest' mosquitos)
              where
                (frog', rest') = eat frog rest (mosquito:mosqs)
                  where
                    frog@(Frog x _ _ _ _) = fromJust lookup

                    mosqs = (case lookup of
                                    Nothing -> []
                                    Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                            )
                      where lookup = Set.lookupGE (Mosq x 0 0) rest

                    eat frog@(Frog x y i c t) rest mosqs
                      | null mosqs || y < p = (frog, rest)
                      | otherwise = eat frog' rest' $ tail mosqs
                      where
                        mosq@(Mosq p b _) = head mosqs
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    Frog _ y _ _ _ = frog'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  putStr . unlines . map (\(c, t) -> show c ++ " " ++ show t) . elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result

{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        g <- readSTRef gr
        let (x, g') = randomR (l, r-1) g
        writeSTRef gr g'

        s <- readArray a x

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort gen (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing = process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) =  out ++ (process frogs'' rest' mosquitos)
              where
                (frog', rest') = eat frog rest (mosquito:mosqs)
                  where
                    frog@(Frog x _ _ _ _) = fromJust lookup

                    mosqs = (case lookup of
                                    Nothing -> []
                                    Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                            )
                      where lookup = Set.lookupGE (Mosq x 0 0) rest

                    eat frog@(Frog x y i c t) rest mosqs
                      | null mosqs || y < p = (frog, rest)
                      | otherwise = eat frog' rest' $ tail mosqs
                      where
                        mosq@(Mosq p b _) = head mosqs
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    Frog _ y _ _ _ = frog'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  putStr . unlines . map (\(c, t) -> show c ++ " " ++ show t) . elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result

{-# OPTIONS_GHC -O3 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs -- :: ST s (STArray s Int (Int, Int))
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        s <- readArray a $ (l + r) `div` 2

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort gen (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(c, t) -> show c ++ " " ++ show t) . elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result

{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs -- :: ST s (STArray s Int (Int, Int))
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        g <- readSTRef gr
        let (x, g') = randomR (l, r-1) g
        writeSTRef gr g'

        s <- readArray a x

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max (-1) $ map (\((x, t), _) -> x + t) fs
            fs = sort gen (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(c, t) -> show c ++ " " ++ show t) . elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max 0 $ map (\((x, t), _) -> x + t) fs
            fs = sort (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x s) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog y y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max 0 $ map (\((x, t), _) -> x + t) fs
            fs = sort (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x s) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y > y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y > x2 = ((Frog y y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max 0 $ map (\((x, t), _) -> x + t) fs
            fs = sort (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y > y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y > x2 = ((Frog y y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)
-- import Debug.Trace (trace)

data Frog = Frog !Int !Int !Int !Int !Int deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            es = scanl max 0 $ map (\((x, t), _) -> x + t) fs
            fs = sort (zip frogsIn [1..])
            z = zipWith (\((x, t), i) s -> Frog (max x (s+1)) (x + t) i 0 t) fs es

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos)
          | isNothing lookup =  process frogs (mosquito `Set.insert` rest) mosquitos
          | otherwise =  out ++ (process frogs'' rest' mosquitos)
          where
            -- d = trace ("frogs = " ++ (show frogs) ++ " rest = " ++ (show rest) ++ " mosq = " ++ (show mosquito))
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            (frog', rest') = eat frog rest (mosquito:mosqs)
              where
                -- d = trace ("ms = " ++ show (mosquito:mosqs) ++ " frog = " ++ show frog)
                frog@(Frog x _ _ _ _) = fromJust lookup

                mosqs = (case lookup of
                                Nothing -> []
                                Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                        )
                  where lookup = Set.lookupGE (Mosq x 0 0) rest

                eat frog@(Frog x y i c t) rest mosqs
                  | null mosqs || y < p = (frog, rest)
                  | otherwise = eat frog' rest' $ tail mosqs
                  where
                    mosq@(Mosq p b _) = head mosqs
                    frog' = Frog x (y+b) i (c+1) (t+b)
                    rest' = mosq `Set.delete` rest

            (frogs'', out) = clean frogs' [] frogsList
              where
                frogs' = frog' `Set.insert` frogs
                frogsList = Set.elems . snd $ Set.split frog' frogs'

                Frog _ y _ _ _ = frog'

                clean frogs out [] = (frogs, out)
                clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                  | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                  | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                  | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sort, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

data Frog = Frog { frogX :: !Int, frogY :: !Int, frogIndex :: !Int, frogCount :: !Int, frogT :: !Int } deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

main = do
  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            z = zipWith (\frog y -> frog { frogY = max (frogY frog) (y+1) }) fs ys
              where
                ys = scanl max (-1) $ map frogY fs
                fs = sortBy (comparing frogX) $ zipWith (\(x, t) i -> Frog x (x+t) i 0 t) frogsIn [1..]

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing =  process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) = out ++ (process frogs'' rest'' mosquitos)
              where
                (frog'@(Frog _ y _ _ _), rest'') = eat frog rest' mosqs
                  where
                    rest' = mosquito `Set.insert` rest

                    mosqs = mosq:(Set.elems . snd $ Set.split mosq rest')
                      where
                        mosq = fromJust $ Set.lookupGE (Mosq x 0 0) rest'

                    eat frog rest [] = (frog, rest)
                    eat frog@(Frog x y i c t) rest (mosq@(Mosq p b _):mosqs)
                      | y < p = (frog, rest)
                      | otherwise = eat frog' rest' mosqs
                      where
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  putStr . unlines . map (\(Frog _ _ _ c t) -> show c ++ " " ++ show t) $ sortBy (comparing (\(Frog _ _ i _ _) -> i)) result

{-# LANGUAGE FlexibleContexts #-}
{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}

import Control.Applicative ((<$>))
import Control.Monad (replicateM)
import Data.ByteString.Char8 (getLine, getContents, lines, readInt, words)
import Data.List (partition, sortBy)
import Data.Maybe (fromJust, isNothing)
import Data.Ord (comparing)
import Data.Set (Set)
import qualified Data.Set as Set
import Prelude hiding (getLine, lines, words)

import Control.Monad (when)
import Data.Array (array, elems)
import Data.Array.MArray.Safe (getElems, newListArray, readArray, writeArray)
import Data.Array.ST.Safe (STArray, runSTArray)
import Data.STRef (newSTRef, readSTRef, writeSTRef, modifySTRef)
import System.Random

data Frog = Frog { frogX :: !Int, frogY :: !Int, frogI :: !Int, frogC :: !Int, frogT :: !Int } deriving (Show)

instance Eq Frog where
  Frog x _ _ _ _ == Frog y _ _ _ _ = x == y
instance Ord Frog where
  Frog x _ _ _ _ <= Frog y _ _ _ _ = x < y

data Mosq = Mosq !Int !Int !Int deriving (Show)

instance Eq Mosq where
  Mosq _ _ i == Mosq _ _ j = i == j
instance Ord Mosq where
  Mosq x _ i <= Mosq y _ j = (x, i) <= (y, j)

getInts = map (fst . fromJust . readInt) . words <$> getLine
getPair = (\[a, b] -> (a, b)) <$> getInts

sort gen xs = elems $ runSTArray $ do
    let n = length xs
    a <- newListArray (1, n) xs
    gr <- newSTRef gen

    let sort' l r = when (l < r) $ do
        g <- readSTRef gr
        let (x, g') = randomR (l, r-1) g
        writeSTRef gr g'

        s <- readArray a x

        let loop i j
              | i > j = return (i, j)
              | otherwise = do
                ai <- readArray a i
                aj <- readArray a j
                case () of
                  _ | ai < s -> loop (i+1) j
                    | aj > s -> loop i (j-1)
                    | otherwise -> do
                        writeArray a i aj
                        writeArray a j ai
                        loop (i+1) (j-1)

        (i, j) <- loop l r

        sort' l j
        sort' i r

    sort' 1 n
    return a

main = do
  gen <- newStdGen

  [n, m] <- getInts
  frogsIn <- replicateM n getPair
  mosquitosIn <- replicateM m getPair

  let
    result = out ++ (process frogsSet Set.empty mosquitos)
      where
        (frogs, out) = partition (\(Frog a b _ _ _) -> a <= b) z
          where
            z = zipWith (\frog y -> frog { frogY = max (frogY frog) (y+1) }) fs ys
              where
                fs = sort gen $ zipWith (\(x, t) i -> Frog x (x+t) i 0 t) frogsIn [1..]
                ys = scanl max (-1) $ map frogY fs

        frogsSet = Set.fromList frogs

        mosquitos = zipWith (\(p, b) i -> Mosq p b i) mosquitosIn [1..]

        process :: Set Frog -> Set Mosq -> [Mosq] -> [Frog]
        process frogs _ [] = Set.elems frogs
        process frogs rest (mosquito@(Mosq p b _):mosquitos) = process' lookup
          where
            lookup = Set.lookupLE (Frog p 0 0 0 0) frogs >>= (\frog@(Frog _ y _ _ _) -> if p <= y then Just frog else Nothing)

            process' Nothing = process frogs (mosquito `Set.insert` rest) mosquitos
            process' (Just frog@(Frog x _ _ _ _)) =  out ++ (process frogs'' rest' mosquitos)
              where
                (frog', rest') = eat frog rest (mosquito:mosqs)
                  where
                    frog@(Frog x _ _ _ _) = fromJust lookup

                    mosqs = (case lookup of
                                    Nothing -> []
                                    Just mosq -> mosq:(Set.elems . snd $ Set.split mosq rest)
                            )
                      where lookup = Set.lookupGE (Mosq x 0 0) rest

                    eat frog@(Frog x y i c t) rest mosqs
                      | null mosqs || y < p = (frog, rest)
                      | otherwise = eat frog' rest' $ tail mosqs
                      where
                        mosq@(Mosq p b _) = head mosqs
                        frog' = Frog x (y+b) i (c+1) (t+b)
                        rest' = mosq `Set.delete` rest

                (frogs'', out) = clean frogs' [] frogsList
                  where
                    frogs' = frog' `Set.insert` frogs
                    frogsList = Set.elems . snd $ Set.split frog' frogs'

                    Frog _ y _ _ _ = frog'

                    clean frogs out [] = (frogs, out)
                    clean frogs out (frog@(Frog x2 y2 i c t):frogsList)
                      | y >= y2 = clean (frog `Set.delete` frogs) (frog:out) frogsList
                      | y >= x2 = ((Frog (y+1) y2 i c t) `Set.insert` (frog `Set.delete` frogs), out)
                      | otherwise = (frogs, out)

  putStr . unlines . map (\(c, t) -> show c ++ " " ++ show t) . elems . array (1, n) $ map (\(Frog _ _ i c t) -> (i, (c, t))) result


You are given an array $$$a$$$, consisting of $$$n$$$ integers.Each position $$$i$$$ ($$$1 \le i \le n$$$) of the array is either locked or unlocked. You can take the values on the unlocked positions, rearrange them in any order and place them back into the unlocked positions. You are not allowed to remove any values, add the new ones or rearrange the values on the locked positions. You are allowed to leave the values in the same order as they were.For example, let $$$a = [-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$, the underlined positions are locked. You can obtain the following arrays:   $$$[-1, 1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$;  $$$[-4, -1, \underline{3}, 2, \underline{-2}, 1, 1, \underline{0}]$$$;  $$$[1, -1, \underline{3}, 2, \underline{-2}, 1, -4, \underline{0}]$$$;  $$$[1, 2, \underline{3}, -1, \underline{-2}, -4, 1, \underline{0}]$$$;  and some others. Let $$$p$$$ be a sequence of prefix sums of the array $$$a$$$ after the rearrangement. So $$$p_1 = a_1$$$, $$$p_2 = a_1 + a_2$$$, $$$p_3 = a_1 + a_2 + a_3$$$, $$$\dots$$$, $$$p_n = a_1 + a_2 + \dots + a_n$$$.Let $$$k$$$ be the maximum $$$j$$$ ($$$1 \le j \le n$$$) such that $$$p_j < 0$$$. If there are no $$$j$$$ such that $$$p_j < 0$$$, then $$$k = 0$$$.Your goal is to rearrange the values in such a way that $$$k$$$ is minimum possible.Output the array $$$a$$$ after the rearrangement such that the value $$$k$$$ for it is minimum possible. If there are multiple answers then print any of them.

The first line contains a single integer $$$t$$$ ($$$1 \le t \le 1000$$$) — the number of testcases. Then $$$t$$$ testcases follow. The first line of each testcase contains a single integer $$$n$$$ ($$$1 \le n \le 100$$$) — the number of elements in the array $$$a$$$. The second line of each testcase contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$-10^5 \le a_i \le 10^5$$$) — the initial array $$$a$$$. The third line of each testcase contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$0 \le l_i \le 1$$$), where $$$l_i = 0$$$ means that the position $$$i$$$ is unlocked and $$$l_i = 1$$$ means that the position $$$i$$$ is locked.

Print $$$n$$$ integers — the array $$$a$$$ after the rearrangement. Value $$$k$$$ (the maximum $$$j$$$ such that $$$p_j < 0$$$ (or $$$0$$$ if there are no such $$$j$$$)) should be minimum possible. For each locked position the printed value should be equal to the initial one. The values on the unlocked positions should be an arrangement of the initial ones. If there are multiple answers then print any of them.

NoteIn the first testcase you can rearrange all values however you want but any arrangement will result in $$$k = 0$$$. For example, for an arrangement $$$[1, 2, 3]$$$, $$$p=[1, 3, 6]$$$, so there are no $$$j$$$ such that $$$p_j < 0$$$. Thus, $$$k = 0$$$.In the second testcase you are not allowed to rearrange any elements. Thus, the printed array should be exactly the same as the initial one.In the third testcase the prefix sums for the printed array are $$$p = [-8, -14, -13, -9, -5, 2, 0]$$$. The maximum $$$j$$$ is $$$5$$$, thus $$$k = 5$$$. There are no arrangements such that $$$k < 5$$$.In the fourth testcase $$$p = [-4, -4, -3, 3, 6]$$$.In the fifth testcase $$$p = [-1, 3, 10, 2, 12, 11]$$$.

import Control.Monad
import Data.Array
import Data.Foldable
import Data.Graph

-- Split a list in half, with both halves in order
half :: [a] -> ([a], [a])
half = join go
  where
    go t [] = ([], t)
    go t [_] = ([], t)
    go (t : ts) (_ : _ : hs) = (t : front, rear)
      where
        (front, rear) = go ts hs -- Interesting laziness

merge :: Ord a => [a] -> [a] -> [a]
merge [] ys = ys
merge xs [] = xs
merge xxs@(x : xs) yys@(y : ys)
  | x <= y = x : merge xs yys
  | otherwise = y : merge xxs ys

msort :: Ord a => [a] -> [a]
msort [] = []
msort [x] = [x]
msort xs = merge (msort front) (msort rear)
  where
    (front, rear) = half xs

readInput :: IO (Int, [Int], [Int])
readInput = do
  n <- read <$> getLine
  arr <- map read . words <$> getLine
  lock <- map read . words <$> getLine
  return (n, arr, lock)

solve (n, arr, lock) =
  snd $
    foldl
      ( \(j, str) i ->
          if (l ! i) == 0
            then (j + 1, (s' ! j) : str)
            else (j, (a ! i) : str)
      )
      (1, [])
      [1 .. n]
  where
    a = array (1, n) $ zip [1 .. n] arr
    l = array (1, n) $ zip [1 .. n] lock
    s' = array (1, length s) $ zip [1 .. length s] s
    s = reverse $ msort $ map (\(a, b) -> b) $ filter (\(a, b) -> a == 0) (zip lock arr)

main = do
  a <- read <$> getLine
  replicateM_ a $ do
    i <- readInput
    let r = solve i
    putStrLn $ foldl (\s x -> show x ++ " " ++ s) "" r

import Control.Monad
import Data.List

remerge :: [Int] -> [Int] -> [Int] -> [Int]
remerge [] [] [] = []
remerge (a:as) (1:los) unls =     a : remerge as los unls
remerge (_:as) (0:los) (v:unls) = v : remerge as los unls
remerge _ _ _ = error "huh?"

main :: IO ()
main = do
  t <- read <$> getLine
  replicateM_ t $ do
    _nStr <- getLine
    a  <- map read . words <$> getLine
    lo <- map read . words <$> getLine
    let
      unls = reverse $ sort [v | (v, 0) <- zip a lo]
      ans = remerge a lo unls
    putStrLn . unwords $ map show ans


In a Berland's zoo there is an enclosure with camels. It is known that camels like to spit. Bob watched these interesting animals for the whole day and registered in his notepad where each animal spitted. Now he wants to know if in the zoo there are two camels, which spitted at each other. Help him to solve this task.The trajectory of a camel's spit is an arc, i.e. if the camel in position x spits d meters right, he can hit only the camel in position x + d, if such a camel exists.

The first line contains integer n (1 ≤ n ≤ 100) — the amount of camels in the zoo. Each of the following n lines contains two integers xi and di ( - 104 ≤ xi ≤ 104, 1 ≤ |di| ≤ 2·104) — records in Bob's notepad. xi is a position of the i-th camel, and di is a distance at which the i-th camel spitted. Positive values of di correspond to the spits right, negative values correspond to the spits left. No two camels may stand in the same position.

If there are two camels, which spitted at each other, output YES. Otherwise, output NO.

import Control.Monad

getA = fmap (map read . words) getLine

main = do
	[n] <- getA
	a <- replicateM n getA
	putStr $ last $ "NO":["YES" | [x,d] <- a, [y,t] <- a, x+d==y, y+t==x]

module Main where

main
  = interact $ solve . map (read::String->Integer) . tail . words

solve list
  = process $ combine list
  where
    combine [] = []
    combine (x:b:rest) = (x,b) : combine rest

process list
  | any (\((x1,b1),(x2,b2))->x1+b1==x2 && x2+b2==x1) product
  = "YES\n"
  | otherwise
  = "NO\n"
  where
    product = [(a,b) | a<-list, b<-list]

module Main where
import Control.Monad
import Data.List
import Data.Maybe
import Data.Function
main = do
    n <- readLn :: IO Int
    ds <- replicateM n $ map (read :: String -> Int). words <$> getLine
    let e = zip (map (\[a,b] -> a+b) ds) [1..]
        f = zipWith (\[a,_] i -> (a,i) ) ds [1..]
        g = any ((>1).length) $ groupBy (\(a,b) (c,d) -> a==c && b==d) $  sortOn fst $ catMaybes $  (\(a,b) (c,d) -> if a==c then Just((min b d),(max b d)) else Nothing ) <$> e <*> f
    putStrLn $ if g then "YES" else "NO"

import Array (Array, (!), bounds, listArray)
import Monad (liftM)

solve :: Array Int (Int, Int) -> Bool
solve array  =  not (null solve')
  where
    n  =  snd $ bounds array
    solve'  =  [undefined | i <- [1..n], j <- [1..n], isGood i j]
    isGood i j  =  x1 + y1 == x2 && x2 + y2 == x1
      where
        (x1, y1)  =  array ! i
        (x2, y2)  =  array ! j

readPair :: String -> (Int, Int)
readPair line  =  case words line of
    [a, b]  ->  (read a, read b)
    _  ->  error $ concat ["readPair: ", "string ", show line, " must be two words"]

main :: IO ()
main  =  do
    n  <-  readLn
    pairs  <-  liftM (map readPair . take n . lines) getContents
    let pairs'  =  listArray (1, n) pairs
    putStrLn $ if solve pairs' then "YES" else "NO"

solve :: [(Int,Int)] -> Bool
solve camels = length (spittingPairs camels) > 0

spittingPairs :: [(Int,Int)] -> [(Int,Int)]
spittingPairs camels = filter (\(a,b) -> spitEachOther (camels!!a) (camels!!b)) [(x,y) | x <- [0..len], y <- [0..len], x < y]
    where len = length camels - 1

spitEachOther :: (Int,Int) -> (Int,Int) -> Bool
spitEachOther camel1 camel2 = (fst camel1 + snd camel1 == fst camel2) && (fst camel2 + snd camel2 == fst camel1)

bool2string :: Bool -> [Char]
bool2string True = "YES\n"
bool2string False = "NO\n"

line2tuple :: [Char] -> (Int,Int)
line2tuple line = (head x, last x)
    where x = map read tmp :: [Int]
          tmp = words line

process :: [[Char]] -> [Char]
process input = bool2string (solve (map line2tuple input))

processInput :: [Char] -> [Char]
processInput input = process (tail (lines input))

main = interact processInput

{-# OPTIONS_GHC -funbox-strict-fields #-}

import Control.Applicative
import Control.Monad
import Control.Arrow
import Data.List
import Data.Ord
import Text.Printf

main :: IO ()
main = do
  let readInts = map read . words
  n : _ <- readInts <$> getLine
  xs <- replicateM n $ readInts <$> getLine
  let as = do
        x : d : _ <- xs
        return (x, x + d)
      bs = do
        (x, s) <- as
        return (s, x)
      answer = if any (`elem` bs) as
                 then "YES"
                 else "NO"
  putStrLn answer

main = do
  (_:camels) <- fmap (map (map read) . map words . lines) getContents
  let pairs = [(a,b) | a<-camels, b<-camels, f a b]
      f [x,d] [y,e] = x+d == y && y+e == x
  if null pairs
     then putStrLn "NO"
     else putStrLn "YES"

module Main where
import Control.Monad
import Data.List
main = do
    n <- readLn :: IO Int
    ds <- replicateM n $ map (read :: String -> Int). words <$> getLine
    let e = map (\[a,b] -> a+b) ds
        f = e \\ map (\[a,_] -> a) ds
    putStrLn $ if length f > 1 then "YES" else "NO"

module Main where
import Control.Monad
import Data.List
main = do
    n <- readLn :: IO Int
    ds <- replicateM n $ map (read :: String -> Int). words <$> getLine
    let e = map (\[a,b] -> a+b) ds
        f = e `intersect`  map (\[a,_] -> a) ds
    putStrLn $ if length f > 1 then "YES" else "NO"

module Main where
import Control.Monad
import Data.List
import Data.Maybe
import Data.Function
main = do
    n <- readLn :: IO Int
    ds <- replicateM n $ map (read :: String -> Int). words <$> getLine
    let e = zip (map (\[a,b] -> a+b) ds) [1..]
        f = zipWith (\[a,_] i -> (a,i) ) ds [1..]
        g = any ((>1).length) $ groupBy ((==) `on` fst) $ sortOn fst $ catMaybes $  (\(a,b) (c,d) -> if a==c then Just((min b d),(max b d)) else Nothing ) <$> e <*> f
    putStrLn $ if g then "YES" else "NO"

module Main where
import Control.Monad
import Data.List
main = do
    n <- readLn :: IO Int
    ds <- replicateM n $ map (read :: String -> Int). words <$> getLine
    let e = map (\[a,b] -> a+b) ds
        f = e \\ map (\[a,_] -> a) ds
    putStrLn $ if length f > 2 then "YES" else "NO"


Two T-shirt sizes are given: $$$a$$$ and $$$b$$$. The T-shirt size is either a string M or a string consisting of several (possibly zero) characters X and one of the characters S or L.For example, strings M, XXL, S, XXXXXXXS could be the size of some T-shirts. And the strings XM, LL, SX are not sizes.The letter M stands for medium, S for small, L for large. The letter X refers to the degree of size (from eXtra). For example, XXL is extra-extra-large (bigger than XL, and smaller than XXXL).You need to compare two given sizes of T-shirts $$$a$$$ and $$$b$$$.The T-shirts are compared as follows:   any small size (no matter how many letters X) is smaller than the medium size and any large size;  any large size (regardless of the number of letters X) is larger than the medium size and any small size;  the more letters X before S, the smaller the size;  the more letters X in front of L, the larger the size. For example:   XXXS < XS  XXXL > XL  XL > M  XXL = XXL  XXXXXS < M  XL > XXXS

The first line of the input contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of test cases. Each test case consists of one line, in which $$$a$$$ and $$$b$$$ T-shirt sizes are written. The lengths of the strings corresponding to the T-shirt sizes do not exceed $$$50$$$. It is guaranteed that all sizes are correct.

For each test case, print on a separate line the result of comparing $$$a$$$ and $$$b$$$ T-shirt sizes (lines "<", ">" or "=" without quotes).

6

XXXS XS

XXXL XL

XL M

XXL XXL

XXXXXS M

L M

module Main where

import Data.Function
import Control.Monad
--import Data.Text (split)

toInt :: String -> Int
toInt str = core * (length xs + 1)
  where
    (s : xs) = reverse str
    core = case s of
      'S' -> -1
      'M' -> 0
      'L' -> 1

compareTShirts :: String -> String -> String
compareTShirts a b = toAns $ (compare `on` toInt) a b
  where
    toAns LT = "<"
    toAns EQ = "="
    toAns GT = ">"

main :: IO ()
main = do
  n <- getLine
  replicateM_ (read n) test
  where
    test = do
      input <- getLine
      let (a : b : []) = words input
      putStrLn $ compareTShirts a b


Roman and Denis are on the trip to the programming competition. Since the trip was long, they soon got bored, and hence decided to came up with something. Roman invented a pizza's recipe, while Denis invented a string multiplication. According to Denis, the result of multiplication (product) of strings $$$s$$$ of length $$$m$$$ and $$$t$$$ is a string $$$t + s_1 + t + s_2 + \ldots + t + s_m + t$$$, where $$$s_i$$$ denotes the $$$i$$$-th symbol of the string $$$s$$$, and "+" denotes string concatenation. For example, the product of strings "abc" and "de" is a string "deadebdecde", while the product of the strings "ab" and "z" is a string "zazbz". Note, that unlike the numbers multiplication, the product of strings $$$s$$$ and $$$t$$$ is not necessarily equal to product of $$$t$$$ and $$$s$$$.Roman was jealous of Denis, since he invented such a cool operation, and hence decided to invent something string-related too. Since Roman is beauty-lover, he decided to define the beauty of the string as the length of the longest substring, consisting of only one letter. For example, the beauty of the string "xayyaaabca" is equal to $$$3$$$, since there is a substring "aaa", while the beauty of the string "qwerqwer" is equal to $$$1$$$, since all neighboring symbols in it are different.In order to entertain Roman, Denis wrote down $$$n$$$ strings $$$p_1, p_2, p_3, \ldots, p_n$$$ on the paper and asked him to calculate the beauty of the string $$$( \ldots (((p_1 \cdot p_2) \cdot p_3) \cdot \ldots ) \cdot p_n$$$, where $$$s \cdot t$$$ denotes a multiplication of strings $$$s$$$ and $$$t$$$. Roman hasn't fully realized how Denis's multiplication works, so he asked you for a help. Denis knows, that Roman is very impressionable, he guarantees, that the beauty of the resulting string is at most $$$10^9$$$.

The first line contains a single integer $$$n$$$ ($$$2 \leq n \leq 100\,000$$$) — the number of strings, wroted by Denis. Next $$$n$$$ lines contain non-empty strings $$$p_1, p_2, \ldots, p_n$$$, consisting of lowercase english letters. It's guaranteed, that the total length of the strings $$$p_i$$$ is at most $$$100\,000$$$, and that's the beauty of the resulting product is at most $$$10^9$$$.

Print exactly one integer — the beauty of the product of the strings.

NoteIn the first example, the product of strings is equal to "abaaaba".In the second example, the product of strings is equal to "abanana".

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
import Control.Monad
import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Data.Char
import Data.Array.IArray
--import Debug.Trace

readInt = fst . fromJust . B.readInt <$> B.getLine
readInts = map (fst . fromJust . B.readInt) . B.words <$> B.getLine

charToNum x = ord x - ord 'a'

compress = map (\s -> let c = B.head s in (charToNum c,B.length s)) . B.group

getUpdateList ((l,x):xs) a
    | l == r = (l,if a!l == 0 then max x y else 1+x+y):xs
    | otherwise = (l,a!l+x):(r,a!r+y):xs
        where (r,y) = last xs

f :: Array Int Int -> B.ByteString -> Array Int Int
f a s = let s' = compress s in case s' of
                                    [(i,n)] -> a' // [(i,(a!i+1)*n+a!i)]
                                    xs -> accum max a' $ getUpdateList s' a'
        where a' = amap (\e -> if e == 0 then 0 else 1) a

main = do   n <- readInt
            (x:xs) <- B.words <$> B.getContents
            let a = accumArray max 0 (0,25) . compress $ x
            print $ maximum . foldl f a $ xs

{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE FlexibleContexts #-}
import Control.Monad
import qualified Data.ByteString.Char8 as B
import Data.Maybe
import Data.Char
import Data.Array.IArray
--import Debug.Trace

readInt = fst . fromJust . B.readInt <$> B.getLine
readInts = map (fst . fromJust . B.readInt) . B.words <$> B.getLine

charToNum x = ord x - ord 'a'

compress = map (\s -> let c = B.head s in (charToNum c,B.length s)) . B.group

getUpdateList ((l,x):xs) a
    | l == r = (l,if a!l == 0 then max x y else 1+x+y):xs
    | otherwise = (l,a!l+x):(r,a!r+y):xs
        where (r,y) = last xs

f :: Array Int Int -> B.ByteString -> Array Int Int
f a s = case s' of
            [(i,n)] -> a' // [(i,(a!i+1)*n+a!i)]
            xs -> accum max a' $ getUpdateList s' a'
        where   s' = compress $ B.init s
                a' = amap (\e -> if e == 0 then 0 else 1) a

main = do   n <- readInt
            --(x:xs) <- B.words <$> B.getContents
            --let a = accumArray max 0 (0,25) . compress $ x
            --print $ maximum . foldl f a $ xs
            a <- accumArray max 0 (0,25) . compress . B.init <$> B.getLine
            a' <- foldl f a <$> replicateM (n-1) B.getLine
            print $ maximum a'