Prefix XOR

All submissions for this problem are available.
Read problems statements in Mandarin Chinese, Russian and Vietnamese as well.
Given an array A with n elements. There are q queries of the form (l, r). For each query you need to find the number of pairs (i, j) such that l ≤ i ≤ j ≤ r and and which satisfies the following condition: A_{i} ≤ A_{i} ^ A_{i + 1} ≤ ... ≤ A_{i} ^ A_{i + 1} ^ ... ^ A_{j}, where ^ is XOR.
Input
 The first line contains one integer n, denoting the number of elements.
 The second line contains n spaceseparated integers: A_{1}, A_{2}, ... , A_{n} .
 The third line contains one integer q, denoting the number of queries.
 The ith of the next q lines contains two integers: a_{i}, b_{i}. This is explained below.
You need to find the answers to the queries online, and so, the queries are encoded. To decode the queries from the data given in input, you need to perform the following transformations:
l_{i} = (a_{i} + ans_{i1}) mod n + 1
r_{i} = (b_{i} + ans_{i1}) mod n + 1
Where ans_{i1} is the answer for the previous query. (l_{i}, r_{i}) is the decoded ith query, for which you need to find and output the answer. Assume that ans_{0} = 0. The first query is (l_{1}, r_{1}).
Output
For each query print the answer on a single line.
Constraints
 1 ≤ n, q ≤ 4 * 10^{5}
 0 ≤ A_{i} ≤ 10^{9}
 0 ≤ a_{i}, b_{i} < n
 1 ≤ l_{i} ≤ r_{i} ≤ n after decoding
Example
Input: 4 1 2 3 4 3 1 3 0 3 3 1 Output: 4 6 4
Author:  gomelfk 
Tags  gomelfk 
Date Added:  26052017 
Time Limit:  2 sec 
Source Limit:  50000 Bytes 
Languages:  C, CPP14, JAVA, PYTH, PYTH 3.6, PYPY, CS2, PAS fpc, PAS gpc, RUBY, PHP, GO, NODEJS, HASK, SCALA, D, PERL, FORT, WSPC, ADA, CAML, ICK, BF, ASM, CLPS, PRLG, ICON, SCM qobi, PIKE, ST, NICE, LUA, BASH, NEM, LISP sbcl, LISP clisp, SCM guile, JS, ERL, TCL, PERL6, TEXT, SCM chicken, PYP3, CLOJ, FS 
Comments
 Please login at the top to post a comment.
SUCCESSFUL SUBMISSIONS
Fetching successful submissions
HELP
If you are still having problems, see a sample solution here. 