Codeforces Round 932 (Div. 2) ---- E. Distance Learning Courses in MAC ---- 题解

E. Distance Learning Courses in MAC:

题目大意:

思路解析:

// 对于这种二进制多个数计算答案,我们应该灵敏的想到是否可以通过枚举二进制位来计算答案。

就是对每一个查询找出或和的最大值,那我们想xi 和 yi中哪些位一定会出现在答案中,假设为25 和 31,他们两转为二进制为 (11001) 和 (11111)我们可以想到24一定会进入答案,如果它不是答案的一部分,那无论怎么选都无法满足选择的数大于等于x。那我们这样就可以对[l,r]的答案进行简单计算(这里利用线段树或者树状数组的区间查询即可),那后续剩下的答案怎么办。后续 x 和 y剩下的二进制位为 (00001) 和 (0111)我们发现对r的剩余无论选择哪一位都可以满足大于等于x,那我们可以对于这些剩下的数做一个前缀和的处理,如果有我们一定会把它假如答案。

那么如果假如剩下 有00100这一位,之前粗略的答案中也有00100这一位可以发现我们可以将其转化为00111,后面就可以不用枚举了。

代码实现:

复制代码
import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main {
    static int inf = (int) 1e9;
    static int mod = 998244353;

    public static void main(String[] args) throws IOException {
        int t = f.nextInt();
        while (t > 0) {
            solve();
            t--;
        }

        w.flush();
        w.close();
        br.close();
    }

    static int n;
    static int[] l;
    static int[] r;
    static int[] v;
    static int maxN = (int) 2e5 + 5;
    static int[] t = new int[maxN * 2];


    public static void solve() {
        n = f.nextInt();
        l = new int[n + 1];
        r = new int[n + 1];
        v = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            l[i] = f.nextInt();
            r[i] = f.nextInt();
        }
        fix();
        int[][] bits = new int[31][n + 1];
        for (int i = 1; i <= n; i++) {
            update(i, v[i]);
            for (int j = 30; j >= 0; j--) {
                bits[j][i] = bits[j][i-1];
                if (((r[i] >> j) & 1) == 1) bits[j][i]++;
            }
        }
        int q = f.nextInt();
        for (int i = 0; i < q; i++) {
            int x = f.nextInt();
            int y = f.nextInt();
            int ans = sum(x, y);
            for (int j = 30; j >= 0; j--) {
                int cnt = bits[j][y] - bits[j][x-1] + ((ans >> j) & 1);
                if (cnt > 1) {
                    ans |= (2 << j) - 1;
                    break;
                } else if (cnt == 1) {
                    ans |= (1 << j);
                }
            }
            w.print(ans + " ");
        }
        w.println();
        for (int i = 0; i <= n; i++) {
            t[i] = 0;
        }
    }

    //    public static void fix(){
//        for (int i = 1; i <= n; i++) {
//            if (l[i] == r[i]){
//                v[i] = r[i];
//                l[i] = r[i] = 0;
//                continue;
//            }
//            int pref = (1 << (lg(l[i] ^ r[i]) + 1)) - 1;
//            v[i] = r[i] - (r[i] & pref);
//            r[i] = r[i] & pref;
//            l[i] = l[i] & pref;
//        }
//    }
    public static void fix() {
        for (int i = 1; i <= n; ++i) {
            if (l[i] == r[i]) {
                v[i] = l[i];
                l[i] = r[i] = 0;
                continue;
            }
            int pref = (1 << (lg(l[i] ^ r[i]) + 1)) - 1;
            v[i] = r[i] - (r[i] & pref);
            r[i] &= pref;
            l[i] &= pref;
        }
    }

    public static void update(int x, int val) {
        for (int i = x; i <= n; i += lowbit(i)) {
            t[i] |= val;
        }
    }

    public static int lg(int x) {
        for (int i = 30; i >= 0; i--) {
            if (((x >> i) & 1) == 1) return i;
        }
        return 0;
    }


    public static int sum(int l, int r) {
        int res = 0;
        while (l <= r) {
            res |= v[r];
            r--;
            while (r - lowbit(r) >= l) {
                res |= t[r];
                r -= lowbit(r);
            }
        }
        return res;
    }

    public static int lowbit(int x) {
        return x & -x;
    }



    static PrintWriter w = new PrintWriter(new OutputStreamWriter(System.out));
    static Input f = new Input(System.in);
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    static class Input {
        public BufferedReader reader;
        public StringTokenizer tokenizer;

        public Input(InputStream stream) {
            reader = new BufferedReader(new InputStreamReader(stream), 32768);
            tokenizer = null;
        }

        public String next() {
            while (tokenizer == null || !tokenizer.hasMoreTokens()) {
                try {
                    tokenizer = new StringTokenizer(reader.readLine());
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            return tokenizer.nextToken();
        }

        public String nextLine() {
            String str = null;
            try {
                str = reader.readLine();
            } catch (IOException e) {
                // TODO 自动生成的 catch 块
                e.printStackTrace();
            }
            return str;
        }

        public int nextInt() {
            return Integer.parseInt(next());
        }

        public long nextLong() {
            return Long.parseLong(next());
        }

        public Double nextDouble() {
            return Double.parseDouble(next());
        }

        public BigInteger nextBigInteger() {
            return new BigInteger(next());
        }
    }
}
相关推荐
谭欣辰2 分钟前
LCS(最长公共子序列)详解
开发语言·c++·算法
m0_629494737 分钟前
LeetCode 热题 100-----17.缺失的第一个正数
数据结构·算法·leetcode
Cando学算法7 分钟前
鸽笼原理(抽屉原理)
c++·算法·学习方法
Tisfy12 分钟前
LeetCode 0796.旋转字符串:暴力模拟
算法·leetcode·题解·模拟·字符串匹配
BlockChain88823 分钟前
AI+区块链深度探索:算法与账本的共生时代
人工智能·算法·区块链
生成论实验室32 分钟前
《源·觉·知·行·事·物:生成论视域下的统一认知语法》第一章 源:不可言说的生成之源
人工智能·科技·算法·生活·创业创新
2zcode1 小时前
基于低光照增强与轻量型CNN道路实时识别算法研究(UI界面+数据集+训练代码)
人工智能·算法·cnn·低光照增强·自动驾驶技术
小雅痞1 小时前
[Java][Leetcode middle] 209. 长度最小的子数组
java·算法·leetcode
做时间的朋友。1 小时前
精准核酸检测
java·数据结构·算法
冯诺依曼的锦鲤2 小时前
从零实现高并发内存池:TCMalloc 核心架构拆解
c++·学习·算法·架构