/* ░░░░░░░▄▀▀▀▀▀▀▀▄▄░░░░░░ ░░░░░░▐░░░▄▄▄▄▄░ ▀▀▄ ░░░░░▐░$░▐▀░░░░▀▀▀▀▄▀ ░░░░░▌░░▐░░░░░░░░░░▄▄ ░░░░▐░░░░▀▄▄▄▄▄▄▀▀▀░▄▀ ░░░░▌▒░▒░▒░▒░░░▄▄▄▀▀ ░░░░▌▒▒░▒▒▒▒▄▄▀▀ ░░░▐░░▒▒▒▄▀▀░░░░░░░ ░░░▐░▒▒▒▐░░░░░░░░░ ░░░▐▒▒▒▒▌░░░░░░░░ ░░░▐▒▒▒░▐░░░░░░░░░▄▀▄ ░░░▐▒▒░▒▐▄▄▀▀▀▀▄▄▄▀░▌ ░░░▐░▒▒░▄▄▄▄▀▀▄▄▄▄▄▀ ░░▐▒▒▄▒▒▀▄▄▄▄▄▀░░░▄▌░░ ░░▌▒▐░▀▀▄▄▄▄▄▄▄▀▀▀░░░░ ░▐▒▒▌░░░░░░░░░░░░░ */ #include using namespace std; typedef long long ll; const int dist_sz = 1024; const int block_sz = 1024; const int sz = 1024; const int MX = 500; const int N = 2e6 + 7; int cost[sz][sz]; bool vis[sz][sz]; int kek[sz][sz][2]; int sum[N]; int cnt[N]; struct type { bool add; int val; }; vector gr[N]; void add(int x) { sum[x / dist_sz]++; cnt[x]++; } void del(int x) { sum[x / dist_sz]--; cnt[x]--; } int get() { for (int i = 0; i <= dist_sz; i++) { if (sum[i]) { for (int j = i * dist_sz; ; j++) { if (cnt[j]) { return j; } } } } return 1e9; } struct ev { int t, x, y, ind; }; struct pt { int x, y; }; pt a[N], t[N]; bool operator < (const pt &a, const pt &b) { return (a.y != b.y ? a.y < b.y : a.x < b.x); } int ans = 1e9; void rec(int l, int r) { if (r - l <= 3) { for (int i = l; i <= r; i++) { for (int j = i + 1; j <= r; j++) { ans = min(ans, cost[a[i].x][a[j].x] + cost[a[i].y][a[j].y]); } } sort(a + l, a + r + 1); return; } else { int m = (l + r) / 2; int mid = a[m].x; rec(l, m); rec(m + 1, r); merge(a + l, a + m + 1, a + m + 1, a + r + 1, t); copy(t, t + r - l + 1, a + l); int tsz = 0; for (int i = l; i <= r; i++) { if (cost[a[i].x][mid] < ans) { for (int j = tsz - 1; j >= 0 && cost[a[i].y][t[j].y] < ans; j--) { ans = min(ans, cost[a[i].x][t[j].x] + cost[a[i].y][t[j].y]); } t[tsz++] = a[i]; } } return; } } int find_closest(vector p) { sort(p.begin(), p.end(), [] (const pt &a, const pt &b) { if (a.x == b.x) { return a.y < b.y; } else { return a.x < b.x; } }); ans = 1e9; int n = p.size(); for (int i = 0; i < n; i++) { a[i] = p[i]; } rec(0, n - 1); return ans; } int main() { #ifdef ONPC freopen("a.in", "r", stdin); //freopen("a.out", "w", stdout); #else //freopen("a.in", "r", stdin); //freopen("a.out", "w", stdout); #endif for (int i = 0; i < sz; i++) { for (int j = 0; j < sz; j++) { cost[i][j] = (i - j) * (i - j); } } vector e; int q; scanf("%d", &q); vector die(q, q); vector a(q), b(q); map , int> ind; for (int i = 0; i < q; i++) { char c; scanf(" %c", &c); int x, y; scanf("%d%d", &x, &y); if (c == '+') { e.push_back({1, x, y, i}); ind[{x, y}] = i; a[i] = x, b[i] = y; } else { e.push_back({2, x, y, ind[{x, y}]}); die[ind[{x, y}]] = i; } } vector trash, cur; vector p; for (int i = 0; i < q; i += block_sz) { int st = i, en = min(q, i + block_sz); cur.clear(); for (int j = 0; j < st; j++) { if (e[j].t <= 2) { if (die[e[j].ind] >= en) { cur.push_back(e[j].ind); } } } for (auto c : cur) { vis[e[c].x][e[c].y] = true; } for (int i = 1; i <= MX; i++) { int lst = -1; for (int j = 1; j <= MX; j++) { if (vis[i][j]) { lst = j; } kek[i][j][0] = lst; } lst = -1; for (int j = MX; j >= 1; j--) { if (vis[i][j]) { lst = j; } kek[i][j][1] = lst; } } trash.clear(); for (int a = st; a < en; a++) { if (e[a].t == 1) { int lul = min(en, die[a]); int x = e[a].x, y = e[a].y; int dist = 1e9; for (int i = 1; i <= MX; i++) { for (int t = 0; t < 2; t++) { if (kek[i][y][t] != -1) { dist = min(dist, cost[i][x] + cost[y][kek[i][y][t]]); } } } if (dist == 1e9) { continue; } gr[a - st].push_back({1, dist}); if (lul != en) { gr[lul - st].push_back({0, dist}); } else { trash.push_back(dist); } } else if (e[a].t == 2 && e[a].ind < st) { int x = e[a].x, y = e[a].y; int dist = 1e9; for (int i = 1; i <= MX; i++) { for (int t = 0; t < 2; t++) { if (kek[i][y][t] != -1) { dist = min(dist, cost[i][x] + cost[y][kek[i][y][t]]); } } } if (dist == 1e9) { continue; } gr[st - st].push_back({1, dist}); gr[a - st].push_back({0, dist}); } } for (int j = st; j < en; j++) { for (int k = j + 1; k < en; k++) { if (e[j].t <= 2 && e[k].t <= 2 && e[j].ind != e[k].ind) { int _st = max(st, max(e[j].ind, e[k].ind)); int _en = min(en, min(die[e[j].ind], die[e[k].ind])); if (_st > _en) { continue; } int dist = cost[e[j].x][e[k].x] + cost[e[j].y][e[k].y]; gr[_st - st].push_back({1, dist}); if (_en != en) { gr[_en - st].push_back({0, dist}); } else { trash.push_back(dist); } } } } p.clear(); for (auto c : cur) { p.push_back({e[c].x, e[c].y}); } int rlx = find_closest(p); for (int i = st; i < en; i++) { for (auto c : gr[i - st]) { if (c.add) { add(c.val); } else { del(c.val); } } gr[i - st].clear(); int res = min(get(), rlx); if (res == 1e9) { res = 0; } printf("%d\n", res); } for (auto c : trash) { del(c); } for (int i = 1; i <= MX; i++) { for (int j = 1; j <= MX; j++) { vis[i][j] = false; } } } }