Program Optimization
Time limit2sMemory limit256 MB
Simulate the given C++ program exactly, including its mt19937 random sequence, while answering range mex queries over a permuted array fast enough for q up to 10^7.
- Level
Hard8 of 10
- Topics
- Simulation, Segment tree, Implementation
- Solved
- No attempts yet
Problem
Consider the following C++ code:
#include <algorithm>
#include <random>
int query_mex(const int *a, int l, int r);
int simulate(int n, int *a, int q, int k, int s) {
std::mt19937 gen;
gen.seed(s);
int last = 0;
while (q--) {
int op = gen() % k;
int i = (gen() + last) % n;
if (!op && i) {
std::swap(a[i - 1], a[i]);
} else {
int j = gen() % n;
last ^= query_mex(a, std::min(i, j), std::max(i, j));
}
}
return last;
}
In this program, query_mex(a, l, r) returns the smallest non-negative integer that does not occur among .
Given the values of , , , , and , find the value the function returns.
Input
The first line contains four integers , , , and (, , , ). The second line contains distinct integers ().
Output
Output an integer, the value the function returns.