zoukankan      html  css  js  c++  java
  • [SCOI 2016]美味

    Description

    题库链接

    给你一个长度为 (n) 的序列 (A)(m) 组询问 ((b,x,l,r)) 询问 [max_{i=l}^r boplus (A_i+x)]

    (1leq nleq 2cdot 10^5,1leq m,A_i,b,xleq 10^5)

    Solution

    还是按位贪心。

    先找到在区间内是否有满足的当前位的 (A) 值。再逐步缩小范围查找。

    具体为:

    假设我们已经处理到 (b) 的第 (i) 位(转换成二进制),假设是 (1)(0) 同理)。

    那么我们只需要查找是否存在 (A_j+x) 使得其二进制第 (i) 位数字是 (0) ,显然我们已经处理了前 (i-1) 位了, (i) 位前的限制为 (last) ,那么我们需要查找的数的大小就是在区间 ([last-x,last+(1<<i)-1-x]) ,手算一下就知道这个区间里的数字的第 (i) 位加了 (x) 后就都是 (0)

    那么现在我们就是要在 (A_lsim A_r) 中找出是否存在于 ([last-x,last+(1<<i)-1-x]) 的数字,两个区间范围限制,用主席树模板一套就好了。

    Code

    //It is made by Awson on 2018.3.4
    #include <bits/stdc++.h>
    #define LL long long
    #define dob complex<double>
    #define Abs(a) ((a) < 0 ? (-(a)) : (a))
    #define Max(a, b) ((a) > (b) ? (a) : (b))
    #define Min(a, b) ((a) < (b) ? (a) : (b))
    #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
    #define writeln(x) (write(x), putchar('
    '))
    #define lowbit(x) ((x)&(-(x)))
    using namespace std;
    const int N = 2e5;
    void read(int &x) {
        char ch; bool flag = 0;
        for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
        for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
        x *= 1-2*flag;
    }
    void print(int x) {if (x > 9) print(x/10); putchar(x%10+48); }
    void write(int x) {if (x < 0) putchar('-'); print(Abs(x)); }
    
    int n, m, a, L, R, b, x, bin[20];
    struct Segment_tree {
        int root[N+5], ch[N*50+5][2], key[N*50+5], pos;
        int cpynode(int o) {++pos, ch[pos][0] = ch[o][0], ch[pos][1] = ch[o][1], key[pos] = key[o]; return pos; }
        void insert(int &o, int l, int r, int loc) {
        o = cpynode(o); ++key[o];
        if (l == r) return; int mid = (l+r)>>1;
        if (loc <= mid) insert(ch[o][0], l, mid, loc);
        else insert(ch[o][1], mid+1, r, loc);
        }
        int query(int o1, int o2, int l, int r, int a, int b) {
        if (a <= l && r <= b) return key[o2]-key[o1]; int mid = (l+r)>>1;
        int c1 = 0, c2 = 0;
        if (a <= mid) c1 = query(ch[o1][0], ch[o2][0], l, mid, a, b);
        if (b > mid) c2 = query(ch[o1][1], ch[o2][1], mid+1, r, a, b);
        return c1+c2;
        }
    }T;
    
    void work() {
        read(n), read(m); bin[0] = 1; for (int i = 1; i <= 18; i++) bin[i] = bin[i-1]<<1;
        for (int i = 1; i <= n; i++) read(a), T.insert(T.root[i] = T.root[i-1], 0, N, a);
        while (m--) {
        read(b), read(x), read(L), read(R);
        int last = 0, ans = 0;
        for (int i = 18; i >= 0; i--) {
            int tmp = last+((bin[i]&b)^bin[i]);
            int l = Max(tmp-x, 0), r = Min(N, tmp-x+bin[i]-1);
            if (l > r) {last += (bin[i]&b); continue; }
            if (T.query(T.root[L-1], T.root[R], 0, N, l, r)) last = tmp, ans += bin[i];
            else last += (bin[i]&b);
        }
        writeln(ans);
        }
    }
    int main() {
        work(); return 0;
    }
  • 相关阅读:
    scss文件报错处理 (报错信息Invalid CSS after "v": expected 1 selector or at-rule, was 'var api = require)
    vue-countdown组件
    vue dayjs in ./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js
    vue you can run: npm install --save !!vue-styles-loader!css-loader?
    解决npm报错:Module build failed: TypeError: this.getResolve is not a function
    【JVM从小白学成大佬】3.深入解析强引用、软引用、弱引用、幻象引用
    【JVM从小白学成大佬】2.Java虚拟机运行时数据区
    【JVM从小白学成大佬】开篇
    【必知必会】深入解析强引用、软引用、弱引用、幻象引用
    【不做标题党,只做纯干货】HashMap在jdk1.7和1.8中的实现
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/8504765.html
Copyright © 2011-2022 走看看