BZOJ4571
BZOJ4571
Description
给定n个数, m次询问, 每次询问[l,r]范围内的数加上x后异或b的最大值, x, b给出.
\]
Solution
考虑不用加上x的做法, 那么直接从高到低位贪心.
但是在加上了x后, 就不那么好搞了. 同样考虑按位处理, 令ans为确定了前几位的答案,那么如果b的某一位是1, 最好在[ans, ans | ((1 << j) - 1)]取值. 我们只要看在这个区间内是否有值就可以了, 因为要加上x, 我们处理时反着减去x即可
Code
#include<bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for(int i = (a), i##_end_ = (b); i <= i##_end_; ++i)
#define drep(i, a, b) for(int i = (a), i##_end_ = (b); i >= i##_end_; --i)
#define clar(a, b) memset((a), (b), sizeof(a))
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define Debug(s) debug("The massage in line %d, Function %s: %s\n", __LINE__, __FUNCTION__, s)
typedef long long LL;
typedef long double LD;
const int BUF_SIZE = (int)1e6 + 10;
struct fastIO {
char buf[BUF_SIZE], buf1[BUF_SIZE];
int cur, cur1;
FILE *in, *out;
fastIO() {
cur = BUF_SIZE, in = stdin, out = stdout;
cur1 = 0;
}
inline char getchar() {
if(cur == BUF_SIZE) fread(buf, BUF_SIZE, 1, in), cur = 0;
return *(buf + (cur++));
}
inline void putchar(char ch) {
*(buf1 + (cur1++)) = ch;
if (cur1 == BUF_SIZE) fwrite(buf1, BUF_SIZE, 1, out), cur1 = 0;
}
inline int flush() {
if (cur1 > 0) fwrite(buf1, cur1, 1, out);
return cur1 = 0;
}
}IO;
#define getchar IO.getchar
#define putchar IO.putchar
int read() {
char ch = getchar();
int x = 0, flag = 1;
for(;!isdigit(ch); ch = getchar()) if(ch == '-') flag *= -1;
for(;isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
return x * flag;
}
void write(int x) {
if(x < 0) putchar('-'), x = -x;
if(x >= 10) write(x / 10);
putchar(x % 10 + 48);
}
void putString(char s[], char EndChar = '\n') {
rep(i, 0, strlen(s) - 1) putchar(*(s + i));
if(~EndChar) putchar(EndChar);
}
#define Maxn 200009
int n, m, a[Maxn], rt[Maxn];
namespace ChairmanTree {
struct Node {
int son[2], val;
}tree[Maxn * 30];
int amt;
int build(int l, int r) {
if(l > r) return 0;
int u = ++amt, mid = (l + r) >> 1;
tree[u] = (Node){0, 0, 0};
if(l == r) return u; /**/
tree[u].son[0] = build(l, mid);
tree[u].son[1] = build(mid + 1, r);
return u;
}
int modify(int sou, int l, int r, int v) {
if(l > r) return 0;
int u = ++amt, mid = (l + r) >> 1;
tree[u] = tree[sou]; ++tree[u].val;
if(l == r) return u;
if(v <= mid) tree[u].son[0] = modify(tree[sou].son[0], l, mid, v);
else tree[u].son[1] = modify(tree[sou].son[1], mid + 1, r, v);
return u;
}
int query(int Lr, int Rr, int l, int r, int q1, int q2) {
if(q1 <= l && r <= q2) return tree[Rr].val - tree[Lr].val;
int mid = (l + r) >> 1;
if(q2 <= mid) return query(tree[Lr].son[0], tree[Rr].son[0], l, mid, q1, q2);
else if(q1 > mid) return query(tree[Lr].son[1], tree[Rr].son[1], mid + 1, r, q1, q2);
else return query(tree[Lr].son[0], tree[Rr].son[0], l, mid, q1, mid) + query(tree[Lr].son[1], tree[Rr].son[1], mid + 1, r, mid + 1, q2);
}
}
namespace INIT {
void Main() {
n = read(); m = read();
rt[0] = ChairmanTree :: build(1, Maxn - 1);
rep(i, 1, n) a[i] = read(), rt[i] = ChairmanTree :: modify(rt[i - 1], 1, Maxn - 1, a[i]);
}
}
namespace SOLVE {
void Main() {
rep(i, 1, m) {
int b = read(), x = read(), l = read(), r = read();
int ans = 0;
drep(j, 17, 0)
if(b & (1 << j)) {
int L = ans, R = ans | ((1 << j) - 1);
L -= x, R -= x;
if(R <= 0 || !ChairmanTree :: query(rt[r], rt[l - 1], 1, Maxn - 1, L, R)) ans ^= (1 << j);
}else {
int L = ans | (1 << j), R = ans | ((1 << (j + 1)) - 1);
L -= x, R -= x;
if(R <= 0 || ChairmanTree :: query(rt[r], rt[l - 1], 1, Maxn - 1, L, R)) ans ^= (1 << j);
}
write(ans ^ b), putchar('\n');
}
}
}
int main() {
#ifdef Qrsikno
freopen("BZOJ4571.in", "r", stdin);
freopen("BZOJ4571.out", "w", stdout);
#endif
INIT :: Main();
SOLVE :: Main();
#ifdef Qrsikno
debug("\nRunning time: %.3lf(s)\n", clock() * 1.0 / CLOCKS_PER_SEC);
#endif
return IO.flush();
}
BZOJ4571的更多相关文章
- 【BZOJ4571】美味(主席树)
[BZOJ4571]美味(主席树) 题面 Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期 望值为 ...
- 【bzoj4571 scoi2016】美味
题目描述 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1<=i<=n).有 m 位顾客,第 i 位顾客的期望值为 bi,而他的偏好值为 xi .因此,第 ...
- [BZOJ4571][SCOI2016]美味(贪心+主席树)
经典问题,按位贪心,每次需要知道的是”在这一位之前的位都以确定的情况下,能否找到这一位是0/1的数”,这就是在询问[L,R]内某个值域区间是否有数,主席树即可. #include<cstdio& ...
- 【bzoj4571】美味
Portal -->bzoj4571 Solution emmm持续智力康复.. 虽然说因为统计的是加上\(x\)的跟\(b\)异或的最大值所以可持久化trie用不了了 但是按位贪心的思想还 ...
- 【BZOJ4571】[Scoi2016]美味 主席树
[BZOJ4571][Scoi2016]美味 Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期望值 ...
- bzoj4571: [Scoi2016]美味
4571: [Scoi2016]美味 Time Limit: 30 Sec Memory Limit: 256 MB Submit: 275 Solved: 141 [Submit][Status][ ...
- 【bzoj4571&&SCOI2016美味】
4571: [Scoi2016]美味 Time Limit: 30 Sec Memory Limit: 256 MBSubmit: 656 Solved: 350[Submit][Status][ ...
- bzoj4571/luogu3293 美味 (主席树+贪心)
首先想到建出可持久化trie树然后在上面贪心,但是它加了一个数所以不能这么做 但依然可以贪心,仿照上面那个的过程,如果设y是在第i位上^b是1的数(前面的位数已经贪好了),我只要在[l,r]范围内能有 ...
- 2018.10.14 bzoj4571: [Scoi2016]美味(主席树)
传送门 自认为是一道思想很妙的题. 直接分析问题. 如果没有xxx的干扰直接上可持久化01trie01trie01trie走人. 但现在有了xxx这个偏移量. 相当于把整个01trie01trie01 ...
随机推荐
- python执行系统命令的几种方法
(1) os.system 这个方法是直接调用标准C的system() 函数,仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息. import os os.system('cat /pro ...
- Github配置SSH
以前也配置过ssh,但是没有注意用法,在配置一次熟悉流程 检查本机是否有ssh key设置 $ cd ~/.ssh 或cd .ssh 如果没有则提示: No such file or director ...
- 搜索引擎keyword智能提示的一种实现
问题背景 搜索关键字智能提示是一个搜索应用的标配.主要作用是避免用户输入错误的搜索词,并将用户引导到相应的关键词上,以提升用户搜索体验. 美团CRM系统中存在数以百万计的商家,为了让用户高速查找到目标 ...
- Android Activity与远程Service的通信学习总结
当一个Service在androidManifest中被声明为 process=":remote", 或者是还有一个应用程序中的Service时,即为远程Service, 远程的意 ...
- js中splice()的强大(删除,插入或替换数组的元素)
1.删除-用于删除元素,两个参数,第一个参数(要删除第一项的位置),第二个参数(要删除的项数) 2.插入-向数组指定位置插入任意项元素.三个参数,第一个参数(其实位置),第二个参数(0),第三个参数( ...
- 各项异性滤波简单介绍Anisotropic Filtering(AF)
本文主要整理简绍来自互联网的各项异性滤波的知识. 原文链接:http://www.linuxgraphics.cn/graphics/using_anisotropic_texture_filteri ...
- 数据库分表和分库的原理及基于thinkPHP的实现方法
为什么要分表,分库: 当我们的数据表数据量,訪问量非常大.或者是使用频繁的时候,一个数据表已经不能承受如此大的数据訪问和存储,所以,为了减轻数据库的负担,加快数据的存储,就须要将一张表分成多张,及将一 ...
- Codeforces Round #135 (Div. 2)---A. k-String
k-String time limit per test 2 seconds memory limit per test 256 megabytes input standard input outp ...
- 使用VLC搭建视频直播服务器
去年我们信息之夜我们进行过视频直播服务,当时我们使用了WMS(Windows Media Server)实现了这个服务,但是编码是微软的WMV,因而像iPhone/Android这样的智能手机无法观看 ...
- MongoDB 自己定义函数
定义 db.system.js.insert({ _id : "TestConcat", value : function TestConcat(s1, s2){ return s ...