CF 983B XOR-pyramid(区间dp 加 异或)

For an array bb of length mm we define the function ff as

f(b)={b[1]f(b[1]b[2],b[2]b[3],,b[m1]b[m])if m=1otherwise,f(b)={b[1]if m=1f(b[1]⊕b[2],b[2]⊕b[3],…,b[m−1]⊕b[m])otherwise,

where  is bitwise exclusive OR.

For example, f(1,2,4,8)=f(12,24,48)=f(3,6,12)=f(36,612)=f(5,10)=f(510)=f(15)=15f(1,2,4,8)=f(1⊕2,2⊕4,4⊕8)=f(3,6,12)=f(3⊕6,6⊕12)=f(5,10)=f(5⊕10)=f(15)=15

You are given an array aa and a few queries. Each query is represented as two integers ll and rr. The answer is the maximum value of ff on all continuous subsegments of the array al,al+1,,aral,al+1,…,ar.

Input

The first line contains a single integer nn (1n50001≤n≤5000) — the length of aa.

The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai23010≤ai≤230−1) — the elements of the array.

The third line contains a single integer qq (1q1000001≤q≤100000) — the number of queries.

Each of the next qq lines contains a query represented as two integers llrr (1lrn1≤l≤r≤n).

Output

Print qq lines — the answers for the queries.

Examples
Input
3
8 4 1
2
2 3
1 2
Output
5
12
Input
6
1 2 4 8 16 32
4
1 6
2 5
3 4
1 2
Output
60
30
12
3
Note

In first sample in both queries the maximum value of the function is reached on the subsegment that is equal to the whole segment.

In second sample, optimal segment for first query are [3,6][3,6], for second query — [2,5][2,5], for third — [3,4][3,4], for fourth — [1,2][1,2].


题意:给出一个区间,让你按照题意上给出的那种异或方式异或,找出这个区间内包含的区间的最大的数;

思路:眨眼一看以为是线段树咧,看了他们的异或方式,区间dp也可以写;

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define Max 5005
int dp[Max][Max];   // dp[i][j] 存它题意上给要求那样异或 
int s[Max][Max];    // s[i][j] 存i,j区间内包含的区间中最大的值; 
int a[Max];          
int n;

void fff(int a[Max])
{
	int i,j;
	for(i = 0;i<n;i++)
	{
		dp[i][i] = a[i];
		s[i][i] = a[i];
	}
		
	
	for(int len = 1;len<n;len++)
	{
		for(i = 0,j = len; j<n;i++,j++)
		{
			dp[i][j] = dp[i][j-1]^dp[i+1][j];
			s[i][j] = max(max(s[i][j-1],s[i+1][j]),dp[i][j]);
		}
	}
}
int main()
{
	int i,j;
	while(~scanf("%d",&n))
	{
		for(i = 0;i<n;i++)
			scanf("%d",&a[i]);
		fff(a);
		int q;
		scanf("%d",&q);
		while(q--)
		{
			int x,y;
			scanf("%d%d",&x,&y);
			printf("%d\n",s[x-1][y-1]);
		}
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/obsorb_knowledge/article/details/80411127
今日推荐