F. Xors on Segments

题目连接:

http://www.codeforces.com/contest/620/problem/F

Description

You are given an array with n integers ai and m queries. Each query is described by two integers (lj, rj).

Let's define the function . The function is defined for only u ≤ v.

For each query print the maximal value of the function f(ax, ay) over all lj ≤ x, y ≤ rj,  ax ≤ ay.

Input

The first line contains two integers n, m (1 ≤ n ≤ 5·104,  1 ≤ m ≤ 5·103) — the size of the array and the number of the queries.

The second line contains n integers ai (1 ≤ ai ≤ 106) — the elements of the array a.

Each of the next m lines contains two integers lj, rj (1 ≤ lj ≤ rj ≤ n) – the parameters of the j-th query.

Output

For each query print the value aj on a separate line — the maximal value of the function f(ax, ay) over all lj ≤ x, y ≤ rj,  ax ≤ ay.

Sample Input

6 3

1 2 3 4 5 6

1 6

2 5

3 4

Sample Output

7

7

7

Hint

题意

题目中定义了f(i,j) = i(i+1)(i+2)...j

给你n个数,然后m次询问

每次问你l,r区间内,f(a[i],a[j])最大是多少,l<=i,j<=r

题解:

正解的话是莫队+字典树

复杂度是(n+m)lognsqrt(n)

但是这道题也有(n^2+nm)的算法,两个算法的复杂度差距不是很大

并且第二种好写的多……

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e6+5;
int f[maxn];
int a[maxn];
int G[maxn];
int l[maxn],r[maxn],ans[maxn];
int main()
{
for(int i=1;i<maxn;i++)
f[i]=f[i-1]^i;
int n,m;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
for(int i=1;i<=m;i++)
scanf("%d%d",&l[i],&r[i]);
for(int i=1;i<=n;i++)
{
int mx = 0;
for(int j=i;j<=n;j++)
{
int cnt = f[a[j]]^f[a[i]];
if(a[j]>a[i])
cnt^=a[i];
else
cnt^=a[j];
mx = max(cnt,mx);
G[j]=mx;
}
for(int j=1;j<=m;j++)
if(l[j]<=i&&i<=r[j])
ans[j]=max(ans[j],G[r[j]]);
}
for(int i=1;i<=m;i++)
printf("%d\n",ans[i]);
}

Educational Codeforces Round 6 F. Xors on Segments 暴力的更多相关文章

  1. Educational Codeforces Round 40 F. Runner's Problem

    Educational Codeforces Round 40 F. Runner's Problem 题意: 给一个$ 3 * m \(的矩阵,问从\)(2,1)$ 出发 走到 \((2,m)\) ...

  2. Educational Codeforces Round 61 F 思维 + 区间dp

    https://codeforces.com/contest/1132/problem/F 思维 + 区间dp 题意 给一个长度为n的字符串(<=500),每次选择消去字符,连续相同的字符可以同 ...

  3. Educational Codeforces Round 51 F. The Shortest Statement(lca+最短路)

    https://codeforces.com/contest/1051/problem/F 题意 给一个带权联通无向图,n个点,m条边,q个询问,询问两点之间的最短路 其中 m-n<=20,1& ...

  4. Educational Codeforces Round 12 F. Four Divisors 求小于x的素数个数(待解决)

    F. Four Divisors 题目连接: http://www.codeforces.com/contest/665/problem/F Description If an integer a i ...

  5. Educational Codeforces Round 26 F. Prefix Sums 二分,组合数

    题目链接:http://codeforces.com/contest/837/problem/F 题意:如题QAQ 解法:参考题解博客:http://www.cnblogs.com/FxxL/p/72 ...

  6. Educational Codeforces Round 9 F. Magic Matrix 最小生成树

    F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...

  7. Educational Codeforces Round 7 F. The Sum of the k-th Powers 拉格朗日插值法

    F. The Sum of the k-th Powers 题目连接: http://www.codeforces.com/contest/622/problem/F Description Ther ...

  8. Educational Codeforces Round 8 F. Bear and Fair Set 最大流

    F. Bear and Fair Set 题目连接: http://www.codeforces.com/contest/628/problem/F Description Limak is a gr ...

  9. Educational Codeforces Round 14 - F (codeforces 691F)

    题目链接:http://codeforces.com/problemset/problem/691/F 题目大意:给定n个数,再给m个询问,每个询问给一个p,求n个数中有多少对数的乘积≥p 数据范围: ...

随机推荐

  1. linux编程之信号

    信号(signal)机制是UNIX系统中最为古老的进程之间的通信机制,它用在一个或多个进程之间传递异步信号,信号可以由各种异步事件产生,如: 键盘中断等等,在Linux 的shell 中,也可以使用信 ...

  2. DSP学习教程基于28335(一)

    首先说明:开发环境Manjaro linux,内核5.0,滚动升级版本,随时都是最新,CCS也是最新的CCv 8 #include "DSP2833x_Device.h" // 这 ...

  3. Yii 1.1.17 四、属性标签、AR类增删改查、使用上传类与扩展第三方类库

    一.属性标签与规则设置 当进入网站页面,将会读数据库返回信息到视图上.那么,现在定义模型中的属性在视图标签上的显示, 也就是模型属性到前台标签的映射 // 定义模型属性到前台标签的映射 public ...

  4. python基础===requests学习笔记

    这里有一个新的学习requests网站:http://docs.python-requests.org/zh_CN/latest/user/quickstart.html2017/11/30 Requ ...

  5. 【bzoj4868】期末考试

    我还第一次见到省选考三分……? #include<bits/stdc++.h> #define N 200005 using namespace std; typedef long lon ...

  6. Makefile 跟着走快点

    引言  - 从"HelloWorld"开始 Makefile 是Linux C 程序开发最重要的基本功. 代表着整个项目编译和最终生成过程.本文重点是带大家了解真实项目中那些简易的 ...

  7. JDBC数据源连接池(2)---C3P0

    我们接着<JDBC数据源连接池(1)---DBCP>继续介绍数据源连接池. 首先,在Web项目的WebContent--->WEB-INF--->lib文件夹中添加C3P0的j ...

  8. Storm实战常见的问题

    该文档为实实在在的原创文档,转载请注明: http://blog.sina.com.cn/s/blog_8c243ea30101k0k1.html 类型 详细 备注 该文档是群里几个朋友在storm实 ...

  9. JAVA二叉树的创建以及各种功能的实现

    直接上代码了,代码说得很清楚了 package BTree; public class BTree { private Node root; private class Node { private ...

  10. sublime text按esc经常进入command mode(不能输入任何东西)

    在使用sublime text进行 选中 操作中,如果使用了esc退出选中状态,会进入command mode,现象是不能输入任何东西,关闭当前编辑文件重新打开可以解决.但是很影响连贯性.可以通过一些 ...