PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#SRD0000051. Các băng chuyền (Conveyor Belts)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Các băng chuyền (Conveyor Belts)

Conveyor Belts

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

An n×mn\times m grid contains one conveyor belt in each cell. A ^ belt moves upward, < moves left, and > moves right. Rows are numbered from top to bottom and columns from left to right, beginning at 11.

Process:

  • A x y: launch an object at cell (x,y)(x,y) and follow the belts until it exits the grid or loops forever. Output the first outside coordinate (top row 00, left column 00, or right column m+1m+1), or -1 -1 for an infinite cycle.
  • C x y c: change cell (x,y)(x,y) to belt direction cc.

Each A event is independent and does not modify the board.

Input

The first line gives n,m,qn,m,q, followed by nn strings of length mm using ^, <, >. Next come qq events A x y or C x y c. There are at most 10 00010\,000 C events.

Output

For each A event, print two integers: the exit coordinate or -1 -1 if the path loops.

Subtasks

  • Subtask 1 (10 points): 1≤n≤601\le n\le60; 1≤q≤1001\le q\le100. Grid width 1≤w≤101\le w\le10 and at most 10410^4 changes.
  • Subtask 2 (20 points): 1≤n≤30001\le n\le3000; 1≤q≤30001\le q\le3000. Grid width 1≤w≤101\le w\le10 and at most 10410^4 changes.
  • Subtask 3 (25 points): 1≤n≤250001\le n\le25000; 1≤q≤250001\le q\le25000. Grid width 1≤w≤101\le w\le10 and at most 10410^4 changes.
  • Subtask 4 (45 points): 1≤n≤1000001\le n\le100000; 1≤q≤1000001\le q\le100000. Grid width 1≤w≤101\le w\le10 and at most 10410^4 changes.

Examples

Input

2 2 3
>>
^^
A 2 1
C 1 2 <
A 2 1

Output

1 3
-1 -1

Explanation

Initially the object travels (2,1)→(1,1)→(1,2)→(1,3)(2,1)\to(1,1)\to(1,2)\to(1,3) and exits at 1 3. After changing (1,2)(1,2) to <, the two top-row cells form a two-cell cycle, giving -1 -1.