F. Xors on Segments

143 阅读1分钟

题目

Problem - F - Codeforces

F. Xors on Segments

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. F. 段上的外线

您将获得一个包含 n 个整数的数组 a_i 和 m 查询。每个查询由两个整数(l_j、r_j)描述。

让我们定义函数。该函数仅针对 u ≤ v 定义。

对于每个查询,打印函数 f(a_x, a_y) 在所有 x、y ≤ r_j a_x ≤ a_y l_j ≤上的最大值。

输入

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

The second line contains n integers a_i (1 ≤ a_i ≤ 1e6) — 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. 第一行包含两个整数 n, m (1 ≤ n ≤ 5·1e4, 1 ≤ m ≤ 5·1e3) — 数组的大小和查询的数量。

第二行包含 n 个整数a_i (1 ≤ a_i ≤ 1e6) — 数组 a 的元素。

接下来的 m 行中的每一行都包含两个整数,l_j,r_j (1 ≤ l_j ≤ r_j ≤ n) – 第 j 个查询的参数。

解法

预处理1+1暴力

Code

#include <iostream> #include <cstring> #include <algorithm> #include <map> #include <queue> #include <vector> #include <cmath> #include <set> #include <stack> #include <bits/stdc++.h>
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;
}