Under construction

Still being built. Things may change

The Scariest Story

Time limit1sMemory limit1024 MB

Summary
Given a story of space-separated words, answer position lookups between characters and words while processing character inserts and deletes.
Level

Medium7 of 10

Topics
String, Tree, Implementation
Solved
No attempts yet

Statement

Little Jack decided to write the scariest story he could to frighten his friends on Halloween.

A story is a non-empty sequence of words separated by spaces. A word is a non-empty sequence of lowercase Latin letters.

The quality of a story depends on its words and also on the characters inside those words.

Jack has already written a story with nn words. Now he wants to perform mm operations on it. Each operation either checks how scary the story is or changes it slightly. There are four kinds of operations:

  • ?1 i: find the word number and the position inside that word for the character at position ii of the story.
  • ?2 w p: find the position in the story of the pp-th character of the ww-th word.
  • + i c: insert character cc (a lowercase letter or a space written as \_) at position ii of the story.
  • - i: delete the character at position ii of the story.

Input

The first line contains two integers nn and mm separated by a space (1⩽n,m⩽2⋅1051 \leqslant n, m \leqslant 2 \cdot 10^5).

The second line contains the story: nn words separated by spaces. The total length of all words does not exceed 10510^5.

Each of the next mm lines describes one operation.

  • ?1 i: 1⩽i⩽L1 \leqslant i \leqslant L, where LL is the current length of the story.
  • ?2 w p: 1⩽w⩽W1 \leqslant w \leqslant W and 1⩽p⩽P1 \leqslant p \leqslant P, where WW is the current number of words and PP is the length of the ww-th word.
  • + i c: 1⩽i⩽L1 \leqslant i \leqslant L. cc is a lowercase letter or a space (\_).
  • - i: 1⩽i⩽L1 \leqslant i \leqslant L.

At no moment does the story contain two consecutive spaces or a space at the start. Characters in queries of the first type are always letters, not spaces.

Output

For each query of the first type, print the word number and the position inside the word on one line, separated by a space.

For each query of the second type, print the position in the story on its own line.

Examples1

  1. Example 1

    Input
    3 16
    hell spirits fear
    ?1 1
    ?2 3 1
    + 12 _
    ?2 3 1
    + 13 i
    ?1 13
    ?1 14
    ?1 16
    ?1 7
    - 5
    ?1 7
    ?2 1 8
    - 1
    - 1
    ?2 2 1
    ?2 3 2
    
    Expected output
    1 1
    14
    13
    3 1
    3 2
    4 1
    2 2
    1 7
    8
    10
    14