#include using namespace std; typedef long long ll; const int MX = 52; const int MY = 100002; struct Point { vector> rects; bool flooded; int parent; int rank; }; Point world[MX][MY]; int toid(int x, int y) { return (MY + 1) * x + y; } Point& gid(int id) { int y = id % (MY + 1); int x = id / (MY + 1); return world[x][y]; } void ufFold(int a) { if(gid(a).parent != a) ufFold(gid(a).parent); gid(a).parent = gid(gid(a).parent).parent; } bool ufFind(int a, int b) { ufFold(a); ufFold(b); return gid(a).parent == gid(b).parent; } void ufUnion(int a, int b) { ufFold(a); ufFold(b); if(gid(gid(a).parent).rank < gid(gid(b).parent).rank) gid(gid(a).parent).parent = gid(b).parent; else if(gid(gid(b).parent).rank < gid(gid(a).parent).rank) gid(gid(b).parent).parent = gid(a).parent; else { gid(gid(b).parent).parent = gid(a).parent; gid(gid(b).parent).rank++; } } void floodTile(int x, int y) { world[x][y].flooded = true; if(x > 0 && world[x-1][y].flooded) ufUnion(toid(x, y), toid(x - 1, y)); if(y > 0 && world[x][y-1].flooded) ufUnion(toid(x, y), toid(x, y - 1)); if(world[x+1][y].flooded) ufUnion(toid(x, y), toid(x + 1, y)); if(world[x][y+1].flooded) ufUnion(toid(x, y), toid(x, y + 1)); } void join(int x1, int y1, int x2, int y2) { for(int x = x1; x <= x2; x++) { floodTile(x, y1); world[x][y1].rects.push_back(make_pair(2, toid(x1, y1))); floodTile(x, y2); world[x][y2].rects.push_back(make_pair(2, toid(x1, y1))); } for(int y = y1 + 1; y <= y2 - 1; y++) { floodTile(x1, y); world[x1][y].rects.push_back(make_pair(0, toid(x1, y1))); floodTile(x2, y); world[x2][y].rects.push_back(make_pair(1, toid(x1, y1))); } } void connectToRects(int x, int y) { int id = toid(x, y); for(auto&& p : world[x][y].rects) ufUnion(id, p.second); unordered_multiset started; for(int xx = x + 1; xx < MX; xx++) { for(auto&& p : world[xx][y].rects) { if(p.first == 0) started.insert(p.second); } for(auto&& p : world[xx][y].rects) { if(p.first == 1 && started.count(p.second) == 0) ufUnion(id, p.second); } world[xx][y].rects.clear(); } } bool connected(int x1, int y1, int x2, int y2) { connectToRects(x1, y1); connectToRects(x2, y2); return ufFind(toid(x1, y1), toid(x2, y2)); } int main() { ios::sync_with_stdio(false); int l; cin >> l; for(int x = 0; x < MX; x++) { for(int y = 0; y < MY; y++) { world[x][y].parent = toid(x, y); } } for(int i = 0; i < l; i++) { int t, x1, y1, x2, y2; cin >> t >> x1 >> y1 >> x2 >> y2; if(t == 0) { if(x1 > x2) swap(x1, x2); if(y1 > y2) swap(y1, y2); join(x1, y1, x2, y2); } else cout << connected(x1, y1, x2, y2) << endl; } }