OI XXX - kol

// https://szkopul.edu.pl/problemset/problem/GzVKpGwP7huOf_gNK4ZnIPn1/site/?key=statement

#include <array>
#include <cstdint>
#include <iostream>
#include <limits.h>
#include <vector>

constexpr int sizik = 2048;

struct Point {
    int time = INT16_MIN;
    // int time = -1;
};

struct Color {
    int color = -1;
};

struct Coord {
    int x, y;
    Coord(int x, int y) {
        this->x = x;
        this->y = y;
    }
};

int main() {
    std::ios_base::sync_with_stdio(0);
    std::cin.tie(0);
    std::cout.tie(0);

    std::array<std::array<Point, sizik>, sizik> map;
    std::array<std::array<Color, sizik>, sizik> colors;

    int m, n, k;
    std::cin >> m >> n >> k;

    std::vector<int> snake;
    snake.push_back(0);
    Coord curr_pos(1, 1);
    int curr_time = 0;
    map[curr_pos.x][curr_pos.y].time = curr_time;

    for (int i = 0; i < n; i++) {
        int k, x, y;
        std::cin >> y >> x >> k;

        colors[x][y].color = k;
    }

    for (; k > 0; k--) {
        char command;
        std::cin >> command;

        if (command == 'Z') {
            int x, y;
            std::cin >> y >> x;

            int diff = curr_time - map[x][y].time;
            if (diff >= snake.size()) {
                std::cout << "-1\n";
            } else {
                std::cout << snake[snake.size() - diff - 1] << '\n';
            }
        } else {
            curr_time++;

            if (command == 'G') {
                curr_pos.y--;
            } else if (command == 'D') {
                curr_pos.y++;
            } else if (command == 'L') {
                curr_pos.x--;
            } else if (command == 'P') {
                curr_pos.x++;
            }

            map[curr_pos.x][curr_pos.y].time = curr_time;

            if (colors[curr_pos.x][curr_pos.y].color != -1) {
                snake.push_back(colors[curr_pos.x][curr_pos.y].color);
                colors[curr_pos.x][curr_pos.y].color = -1;
            }
        }
    }

    return 0;
}