You are on page 1of 2

/**

* author: tourist
* created: 07.08.2021 17:13:13
**/
#include <bits/stdc++.h>

using namespace std;

mt19937_64 rng((unsigned int)


chrono::steady_clock::now().time_since_epoch().count());

int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int tt;
cin >> tt;
for (int qq = 1; qq <= tt; qq++) {
cout << "Case #" << qq << ":";
int b, s, n;
cin >> b >> s >> n;
vector<int> x(s), y(s);
vector<tuple<int, int, int>> e(s);
for (int i = 0; i < s; i++) {
cin >> x[i] >> y[i];
--x[i]; --y[i];
e[i] = make_tuple(x[i], y[i], i);
}
sort(e.begin(), e.end());
vector<uint64_t> h(b);
uint64_t total = 0;
for (int i = 0; i < b; i++) {
h[i] = rng();
total ^= h[i];
}
vector<pair<uint64_t, int>> aux(b);
for (int i = 0; i < b; i++) {
aux[i] = make_pair(total ^ h[i], i);
}
sort(aux.begin(), aux.end());
auto Get = [&](uint64_t hh) {
auto iter = lower_bound(aux.begin(), aux.end(), make_pair(hh, -1));
if (iter != aux.end() && iter->first == hh) {
return iter->second;
}
return -1;
};
vector<vector<uint64_t>> pref_x(s + 1, vector<uint64_t>(1, 0));
vector<vector<uint64_t>> pref_y(s + 1, vector<uint64_t>(1, 0));
for (int m = 1; m <= s; m++) {
for (int i = m - 1; i < s; i += m) {
auto new_x = pref_x[m].back() ^ h[x[i]];
pref_x[m].push_back(new_x);
auto new_y = pref_y[m].back() ^ h[y[i]];
pref_y[m].push_back(new_y);
}
}
uint64_t hx = 0;
uint64_t hy = 0;
int on = 0;
for (int it = 0; it < n; it++) {
string foo;
int L, R, m;
cin >> foo >> L >> R >> m;
int from = (L - 1) / m;
int to = R / m;
if (foo == "E") {
on += to - from;
} else {
on -= to - from;
}
int res = -1;
hx ^= pref_x[m][to] ^ pref_x[m][from];
hy ^= pref_y[m][to] ^ pref_y[m][from];
if (on == b - 1) {
int id_x = Get(hx);
int id_y = Get(hy);
if (id_x != -1 && id_y != -1) {
auto iter = lower_bound(e.begin(), e.end(), make_tuple(id_x, id_y, -1));
if (iter != e.end() && get<0>(*iter) == id_x && get<1>(*iter) == id_y) {
res = get<2>(*iter);
}
}
}
cout << " ";
if (res == -1) {
cout << "X";
} else {
cout << res + 1;
}
}
cout << '\n';
}
return 0;
}

You might also like