ICPC 2019 Asia Danang Regional - Easy Query

Author: Benjamin Qi


Time Complexity: O(30(n+q)logn)\mathcal O(30(n + q) \log n)

The first step is to determine sus_u and svs_v for each query with a Wavelet tree. Once we have these values, we can answer the queries in decreasing order of ll with a segment tree.

We can do this by computing each of the 30 bits of f(t(i))f(t^{(i)}) independently. The kk-th bit of f(t(i))f(t^{(i)}) is equal to 11 if there exists some value xx such that the following conditions hold:

  • x[su+1,sv1]x \in [s_u + 1, s_v - 1].
  • The kk-th bit of xx is 11.
  • The ii-th leftmost occurrence of xx among al,,ana_l, \dots, a_n is less than or equal to rr (see a[k] <= t[0] in the code below.)

The indices of each segment tree correspond to the xx that appear in aia_i in increasing order. Each node of the ii-th segment tree corresponds to some xx-range [xl,xr][x_l, x_r], and stores the minimum jj such that aja_j is the ii-th occurrence of xx among al,,ana_l, \dots, a_n for some x[xl,xr]x \in [x_l, x_r].

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using db = double;
using str = string; // yay python!
using pi = pair<int, int>;
using pl = pair<ll, ll>;

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!