BZOJ4571:[SCOI2016]美味
浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html
题目传送门:https://www.lydsy.com/JudgeOnline/problem.php?id=4571
直接从高位到低位贪心。我们可以一位一位确定还没与\(b\)异或起来的\(ans\),最后再与\(b\)异或。假设前面的高位确定完之后,根据当前位贪心需要什么,可以在一段区间内找到我们需要的数字。如果存在,那么这一位就是贪心的结果,否则就是另一种。在一段区间内找值域在某一段区间的数字,正好可以用主席树搞定。
时间复杂度:\(O(nlognlogv)\)
空间复杂度:\(O(nlogv)\)
代码如下:
#include <cstdio>
#include <algorithm>
using namespace std;
const int maxn=2e5+5;
int n,m;
int rt[maxn];
int read() {
int x=0,f=1;char ch=getchar();
for(;ch<'0'||ch>'9';ch=getchar())if(ch=='-')f=-1;
for(;ch>='0'&&ch<='9';ch=getchar())x=x*10+ch-'0';
return x*f;
}
struct tree_node {
int cnt,ls,rs;
};
struct chairman_tree {
int tot;
tree_node tree[maxn*18];
void ins(int lst,int &now,int l,int r,int pos) {
now=++tot;tree[now]=tree[lst];
tree[now].cnt++;
if(l==r)return;
int mid=(l+r)>>1;
if(pos<=mid)ins(tree[lst].ls,tree[now].ls,l,mid,pos);
else ins(tree[lst].rs,tree[now].rs,mid+1,r,pos);
}
int query(int x,int y,int l,int r,int L,int R) {
if(L<=l&&r<=R)return tree[y].cnt-tree[x].cnt;
int mid=(l+r)>>1,res=0;
if(L<=mid)res+=query(tree[x].ls,tree[y].ls,l,mid,L,R);
if(R>mid)res+=query(tree[x].rs,tree[y].rs,mid+1,r,L,R);
return res;
}
}T;
int main() {
n=read(),m=read();
for(int i=1;i<=n;i++) {
int x=read();
T.ins(rt[i-1],rt[i],0,1e5,x);
}
for(int i=1;i<=m;i++) {
int b=read(),x=read(),l=read(),r=read(),ans=0;
for(int j=17;~j;j--) {
int L,R,bit;
if(b&(1<<j))L=ans,R=ans+(1<<j)-1,bit=0;
else L=ans+(1<<j),R=ans+(1<<(j+1))-1,bit=1;
L=max(L-x,0);R=min(100000,R-x);
bool bo=T.query(rt[l-1],rt[r],0,1e5,L,R);
if(!bo)bit^=1;ans|=bit<<j;
}ans^=b;
printf("%d\n",ans);
}
return 0;
}
BZOJ4571:[SCOI2016]美味的更多相关文章
- 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:[SCOI2016]美味(主席树,贪心)
Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期望值为 bi,而他的偏好值为 xi . 因此,第 ...
- [BZOJ4571][SCOI2016]美味(贪心+主席树)
经典问题,按位贪心,每次需要知道的是”在这一位之前的位都以确定的情况下,能否找到这一位是0/1的数”,这就是在询问[L,R]内某个值域区间是否有数,主席树即可. #include<cstdio& ...
- BZOJ4571: [Scoi2016]美味【主席树】【贪心】
Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期 望值为 bi,而他的偏好值为 xi .因此,第 ...
- BZOJ4571 [Scoi2016]美味 【主席树】
题目 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期 望值为 bi,而他的偏好值为 xi .因此,第 i 位顾客认为第 ...
- 2018.10.14 bzoj4571: [Scoi2016]美味(主席树)
传送门 自认为是一道思想很妙的题. 直接分析问题. 如果没有xxx的干扰直接上可持久化01trie01trie01trie走人. 但现在有了xxx这个偏移量. 相当于把整个01trie01trie01 ...
- 【BZOJ4571】[Scoi2016]美味 主席树
[BZOJ4571][Scoi2016]美味 Description 一家餐厅有 n 道菜,编号 1...n ,大家对第 i 道菜的评价值为 ai(1≤i≤n).有 m 位顾客,第 i 位顾客的期望值 ...
- 【BZOJ4571】美味(主席树)
[BZOJ4571]美味(主席树) 题面 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 ...
随机推荐
- java中Executor、ExecutorService、ThreadPoolExecutor介绍
源码非常简单,只有一个execute(Runnable command)回调接口 public interface Executor { /** * Executes the given c ...
- ubuntu boost.python
安装boost(未尝试只安装 libboost-python-dev) sudo apt-get install libboost-all-dev 新建hello_ext.cpp,输入以下代码 1 c ...
- toggle不支持事件代理的解决办法或者 jquery live绑定toggle
$(".xxx").live("click", function () { $(this).toggle(function () {},functio ...
- JavaScript如何判断非空
JavaScript判断非空的语句一般为: var elvis; if (typeof elvis !== "undefined" && elvis !== nul ...
- 【题解】Making The Grade(DP+结论)
[题解]Making The Grade(DP+结论) VJ:Making the Grade HNOI-D2-T3 原题,禁赛三年. 或许是我做过的最简单的DP题了吧(一遍过是什么东西) 之前做过关 ...
- centos7 安装php
http://blog.csdn.net/zhaozuosui/article/details/48394409
- ubuntu nohup命令用法
让程序在后台运行 该命令的一般形式nohup command & 程序在后台运行并打印日志 nohup ./china_fund.py > china_fund.file 2>&a ...
- jQuery:[2]百度地图开发平台实战
jQuery:[2]百度地图开发平台实战 原文链接: http://blog.csdn.net/moniteryao/article/details/51078779 快速开始 开发平台地址 ht ...
- vector缩减容量
在C++标准库容器vector的容量是不会自动的缩减的,也就是说删除元素操作,其引用.指针.迭代器也会继续有效.那么当在一个较大的vector中删除了大量的元素之后,其实际的size比较小,而其cap ...
- 算法(Algorithms)第4版 练习 链表类 1.3.19~1.3.29
package com.qiusongde.linkedlist; import java.util.Iterator; import java.util.NoSuchElementException ...