#include<bits/stdc++.h>
using namespace std;
int main() {
    int N, M, Q;
    cin >> N >> M >> Q;
    int a, x, y;
    vector<bool> shelf(10001, false);
    vector<int> book(10001, 0);
    vector<int> borrow;
    while (Q--) {
        cin >> a;
        switch (a) {
        case 1:
            cin >> x >> y;
            if (book[x] != -1 && !shelf[book[x]] && !shelf[y])
                book[x] = y;
            break;
        case 2:
            cin >> y;
            shelf[y] = true;
            break;
        case 3:
            cin >> y;
            shelf[y] = false;
            break;
        case 4:
            cin >> x;
            if (book[x] != -1 && book[x] != 0 && !shelf[book[x]]) {
                borrow.push_back(book[x]);
                book[x] = -1;
            }
            else
                borrow.push_back(-1);
            break;
        case 5:
            cin >> x;
            if (book[x] == -1)
                book[x] = 0;
            break;
        default:
            break;
        }

    }
    for (int i = 0; i < borrow.size(); i++)
        cout << borrow[i] << endl;
}

vector<bool> shelf(N + 1, false);
    vector<int> book(M + 1, 0);

会发生溢出。改成

vector<bool> shelf(10001, false);
    vector<int> book(10001, 0);

之后测试通过。