MAXOR - MAXOR

You are given a sequence A[1], A[2] ... A[N]. (0 ≤ A[i] < 231, 1 ≤ N ≤ 12000).

A query is defined as follows:

  • Query(x, y) = Max { a[i] xor a[i+1] xor ... xor a[j] ; l ≤ i ≤ j ≤ r }.
  • l = min ( ((x+lastans) mod N)+1, ((y+lastans) mod N)+1 ).
  • r = max ( ((x+lastans) mod N)+1, ((y+lastans) mod N)+1 ).
  • lastans[1] = 0 , lastans[i] = Query[i-1].

Given M queries, your program must output the results of these queries. (0 ≤ M ≤ 6000).

IMPORTANT : PROBLEM ENHANCED. ( I'M SO SORRY.. )

Input

  • The first line of the input file contains 2 numbers : N M.
  • In the second line, N numbers follow.
  • M lines follow, where line i contains 2 numbers xi and yi.

Output

Your program should output the results of the M queries, one query per line.

Example

Input:
3 3
1 4 3
0 1
0 1
4 3

Output:
5
7
7

Added by:Fotile
Date:2012-04-23
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All except: ASM64
Resource:seter

hide comments
2016-10-14 17:58:14 [Rampage] Blue.Mary
The time limit is ridiculous, my first program runs for 0.56 second in the worst test case and get TLE. After added a little constant optimization it gets AC.
2012-04-27 04:25:05 Seter
@Devil D
0 1->(0+0)mod3+1 (1+0)mod3+1->1 2->lastans=5
0 1->(0+5)mod3+1 (1+5)mod3+1->1 3->lastans=7
4 3->(4+7)mod3+1 (3+7)mod3+1->2 3->lastans=7
2012-04-26 11:48:17 Siarhei Kulik
@seter, agree..
2012-04-26 11:08:04 Devil D
What is the significance of x & y,
if the range for i and j is between l & r...

Can you please explain the sample use case

Last edit: 2012-04-26 11:08:43
2012-04-26 10:17:28 Seter
@sk Easy problems are seldom needed..
2012-04-25 17:12:24 Siarhei Kulik
Thanks, enhanced version is more interesting than the previous one. But I think that next time it would be better to make one more problem (well, with another testdata and some changes in the statement) instead of changing the existing one.
© Spoj.com. All Rights Reserved. Spoj uses Sphere Engine™ © by Sphere Research Labs.