[SCOI2016]美味——主席树+按位贪心
题解
让异或值最大显然要按位贪心,然后我们还发现加上一个\(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]美味——主席树+按位贪心的更多相关文章
- P3293 [SCOI2016]美味 主席树+按位贪心
给定长度为 \(n\) 序列 \(a[i]\) ,每次询问区间 \([l,r]\) ,并给定 \(b,x\) 中的一个数 \(p=a[i]\) ,使得最大化 \(b \bigoplus p^x\) 主 ...
- BZOJ.4571.[SCOI2016]美味(主席树 贪心)
题目链接 要求 \(b\ xor\ (a_j+x)\) 最大,应让 \(a_j+x\) 的最高位尽可能与b相反.带个减法Trie树好像很难做?反正我不会. 从最高位开始,如果这位b是0/1,判断是否存 ...
- BZOJ4571:[SCOI2016]美味(主席树,贪心)
Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期望值为 bi,而他的偏好值为 xi . 因此,第 ...
- 【BZOJ4571】[Scoi2016]美味 主席树
[BZOJ4571][Scoi2016]美味 Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期望值 ...
- bzoj 4571: [Scoi2016]美味 (主席树)
链接:https://www.lydsy.com/JudgeOnline/problem.php?id=4571 题面; 4571: [Scoi2016]美味 Time Limit: 30 Sec ...
- BZOJ4517[Scoi2016]美味——主席树
题目描述 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期 望值为 bi,而他的偏好值为 xi .因此,第 i 位顾客认为 ...
- bzoj 4571 [Scoi2016]美味——主席树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4571 按位考虑,需要的就是一个区间:比如最高位就是(2^k -x). 对于不是最高位的位置该 ...
- BZOJ4571: [Scoi2016]美味【主席树】【贪心】
Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期 望值为 bi,而他的偏好值为 xi .因此,第 ...
- bzoj 4571 美味 —— 主席树
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=4571 区间找异或值最大,还带加法,可以用主席树: 可以按位考虑,然后通过加上之前已经有的答案 ...
随机推荐
- idea配置git的步骤
第一次git到GitHub过程 打开到项目 这样就不用再用git Bash敲命令了
- XKC's basketball team【线段树查询】
XKC , the captain of the basketball team , is directing a train of nn team members. He makes all mem ...
- #【Python】【基础知识】【模块】【Python的常用模块】
在IDE解释器中,执行help('modules')可查看当前环境可调用的Python模块: >>> help('modules') Please wait a moment whi ...
- Feign 接口上传文件
1)Encoder 配置注入容器 2) public class SpringFormEncoderExtension extends FormEncoder { /** * 使用默认的feign编码 ...
- Java静态代理与动态代理实现
一.什么是代理 代理是一种设计模式,它提供了一种通过代理访问目标对象的方式.在应用代理之前,我们调用对象的过程如下: 客户端直接调用对象并获取返回值.而应用了代理之后,我们调用对象的过程变成如下: 客 ...
- Photon Server 实现注册与登录(三) --- 前端UI设计和发起请求
一.打开之前的测试项目.先将服务端代码编译一下,在 bin/Debug/目录下会发现有一个Common.dill.我们相应导入到前端使用.直接拖拽到相应地方 UI相应布局属于前端操作,这里就不做介绍了 ...
- Pytorch中的自编码(autoencoder)
Pytorch中的自编码(autoencoder) 本文资料来源:https://www.bilibili.com/video/av15997678/?p=25 什么是自编码 先压缩原数据.提取出最有 ...
- spark2.0的10个特性介绍
1. Spark 2.0 ! 还记得我们的第七篇 Spark 博文里吗?里面我用三点来总结 spark dataframe 的好处: 当时是主要介绍 spark 里的 dataframe,今天是想总结 ...
- input type=color 设置颜色
在设置背景色的时候,使用html5 type=color 标签,但是初始值一直都是黑色的,背景如果没有设置的时候,应该是白色,比如文本图元,所以需要设置一个初始的颜色值, 注意: value不实用,怎 ...
- linux 目录结构及VIM
目录结构及VIM 文件系统 说明: 文件系统就是操作管理存储设备或分区上的文件的方法和数据结构,也就是存储设备上组织文件的方式. 操作系统中负责管理和存储文件信息的软件机构叫文件管理系统,简称为文件系 ...