[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 区间找异或值最大,还带加法,可以用主席树: 可以按位考虑,然后通过加上之前已经有的答案 ...
随机推荐
- beego项目和go项目 打包部署到linux
参考文章: https://www.jianshu.com/p/64363dff9721 [beego项目] 一. 打包 1. 打开Terminal 定位到工程的 main.go 文件夹目录 2. 执 ...
- 学习笔记:CentOS7学习之十六:LVM管理和ssm存储管理器使用
目录 学习笔记:CentOS7学习之十六:LVM管理和ssm存储管理器使用 16.1 LVM的工作原理 16.1.1 LVM常用术语 16.1.2 LVM优点 16.2 创建LVM的基本步骤 16.2 ...
- uwp,c#,mediaElement与slider进度条绑定
虽然微软uwp官方已停止对传统媒体控件mediaElement的update,新控件为mediaPlayerElement和mediaPlayer[官方word:https://docs.micros ...
- 【LOJ】#3097. 「SNOI2019」通信
LOJ#3097. 「SNOI2019」通信 费用流,有点玄妙 显然按照最小路径覆盖那题的建图思路,把一个点拆成两种点,一种是从这个点出去,标成\(x_{i}\),一种是输入到这个点,使得两条路径合成 ...
- 网络编程[第二篇]基于udp协议的套接字编程
udp协议下的套接字编程 一.udp是无链接的 不可靠的 而上篇的tcp协议是可靠的,会有反馈信息来确认信息交换的完成与否 基于udp协议写成的服务端与客户端,各司其职,不管对方是否接收到信息, ...
- urllib基础
import urllib.request # urlretrieve(网址,本地路径) 直接下载网页到本地 urllib.request.urlretrieve("http://www.b ...
- Java 私有构造函数的使用
被private修饰的私有构造函数无法在其他类中调用,也就是该类无法在其他类中实例化. 这种情况常用的使用场景:1.单例模式: 2.防止实例化. 一.单例模式 单例模式是一种常用的设计模式,思想是单例 ...
- 怎样理解"不推荐不使用var的变量声明方式"这句话
答: 因为不使用var声明的变量不会被预解析, 如下: console.log(a); console.log(b); var a = 1; b = 2;
- StoneTab标签页CAD插件 3.2.6
//////////////////////////////////////////////////////////////////////////////////////////////////// ...
- c++11 常量表达式
c++11 常量表达式 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #inclu ...