Submission #1342858

#TimeUsernameProblemLanguageResultExecution timeMemory
1342858GervidDeda (COCI17_deda)C++20
40 / 140
1096 ms45236 KiB
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <limits.h>
#include <algorithm>
#include <math.h>
#include <array>

using namespace std;
using ll = long long;

vector<set<int>> tree; //on distance, sets of ages
int n, q;

void insert(int l, int r, ll x, int age, ll dist) {
	tree[x].insert(age);
	if (l == r) {
		return;
	}
	int m = (l + r) / 2;
	if (dist <= m) insert(l, m, 2 * x + 1, age, dist);
	else insert(m+1, r, 2 * x + 2, age, dist);
}

set<int> query(int l, int r, ll x, int distlimit) {
	if (distlimit < l) return {};
	if (r <= distlimit) {
		return tree[x];
	}
	int m = (l + r) / 2;
	set<int> s1 = query(m + 1, r, 2 * x + 2, distlimit);
	set<int> s2 = query(l, m, 2*x+1, distlimit);
	if (s2 < s1) swap(s1, s2);
	for (int val : s1) {
		s2.insert(val);
	}
	return s2;
}
vector<int> distances;
int getid(ll d) {
	return lower_bound(distances.begin(), distances.end(), d) - distances.begin();
}

signed main()
{
	iostream::sync_with_stdio(0);
	cin.tie(0);

	cin >> n >> q;
	tree.resize(4 * n);

	vector<array<int, 3>> queries(q);
	distances.resize(q);
	for (int i = 0; i < q; i++) {
		char type;
		int a, b;
		cin >> type >> a >> b;
		queries[i] = { type == 'M', a, b };
		distances[i] = a;
	}
	sort(distances.begin(), distances.end());
	distances.erase(unique(distances.begin(), distances.end()), distances.end());

	for (int i = 0; i < q; i++) {
		
		if (queries[i][0]) {
			insert(0, n, 0, queries[i][2], getid(queries[i][1]));
		}
		else {
			set<int> s = query(0, n, 0, getid(queries[i][1]));
			set<int>::iterator ans = s.lower_bound(queries[i][2]);
			if (ans == s.end()) cout << "-1\n";
			else cout << *ans << '\n';
		}
	}
}
#Verdict Execution timeMemoryGrader output
Fetching results...