Educational Codeforces Round 6 F. Xors on Segments 暴力

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]);
}
    原文作者:qscqesze
    原文地址: https://www.cnblogs.com/qscqesze/p/5217153.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞