题目传送门

题目描述:给你一个f函数,这个函数的自变量是一个数列,函数表达式就是题目所给的描述,然后给你一个数列,问你数列中某区间  怎么选取 可以使函数值最大。

题目思路:  有关区间选取的问题,很容易想到dp,dp[l][r]代表从l到r的区间函数值,然后发现一个神奇的事情,就是:

dp[l][r]=dp[l][r-1]^dp[l+1][r],这是为什么呢,请允许我用拙劣的画技画一幅通俗易懂的图。

怎么样,是不是很通俗易懂呀,实现算出了1-3的值,如果要算1-4的值,那就是在后面再加一个圈,红色的线代表因为这个圈新加的内容,最后发现1-4  =   1-3  ^  2-4。

所以 我们只需要枚举区间长度,就可以算出每个区间的值了。

看到这里好像以为这道题做完了,只需要算出值,最后查询l-r中所有子区间的最大值就可以了嘛,然而这样会超时,因为n是5000,如果这样做需要2n^2的复杂度,于是亮点来啦。

用一个ans数组,在dp的过程中就记录答案,也就是说,dp数组记录的是i到j的函数值,而ans数组则记录i到j的最大值,然后读入查询的l和r,o(1)输出答案。(完全离线输出)

#include<iostream>
#include<algorithm>
#include<cstdlib>
#include<sstream>
#include<cstring>
#include<bitset>
#include<cstdio>
#include<string>
#include<deque>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
using namespace std;
typedef long long ll;
const int maxn=1010;
char mp[120][120];
using namespace std;
typedef long long ll;
int a[6000];
int f[6000][6000];
int ans[6000][6000];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
f[i][i]=a[i];//单个值就是本身
ans[i][i]=a[i];
}
for(int len=2;len<=n;len++){//枚举区间长度
for(int i=1,j=i+len-1 ; j<=n ; i++,j++)
{
f[i][j]=f[i][j-1]^f[i+1][j];
ans[i][j]=max( f[i][j] , max ( ans[i][j-1],ans[i+1][j]) );//预处理
}
}
int q,l,r;
cin>>q;
while(q--)
{
cin>>l>>r;
printf("%d\n",ans[l][r]);
}
}
D. 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].

codeforces-984D——XOR-pyramid(DP)的更多相关文章

  1. Codeforces Gym101341K:Competitions(DP)

    http://codeforces.com/gym/101341/problem/K 题意:给出n个区间,每个区间有一个l, r, w,代表区间左端点右端点和区间的权值,现在可以选取一些区间,要求选择 ...

  2. codeforces 711C Coloring Trees(DP)

    题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...

  3. codeforces#1154F. Shovels Shop (dp)

    题目链接: http://codeforces.com/contest/1154/problem/F 题意: 有$n$个物品,$m$条优惠 每个优惠的格式是,买$x_i$个物品,最便宜的$y_i$个物 ...

  4. Codeforces 1051 D.Bicolorings(DP)

    Codeforces 1051 D.Bicolorings 题意:一个2×n的方格纸,用黑白给格子涂色,要求分出k个连通块,求方案数. 思路:用0,1表示黑白,则第i列可以涂00,01,10,11,( ...

  5. Codeforces 1207C Gas Pipeline (dp)

    题目链接:http://codeforces.com/problemset/problem/1207/C 题目大意是给一条道路修管道,相隔一个单位的管道有两个柱子支撑,管道柱子高度可以是1可以是2,道 ...

  6. Codeforces 704C - Black Widow(dp)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 感觉这种题被评到 *2900 是因为细节太繁琐了,而不是题目本身的难度,所以我切掉这种题根本不能说明什么-- 首先题目中有一个非 ...

  7. Codeforces 682B New Skateboard(DP)

    题目大概说给一个数字组成的字符串问有几个子串其代表的数字(可以有前导0)能被4整除. dp[i][m]表示字符串0...i中mod 4为m的后缀的个数 通过在i-1添加str[i]字符转移,或者以st ...

  8. Codeforces 627A XOR Equation(思路)

    题目大概说两个正整数a.b,已知s=a+b以及x=a xor b的值,问有几种a.b这样的数对. 我知道异或相当于无进位的加法,s-x就是其各个位置的进位,比如s-x=1010,那就表示a和b的第1位 ...

  9. Codeforces 543D Road Improvement(DP)

    题目链接 Solution 比较明显的树形DP模型. 首先可以先用一次DFS求出以1为根时,sum[i](以i为子树的根时,满足要求的子树的个数). 考虑将根从i变换到它的儿子j时,sum[i]产生的 ...

  10. Codeforces 543C Remembering Strings(DP)

    题意比较麻烦 见题目链接 Solution: 非常值得注意的一点是题目给出的范围只有20,而众所周知字母表里有26个字母.于是显然对一个字母进行变换后是不影响到其它字符串的. 20的范围恰好又是常见状 ...

随机推荐

  1. Android添加Menu菜单

    在安卓中添加Menu菜单十分简单. 步骤: 1.在menu文件夹中的main.xml文件中添加要添加的项目. <menu xmlns:android="http://schemas.a ...

  2. python的文件锁使用

    python的文件锁目前使用的是fcntl这个库,它实际上为 Unix上的ioctl,flock和fcntl 函数提供了一个接口. 1.fcntl库的简单使用 import fcntl import ...

  3. 给Activity切换过程添加动画效果

    首先,在资源文件中定义一些动画效果 例如: <scale android:duration="@android:integer/config_mediumAnimTime" ...

  4. day18-事务与连接池 5.关于不考虑事务隔离性出现的问题

  5. ???Spring集成MyBatis02 【不推荐使用,了解即可】

    2017年5月19日09:31:22 由于该种方法比较麻烦,所以三少暂时不更新,哈哈哈:待更新...

  6. 关于c#里的集合的,结构体,枚举的定义,解释与应用

    那么先写一下 集合 . 集合和数组很相似,数组里的类型是必须同一类型,固定长度.然而集合里的可以是不同类型,不固定长度的.所以集合运用的灵活度要更高一些. 要使用集合,必须先引用命名空间:using ...

  7. 数据库之_CRUD

    CRUD是指在做计算处理时的增加(Create).读取查询(Retrieve).更新(Update)和删除(Delete)几个单词的首字母简写.主要被用在描述软件系统中DataBase或者持久层的基本 ...

  8. DIY的RPM包怎么签名呢 - 笔记

    参考 https://gist.github.com/fernandoaleman/1376720 如果打不开上一个连接,请参考https://www.cnblogs.com/LiuYanYGZ/p/ ...

  9. IDEA工作中常用快捷键

    ctrl+shift+t: Ubuntu中在一个工具栏中打开两个终端 shift+shift: 搜索任何类 ctrl+N: 搜索任何类 ctrl+right: forward----自定义 ctrl+ ...

  10. 【Arcgis android】 离线编辑实现及一些代码段

    Arcgis android 离线编辑实现及一些代码段 底图添加 private String path="file:///mnt/sdcard/data/chinasimple.tpk&q ...