Geometry TLE


#1

Can anyone help me make my O(n^2) solution in O(n), or O(n logn) or something similar. Here is the statement

Here is my code:

#include <bits/stdc++.h>

using namespace std;

//A proud student of Gimnazija Svetozar Markovic Nis
//From Serbia
//Team member of SouthSerbia Dziberi
//My professor is a grandmaster
//Can’t be hacked m8!
//Try me ■■■■■ !!!

typedef long long ll;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector vi;
typedef vector vl;
typedef vector vd;
typedef vector vb;
typedef vector vc;
typedef vector vs;
typedef vector vp;
typedef vector vpl;

int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cerr.tie(nullptr);

int n;
cin >> n;

vector<pi> a;
map<pi, int> vis;
vi q;

for (int e = 0; e < n; ++e){
	char c;
	cin >> c;

	if (c == '+'){
		int k, b;
		cin >> k >> b;

		a.push_back({k, b});
		++vis[{k, b}];
	}

	if (c == '-'){
		int k, b;
		cin >> k >> b;

		--vis[{k, b}];
	}

	if (c == '?'){
		int q;
		cin >> q;

		ll sol = 0;
		map<pi, bool> v;
		for (auto& i : a){
			if (!vis[i] || v[i])
				continue;
			int k = i.first, b = i.second;


			int x = (q - b) / k;
			if (x * k == q - b)
				sol += vis[i];
			v[i] = 1; 
		}

		cout << sol << '\n';
	}
}

}