codeforces 620F. Xors on Segments

题目链接

定义一种操作f(u, v) = u^u+1^.......^v。 (u<=v), 给n个数, q个询问, 每个询问给出一个区间[l, r], 求这个区间里的f(a[i], a[j]) (l<=i<=j<=r)的最大值。

一开始竟然用n^2m的方法, 真是有点脑残..

#include <iostream>
#include <vector>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <queue>
#include <stack>
#include <bitset>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, n, a) for(int i = a; i<n; i++)
#define fi first
#define se second
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int mod = 1e9+7;
const int inf = 1061109567;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
int a[50005], ans[5005], b[1000005], dp[50005];
struct node
{
    int l, r;
}q[5005];
int main()
{
    int n, m;
    cin>>n>>m;
    for(int i = 1; i<=n; i++) {
        scanf("%d", &a[i]);
    }
    for(int i = 1; i<=1000000; i++) {
        b[i] = i^b[i-1];
    }
    for(int i = 0; i<m; i++) {
        scanf("%d%d", &q[i].l, &q[i].r);
    }
    for(int i = 1; i<=n; i++) {
        dp[i] = a[i];
        for(int j = i+1; j<=n; j++) {
            int minn = min(a[i], a[j]);
            int maxx = max(a[i], a[j]);
            int num = b[maxx]^b[minn-1];
            dp[j] = max(dp[j-1], num);
        }
        for(int j = 0; j<m; j++) {
            if(i>=q[j].l&&i<=q[j].r) {
                ans[j] = max(ans[j], dp[q[j].r]);
            }
        }
    }
    for(int i = 0; i<m; i++)
        printf("%d\n", ans[i]);
    return 0;
}
时间: 2024-10-06 04:17:05

codeforces 620F. Xors on Segments的相关文章

Codeforces 620F Xors on Segments(暴力+DP)

题目链接 Xors on Segments 预处理出x[i] = 1 ^ 2 ^ 3 ^ -- ^ i; (话说这题O(N^2居然能过)) 先对询问离线. 然后dp[i]表示以a[i]为开头的所有连续序列中最大答案. 然后依次处理到a[j]的时候如果以j为右端点的询问的左端点小于等于i则更新. 复杂度O(N^2) 1 #include <bits/stdc++.h> 2 3 using namespace std; 4 5 #define rep(i, a, b) for (int i(a);

Codeforces 620F Xors on Segments 回滚莫队 + 字典树 || 离心询问分治 + 可持久化字典树

Xors on Segments 转换一下变成询问区间选两个数异或的最大值, 要注意的是一个数作为左端点要-1, 所以在回滚莫队的时候用两棵字典树维护. 这个题居然n ^ 2 也能过...  其实用分治 + 可持久化字典树可以做到n * log(n) * log(n), 懒得写了... #include<bits/stdc++.h> #define LL long long #define LD long double #define ull unsigned long long #defin

Codeforces 430A Points and Segments (easy)

题意:让你染色点,要求在给出的区间内|红色个数-蓝色个数|<=1 思路:排序后依次交替染色就能达到效果 #include <iostream> #include <cstring> #include <algorithm> #include <cstdio> #include <vector> using namespace std; const int MAXN = 110; int arr[MAXN]; int n,m,x,y; int

CodeForces A. Points in Segments

http://codeforces.com/contest/1015/problem/A You are given a set of nn segments on the axis OxOx, each segment has integer endpoints between 11 and mm inclusive. Segments may intersect, overlap or even coincide with each other. Each segment is charac

Codeforces 1108E2 Array and Segments (Hard version) 差分, 暴力

Codeforces 1108E2 E2. Array and Segments (Hard version) Description: The only difference between easy and hard versions is a number of elements in the array. You are given an array \(a\) consisting of \(n\) integers. The value of the \(i\)-th element

CodeForces 610D Vika and Segments

题目链接: http://codeforces.com/problemset/problem/610/D ------------------------------------------------------------------------------------ 虽然说这题是线段并 但是如果会写矩形面积并的话就直接写矩形并不用考虑那么多了 不过这里额外说明下 写矩形面积并的线段树的时候 要注意到某一段 $ -1 $一定是在这一段$ +1 $之后才会出现的操作 因此标记就是这一段被“完

codeforces 430A Points and Segments (easy)(理解能力有待提高……)

题目 //终于看懂题目了,,,, //一条线段里面不是每个坐标上都有要染色的点,所以为了满足条件,只能考虑那些给出坐标的点 //所以就要排序一下了,不能直接根据坐标0 1 0 1…… #include <cstdio> #include <cstring> #include <algorithm> using namespace std ; struct tt { int a,b; }aa[110]; int cmp(tt x,tt y) { return x.a<

Codeforces 193 D. Two Segments(线段树)

机智的线段树题, 参考了这个题解http://www.cnblogs.com/keam37/p/4335914.html 代码: #include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef long long ll; #define lson(x) ((x<<1)+1) #define rson(x) ((x<<1)+2) const

Codeforces 610D Vika and Segments 线段树+离散化+扫描线

可以转变成上一题(hdu1542)的形式,把每条线段变成宽为1的矩形,求矩形面积并 要注意的就是转化为右下角的点需要x+1,y-1,画一条线就能看出来了 #include<bits/stdc++.h> #define pi acos(-1.0) #define ll long long #define mod 1000000007 #define ls l,m,rt<<1 #define rs m+1,r,rt<<1|1 #pragma comment(linker,