F. Xors on Segments

158 阅读2分钟

output

standard output

You are given an array with n integers a**i and m queries. Each query is described by two integers (l**j, r**j).

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

For each query print the maximal value of the function f(a**x, a**y) over all l**j ≤ x, y ≤ r**j,  a**x ≤ a**y.

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 a**i (1 ≤ a**i ≤ 106) — the elements of the array a.

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

Output

For each query print the value a**j on a separate line — the maximal value of the function f(a**x, a**y) over all l**j ≤ x, y ≤ r**j,  a**x ≤ a**y. 输出标准输出 给你一个有n个整数ai和m个查询的数组。每个查询由两个整数(lj, rj)描述。

让我们定义一下函数. 该函数只对u≤v定义。

对于每个查询,打印所有lj≤x,y≤rj,ax≤ay的函数f(ax, ay)的最大值。

输入 第一行包含两个整数n,m(1≤n≤5-104,1≤m≤5-103)--数组的大小和查询的数量。

第二行包含n个整数ai(1≤ai≤106)--数组a的元素。

接下来的m行包含两个整数lj, rj (1 ≤ lj ≤ rj ≤ n) - 第j个查询的参数。

输出 对于每个查询,在单独的一行中打印值aj--函数f(ax, ay)在所有lj ≤ x, y ≤ rj, ax ≤ ay上的最大值。

Code

#include<bits/stdc++.h>
using namespace std;
const int N=1e6;
int l[N],r[N];
int pre[N],ans[N],f[N],a[N];
int n,m;
int ff(int l,int r)
{
    if(l>r)swap(l,r);
    return pre[r]^pre[l-1];
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    for(int i=1; i<=1e6; i++)
        pre[i]=(i^pre[i-1]);
    cin>>n>>m;
    for(int i=1; i<=n; i++)
        cin>>a[i];
    for(int i=1; i<=m; i++)
    {
        cin>>l[i]>>r[i];
    }
    for(int i=1; i<=n; i++)
    {
        f[i]=a[i];
        for(int j=i+1; j<=n; j++)
        {
            f[j]=max(f[j-1],ff(a[i],a[j]));
        }
        for(int j=1; j<=m; j++)
        {
            if(l[j]<=i)
            {
                ans[j]=max(ans[j],f[r[j]]);
            }
        }
    }
    for(int i=1; i<=m; i++)
    {
        cout<<ans[i]<<"\n";
    }
    return 0;
}