codeforce 983B XOR-pyramid



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

f(b)={b[1]if m=1f(b[1]b[2],b[2]b[3],,b[m1]b[m])otherwise,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 ffon 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 ll, rr (1lrn1≤l≤r≤n).

Output

Print qq lines — the answers for the queries.

Examples
input
Copy
3
8 4 1
2
2 3
1 2
output
Copy
5
12
input
Copy
6
1 2 4 8 16 32
4
1 6
2 5
3 4
1 2
output
Copy
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数组。

动态转移方程:dp[i][j] = max(dp[i+1][j],dp[i][j-1],dp[i+1][j]^dp[i][j-1]);

#include <bits/stdc++.h>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define inf 1e9
#define max(a,b) a<b?b:a
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int _max = 5005;
ll dp[_max][_max];
ll ans[_max][_max];
int main(){
	int n,q;
	while(cin>>n){
		for(int i = 1 ; i <= n ; i++){
			cin>>dp[i][i];	
			ans[i][i] = dp[i][i];
		}
		for(int k = 1 ; k < n ; k++){
			for(int i = 1 ; i+k <= n ; i++){
				int j = i+k;
				dp[i][j] = dp[i][j-1]^dp[i+1][j];
				ans[i][j] = max(ans[i][j-1],ans[i+1][j]);
				ans[i][j] = max(ans[i][j],dp[i][j]);
			//	cout<<ans[i][j]<<' ';
			}
		//	cout<<endl;
		}
		cin>>q;
		int L,R;
		while(q--){
			cin>>L>>R;
			cout<<ans[L][R]<<endl;
		}
	}
	return 0;
}



猜你喜欢

转载自blog.csdn.net/qq_38987374/article/details/80348627
今日推荐