XOR-pyramid
time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

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[m−1]⊕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(1⊕2,2⊕4,4⊕8)=f(3,6,12)=f(3⊕6,6⊕12)=f(5,10)=f(5⊕10)=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 (1≤n≤50001≤n≤5000) — the length of aa.

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

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

Each of the next qq lines contains a query represented as two integers ll, rr (1≤l≤r≤n1≤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].

给n个数,询问q次,每次询问给出l,r.   [l,r]区间求异或最大值为多少。一开始没看清是最大值,还以为题目错了。

区间【1,6】和区间【2,5】比较一下就知道很多会重复,所以把它们记下来节省时间。

此题需要记忆化两次。区间动态规划。

我用b数组来存储所以异或的值,dp数来存储最大值。

拿第二个样例:

b数组这样得来:

b[1]这一排还是a数组

b[i][j]=b[i-1][j]^b[i-1][j+1];

dp数组这样的来:

dp[0]这一排还是a数组

dp[i][j]=max( dp[i-1][j], dp[i-1][j+1],b[i][j] );

  这是dp数组

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define maxn 110
#define maxm 10010
#define inf 0x3f3f3f
using namespace std;
int b[][];
int dp[][];
int a[];
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
dp[][i]=a[i];
}
for(int i=;i<=n-;i++)
{
b[][i]=a[i]^a[i+];
dp[][i]=max(a[i],a[i+]);//第一排也是要比较得到dp[1][i],否则第三个样例wa
dp[][i]=max(b[][i],dp[][i]);
}
for(int i=;i<=n-;i++)
{
for(int j=;j<=n-i;j++)
{
b[i][j]=b[i-][j]^b[i-][j+];
}
}
for(int i=;i<=n-;i++)
{
for(int j=;j<=n-i;j++)
{
dp[i][j]=max(dp[i-][j],dp[i-][j+]);
dp[i][j]=max(dp[i][j],b[i][j]);
}
}
for(int i=;i<=n-;i++)
{
for(int j=;j<=n-i;j++)
{
printf("%4d",dp[i][j]);
}
cout<<endl;
}
int q;
scanf("%d",&q);
while(q--)
{
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",dp[r-l][l]);
}
return ;
}

CodeForces - 983B XOR-pyramid(区间dp,异或)的更多相关文章

  1. Codeforces 983B. XOR-pyramid【区间DP】

    LINK 定义了一种函数f 对于一个数组b 当长度是1的时候是本身 否则是用一个新的数组(长度是原数组-1)来记录相邻数的异或,对这个数组求函数f 大概是这样的: \(f(b[1]⊕b[2],b[2] ...

  2. CF 983B XOR-pyramid(区间dp,异或)

    CF 983B XOR-pyramid(区间dp,异或) 若有一个长度为m的数组b,定义函数f为: \(f(b) = \begin{cases} b[1] & \quad \text{if } ...

  3. Codeforces - 149D 不错的区间DP

    题意:有一个字符串 s. 这个字符串是一个完全匹配的括号序列.在这个完全匹配的括号序列里,每个括号都有一个和它匹配的括号 你现在可以给这个匹配的括号序列中的括号染色,且有三个要求: 每个括号只有三种情 ...

  4. Codeforces.392E.Deleting Substrings(区间DP)

    题目链接 \(Description\) \(Solution\) 合法的子序列只有三种情况:递增,递减,前半部分递增然后一直递减(下去了就不会再上去了)(当然还要都满足\(|a_{i+1}-a_i| ...

  5. CodeForces - 1025D: Recovering BST (区间DP)

    Dima the hamster enjoys nibbling different things: cages, sticks, bad problemsetters and even trees! ...

  6. Codeforces 1114D Flood Fill (区间DP or 最长公共子序列)

    题意:给你n个颜色块,颜色相同并且相邻的颜色块是互相连通的(连通块).你可以改变其中的某个颜色块的颜色,不过每次改变会把它所在的连通块的颜色也改变,问最少需要多少次操作,使得n个颜色块的颜色相同. 例 ...

  7. Codeforces 958C3 - Encryption (hard) 区间dp+抽屉原理

    转自:http://www.cnblogs.com/widsom/p/8863005.html 题目大意: 比起Encryption 中级版,把n的范围扩大到 500000,k,p范围都在100以内, ...

  8. CodeForces 149D Coloring Brackets 区间DP

    http://codeforces.com/problemset/problem/149/D 题意: 给一个给定括号序列,给该括号上色,上色有三个要求 1.只有三种上色方案,不上色,上红色,上蓝色 2 ...

  9. 51Nod XOR key —— 区间最大异或值 可持久化字典树

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1295 1295 XOR key  题目来源: HackerRa ...

随机推荐

  1. SprigBoot核心技术

    启动原理 运行流程 自动配置原理 一.启动原理 SpringApplication.run(主程序类)– new SpringApplication(主程序类)• 判断是否web应用• 加载并保存所有 ...

  2. python 浮点数转分数

    from fractions import Fraction value = 4.2 print(Fraction(value).limit_denominator())

  3. Django框架(上传Excel文件并读取)

    博主今天整理下Django框架中上传Excel文件并读取 博主是要在管理平台中新增用例的维护功能,想着通过上传Excel文件来展示用例,下面是项目的路径图: 首先先建数据库模型 model.py 可以 ...

  4. Pave the Parallelepiped CodeForces - 1007B (计数)

    大意: 给定A,B,C, 求有多少个三元组$(a,b,c)$, 满足$a \le b \le c$, 且以若干个$(a,b,c)$为三边的长方体能填满边长(A,B,C)的长方体. 暴力枚举出$A,B, ...

  5. sed:轻量级流编辑器

    一. sed命令 sed是一种几乎包括在所有UNIX平台(包括Linux)的轻量级流编辑器.sed主要是用来将数据进行选取.替换.删除.新增的命令 注意:vi命令只能修改文件,但不能修改命令的结果,如 ...

  6. UVA-10285 Longest Run on a Snowboard (递推)

    题目大意:滑雪.给一个二维数组,找出最长的连续下降序列的长度. 题目分析:定义dp(i,j)表示以a[i][j]结尾的最长连续下降序列的长度,则dp(i,j)=max(dp(i-1,j),dp(i+1 ...

  7. UVALive-2966 King's Quest(强连通+二分图匹配)

    题目大意:有n个男孩和和n个女孩,已只每个男孩喜欢的女孩.一个男孩只能娶一个女孩.一个女孩只能嫁一个男孩并且男孩只娶自己喜欢的女孩,现在已知一种他们的结婚方案,现在要求找出每个男孩可以娶的女孩(娶完之 ...

  8. 尺取法——POJ3061

    #include <iostream> //nlogn复杂度的写法 #include <cstdio> #include <algorithm> using nam ...

  9. FindBugs插件的安装与使用

    转载:http://www.cnblogs.com/kayfans/archive/2012/06/18/2554022.html 1 什么是FindBugs FindBugs 是一个静态分析工具,它 ...

  10. Integer与int的种种比较你知道多少

    如果面试官问Integer与int的区别:估计大多数人只会说道两点,Ingeter是int的包装类,int的初值为0,Ingeter的初值为null. 但是如果面试官再问一下Integer i = 1 ...