#include <string>
#include <vector>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
vector <pair<pair<int, int>,int>> m;
map <pair<pair<int, int>,int>, int> m2;
bool CheckRight(int n) {
bool flag;
for (int i = 0; i< m.size(); i++) {
flag = false;
//기둥 확인
if (m[i].second == 0) {
if ( (m[i].first.second == 0)){
flag = true;
}
if (m[i].first.second > 0) {
if ((find(m.begin(), m.end(), make_pair(make_pair(m[i].first.first, m[i].first.second - 1), 0)) != m.end())) {
flag = true;
}
}
if (m[i].first.first > 0) {
if ((find(m.begin(), m.end(), make_pair(make_pair(m[i].first.first - 1, m[i].first.second), 1)) != m.end())) {
flag = true;
}
}
if ((find(m.begin(), m.end(), make_pair(make_pair(m[i].first.first, m[i].first.second), 1)) != m.end())) {
flag = true;
}
}
//보 확인
else
{
if (m[i].first.second > 0) {
if ((find(m.begin(), m.end(), make_pair(make_pair(m[i].first.first, m[i].first.second - 1), 0)) != m.end())) {
flag = true;
}
}
if (m[i].first.second > 0 && m[i].first.first<n) {
if (find(m.begin(), m.end(), make_pair(make_pair(m[i].first.first + 1, m[i].first.second - 1), 0)) != m.end()) {
flag = true;
}
}
if (m[i].first.first > 0 && m[i].first.first <n-1) {
if ((find(m.begin(), m.end(), make_pair(make_pair(m[i].first.first - 1, m[i].first.second), 1)) != m.end()) && (find(m.begin(), m.end(), make_pair(make_pair(m[i].first.first + 1, m[i].first.second), 1)) != m.end())) {
flag = true;
}
}
}
if (flag == false)
break;
}
return flag;
}
vector<vector<int>> solution(int n, vector<vector<int>> build_frame) {
for (int i = 0; i < build_frame.size(); i++) {
//설치
if (build_frame[i][3] == 1) {
m.push_back(make_pair(make_pair(build_frame[i][0], build_frame[i][1]), build_frame[i][2]));
if(!CheckRight(n))
m.pop_back();
}
//삭제
else {
vector <pair<pair<int, int>, int>> temp;
temp = m;
for (int j = 0; j < m.size(); j++) {
if (m[j] == make_pair(make_pair(build_frame[i][0], build_frame[i][1]), build_frame[i][2])) {
m.erase(m.begin() + j);
break;
}
}
if (!CheckRight(n))
m = temp;
}
}
for (int i = 0; i < m.size(); i++) {
m2.insert(make_pair(make_pair(make_pair(m[i].first.first, m[i].first.second),m[i].second), m[i].second));
}
vector<vector<int>> answer(m2.size());
int i = 0;
for (auto it = m2.begin(); it != m2.end(); it++) {
answer[i].push_back(it->first.first.first);
answer[i].push_back(it->first.first.second);
answer[i].push_back(it->first.second);
i++;
}
return answer;
}