原题戳这里

题解

让异或值最大显然要按位贪心,然后我们还发现加上一个\(x_i\)的效果就是所有\(a_i\)整体向右偏移了,我们对于\({a_i}\)开个主席树,支持查询一个区间中有多少个在\([L,R]\)之间的数,查询时考虑一下\(x_i\)的影响就行了

#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <queue>
#include <map>
#include <set> using namespace std; #define ull unsigned long long
#define pii pair<int, int>
#define uint unsigned int
#define mii map<int, int>
#define lbd lower_bound
#define ubd upper_bound
#define INF 0x3f3f3f3f
#define IINF 0x3f3f3f3f3f3f3f3fLL
#define vi vector<int>
#define ll long long
#define mp make_pair
#define pb push_back
#define re register
#define il inline #define N 200000
#define M 100000
#define A 300000
#define LIM 17 int n, m;
int a[N+5];
int nid, root[N+5], sumv[50*N+5], ch[2][50*N+5]; void insert(int o, int &u, int l, int r, int x) {
u = ++nid;
sumv[u] = sumv[o]+1;
ch[0][u] = ch[0][o], ch[1][u] = ch[1][o];
if(l == r) return ;
int mid = (l+r)>>1;
if(x <= mid) insert(ch[0][o], ch[0][u], l, mid, x);
else insert(ch[1][o], ch[1][u], mid+1, r, x);
} int query(int o, int u, int l, int r, int L, int R) {
if(L <= l && r <= R) return sumv[u]-sumv[o];
int mid = (l+r)>>1, ret = 0;
if(L <= mid) ret += query(ch[0][o], ch[0][u], l, mid, L, R);
if(R > mid) ret += query(ch[1][o], ch[1][u], mid+1, r, L, R);
return ret;
} int Query(int b, int x, int l, int r) {
int ans = 0, t = 0;
for(int bit = LIM; bit >= 0; --bit) {
if((b>>bit)&1) {
if(query(root[l-1], root[r], 0, A, max(0, t-x), max(0, t+(1<<bit)-1-x))) {
ans |= (1<<bit);
}
else t |= (1<<bit);
}
else {
if(query(root[l-1], root[r], 0, A, max(0, t+(1<<bit)-x), max(0, t+(1<<(bit+1))-1-x))) {
t |= (1<<bit);
ans |= (1<<bit);
}
}
}
return ans;
} int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i) scanf("%d", &a[i]), insert(root[i-1], root[i], 0, A, a[i]);
for(int i = 1, b, x, l, r; i <= m; ++i) {
scanf("%d%d%d%d", &b, &x, &l, &r);
printf("%d\n", Query(b, x, l, r));
}
return 0;
}

[SCOI2016]美味——主席树+按位贪心的更多相关文章

  1. P3293 [SCOI2016]美味 主席树+按位贪心

    给定长度为 \(n\) 序列 \(a[i]\) ,每次询问区间 \([l,r]\) ,并给定 \(b,x\) 中的一个数 \(p=a[i]\) ,使得最大化 \(b \bigoplus p^x\) 主 ...

  2. BZOJ.4571.[SCOI2016]美味(主席树 贪心)

    题目链接 要求 \(b\ xor\ (a_j+x)\) 最大,应让 \(a_j+x\) 的最高位尽可能与b相反.带个减法Trie树好像很难做?反正我不会. 从最高位开始,如果这位b是0/1,判断是否存 ...

  3. BZOJ4571:[SCOI2016]美味(主席树,贪心)

    Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期望值为 bi,而他的偏好值为 xi . 因此,第 ...

  4. 【BZOJ4571】[Scoi2016]美味 主席树

    [BZOJ4571][Scoi2016]美味 Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期望值 ...

  5. bzoj 4571: [Scoi2016]美味 (主席树)

    链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4571 题面; 4571: [Scoi2016]美味 Time Limit: 30 Sec   ...

  6. BZOJ4517[Scoi2016]美味——主席树

    题目描述 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期 望值为 bi,而他的偏好值为 xi .因此,第 i 位顾客认为 ...

  7. bzoj 4571 [Scoi2016]美味——主席树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4571 按位考虑,需要的就是一个区间:比如最高位就是(2^k -x). 对于不是最高位的位置该 ...

  8. BZOJ4571: [Scoi2016]美味【主席树】【贪心】

    Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期 望值为 bi,而他的偏好值为 xi .因此,第 ...

  9. bzoj 4571 美味 —— 主席树

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4571 区间找异或值最大,还带加法,可以用主席树: 可以按位考虑,然后通过加上之前已经有的答案 ...

随机推荐

  1. cenos 防火墙操作

    iptables防火墙 1.基本操作 # 查看防火墙状态 service iptables status   # 停止防火墙 service iptables stop   # 启动防火墙 servi ...

  2. 内存泄漏之malloc替换方法

    //内存泄漏之malloc替换方法 //内存泄漏之malloc替换方法#include "stdio.h"#include "stdlib.h" /*文件路径名 ...

  3. CSS3伪类与伪元素的区别及注意事项

    CSS中伪类与伪元素的概念是很容易混淆的 今天就来谈谈伪类与伪元素之间的区别 定义 首先先来看看伪类与伪元素的定义 w3c中对于它们是这么解释的 伪类:用于向某些选择器添加特殊的效果 伪元素:用于将特 ...

  4. LeetCode-第 166 场周赛

    LeetCode-第 166 场周赛 1281.subtract-the-product-and-sum-of-digits-of-an-integer 1282.group-the-people-g ...

  5. ARC081E. Don't Be a Subsequence

    $\newcommand{\dp}{\mathsf{dp}}$ $\newcommand{\next}{\mathsf{next}}$ Let $S$ be a string of lower cas ...

  6. 【AtCoder】ARC063

    ARC063 C - 一次元リバーシ / 1D Reversi 不同的颜色段数-1 #include <bits/stdc++.h> #define fi first #define se ...

  7. Linux IO模式以及select poll epoll详解

    一 背景 同步IO和异步IO,阻塞IO和非阻塞IO分别是什么,到底有什么区别?不同的人在不同的上下文下给出的答案是不同的.所以先限定一下本文的上下文. 本文讨论的背景是Linux环境下的network ...

  8. Laravel框架与ThinkPHP框架的不同

    作为一个PHP菜鸟初学Laravel框架 在学习过程中我发现了其与TP框架的不同点,由于时间问题和认识还不够完善我先写出其中几点,有错误的地方希望各位大牛斧正... 1.渲染模版方式的不同:在Lara ...

  9. 【php设计模式】单例模式

    实现单例的三个关键点: 1.使用一个静态成员来保持一个单例实例 2.一个私有的构造方法使得该类只能在类的内部方法中被实例化 3.在实例化对象的静态方法中,先判断静态变量是否已经被赋值,如果赋值则返回该 ...

  10. java统计字符串中每个字符出现的次数

    package MapTest; import java.util.HashMap; public class MapTest { public static void Count(String st ...