Problem: Example

Input

9
0 0 v
1 0 >
2 0


Output

9

Solution (in CPP):

Solution:
#include <bits/stdc++.h>
using namespace std;

using ll = long long;
using Pi = pair<int, int>;
constexpr int INF = (1<<30) - 1;
constexpr ll LLINF = (1ll<<60);

using vint = vector<int>;
int simulation(int sx, int sy, map<int, set<Pi>> row, map<int, set<Pi>> col) {
        int ret = 1;    
        
        auto getd = [&](int y, int x) {
                return (row[y].lower_bound(Pi(x, -INF))->second);
        };

        int dir = getd(sy, sx);
        //cout << sx << " " << sy << " " << (char)dir << endl;

        //cout << "A:" << row[sy].size() << " " << col[sx].size() << endl;
        row[sy].erase(Pi(sx, dir));
        col[sx].erase(Pi(sy, dir));
        //cout << "B:" << row[sy].size() << " " << col[sx].size() << endl;
        int x = sx, y = sy;
        while (true) {
                //cout << "    " << x << " " << y << endl;
                //find
                int nx, ny, nd;
                if (dir == '^') {
                        auto pos = col[x].lower_bound(Pi(y, -INF));
                        if (pos == begin(col[x])) return (ret);
                        --pos;
                        nx = x, ny = pos->first;
                        nd = pos->second;
                        ret++;
                } else if (dir == '>') {
                        auto pos = row[y].lower_bound(Pi(x, -INF));
                        if (pos == end(row[y])) return (ret);
                        nx = pos->first, ny = y;
                        nd = pos->second;
                        ret++;
                } else if (dir == '<') {
                        auto pos = row[y].lower_bound(Pi(x, -INF));
                        if (pos == begin(row[y])) return (ret);
                        pos--;
                        nx = pos->first, ny = y;
                        nd = pos->second;
                        ret++;
                } else {
                        auto pos = col[x].lower_bound(Pi(y, -INF));
                        if (pos == end(col[x])) return (ret);
                        nx = x, ny = pos->first;
                        //cout << "                " << nx << " " << ny << endl;
                        nd = pos->second;
                        ret++;
                }
                row[ny].erase(Pi(nx, nd));
                col[nx].erase(Pi(ny, nd));
                y = ny, x = nx, dir = nd;
        }
        return (ret);
}

int main() {
        int N; cin >> N;
        vint X(N), Y(N), D(N);

        map<int, set<Pi>> row, col;
        for (int i = 0; i < N; i++) {
                int x, y;
                char d; cin >> x >> y >> d;
                tie(X[i], Y[i], D[i]) = tie(x, y, d);
                row[y].insert(Pi(x, d));
                col[x].insert(Pi(y, d));
        }
        
        int maxv = 0;
        for (int i = 0; i < N; i++) {
                //cout << X[i] << " " << Y[i] << endl;
                int val = simulation(X[i], Y[i], row, col);
                maxv = max(maxv, val);
                //cout << val << endl;
        }

        cout << maxv << endl;   
        return (0);
}