题目传送门

题目描述:给你一个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].

转载于:https://www.cnblogs.com/mountaink/p/9536718.html

codeforces-984D——XOR-pyramid(DP)相关推荐

  1. codeforces 711C Coloring Trees(DP)

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

  2. Codeforces 626F Group Projects (DP)

    题目链接  8VC Venture Cup 2016 - Elimination Round 题意  把$n$个物品分成若干组,每个组的代价为组内价值的极差,求所有组的代价之和不超过$k$的方案数. ...

  3. Codeforces 803E--Roma and Poker (DP)

    原题链接:http://codeforces.com/problemset/problem/803/E 题意:给一个n长度的字符串,其中'?'可以替换成'D'.'W'.'L'中的任意一种,'D'等价于 ...

  4. CodeForces - 835D Palindromic characteristics (dp)

    题目链接:http://codeforces.com/problemset/problem/835/D点击打开链接 D. Palindromic characteristics time limit ...

  5. CodeForces 176B Word Cut(DP)

    题意:给你a串和b串,你能切k次,每次切完将尾部分放在头的前面,问有多少种方案切k次从a串变为b串 思路:令dp[i][0]为砍了i次变成b串的方案数,dp[i][1]为砍了i次变成非b串的方案数,然 ...

  6. CodeForces 14 E.Camels(dp)

    Description 构造序列y1,...,ny_{1,...,n}满足: 1.有tt个j(2≤j≤n−1)j(2\leq j\leq n-1)满足yj−1<yj>yj+1y_{j-1} ...

  7. 求三角形最大面积(DP)

    求三角形最大面积(DP) 在OJ上奇迹般WA了:WA:70. Why? #include <iostream> #include <string.h> using namesp ...

  8. LeetCode 编辑距离 II(DP)

    1. 题目 给你两个单词 s 和 t,请你计算出将 s 转换成 t 所使用的最少操作数. 你可以对一个单词进行如下两种操作: 删除一个字符 替换一个字符 注意: 不允许插入操作 题目保证有解 示例: ...

  9. LeetCode 1220. 统计元音字母序列的数目(DP)

    文章目录 1. 题目 2. 解题 1. 题目 给你一个整数 n,请你帮忙统计一下我们可以按下述规则形成多少个长度为 n 的字符串: - 字符串中的每个字符都应当是小写元音字母('a', 'e', 'i ...

  10. LeetCode 265. 粉刷房子 II(DP)

    文章目录 1. 题目 2. 解题 1. 题目 假如有一排房子,共 n 个,每个房子可以被粉刷成 k 种颜色中的一种,你需要粉刷所有的房子并且使其相邻的两个房子颜色不能相同. 当然,因为市场上不同颜色油 ...

最新文章

  1. python零基础难学吗-如何从零开始学习Python,零基础学python难吗
  2. java实现选择排序 带打印,选择排序算法的JAVA实现
  3. 无人出租车被警察截停后逃逸!AI:我当时害怕极了
  4. OpenStack精华问答 | OpenStack 网络中 OpenFlow 规则的作用是什么?
  5. C++ 以对象管理资源
  6. 很高兴,自己申请到了一个.net的blog
  7. 2021江西高考成绩查询方式6,2021年江西高考成绩6月23日公布 多种查分方式
  8. java并发编程实践之安全发布和逸出
  9. 代码背景护眼色设置RGB
  10. 计算机上什么键有存储,计算器存储运算键是什么?
  11. 可编程串行通信接口芯片8251A
  12. 优秀的程序员是没有性生活的
  13. 【Python】python转义字符
  14. 小程序支付:appid和mch_id不匹配采坑实录
  15. Swing Copters摇摆直升机高分攻略,游戏攻略
  16. 重装系统后mysql不用重新安装
  17. Error: Registry key ‘Software\JavaSoft\Java Runtime Environment’\CurrentVersion’
  18. js 的数组怎么push一个对象
  19. 多元正态分布的性质和定理
  20. thinkpad 重装--AHCI 导致系统蓝屏---迅盘

热门文章

  1. 计算机视觉工作项目方案设计,机器视觉(项目方案设计案例)47.pdf
  2. OpenCart如何添加货币
  3. 数据库服务器修改地址,数据库服务器修改地址吗
  4. 项目缺少包如何和服务器,解决缺少服务器依赖包问题
  5. 【learn】learn1
  6. 发布到服务器接口404_新版本永雾林渊周五来袭,404战队真的404了
  7. [Silverlight 4 RC]WebBrowser概览
  8. go tcp连接_在Go中构建并发TCP服务器样例
  9. 插入始终是1_插入式电磁流量计的安装说明
  10. 走在网页游戏开发的路上——页游资源管理