【jzoj4598】【准备食物】【字典树】
来源:互联网 发布:在哪看淘宝自动售货 编辑:程序博客网 时间:2024/06/10 09:33
题目大意
给出长度为n的序列,询问[1,r]的后缀区间[i,r] (1<=i,i<=r)异或和大于等于k的区间的个数。
题解
异或可逆,[i,r]等于[1,i-1]异或[1,r],设前缀和s,求满足s[i-1]^s[r]>=k,由于不可移项,必须考虑s[r]和k的影响。考虑建立Trie在上面查询答案。考虑当先选0还是1,选了一个异或后比k大则所有答案可以贡献,当前相等则继续比较,小于则放弃。详细见code。
code
#include<set>#include<cmath>#include<cstdio>#include<cstring>#include<algorithm>#define fo(i,j,k) for(int i=j;i<=k;i++)#define fd(i,j,k) for(int i=j;i>=k;i--)using namespace std;int const maxn=100000,maxq=100000;int n,q,pon=1,ans[maxq+10],b[40],c[40],size[maxn*40+10],son[maxn*40+10][2];long long a[maxn+10];struct rec{int r,k,num;};rec qu[maxq+10];bool cmp(rec i,rec j){ return (i.r<j.r)||((i.r==j.r)&&(i.num==j.num));}void insert(int now,int pos){ size[now]++; if(pos>30)return; if(!son[now][b[pos]])son[now][b[pos]]=++pon; insert(son[now][b[pos]],pos+1);}int count(int now,int pos){ if(!now)return 0; if(pos>30)return size[now]; if(b[pos]&&c[pos])return count(son[now][0],pos+1); else if(b[pos]&&(!c[pos]))return count(son[now][1],pos+1)+size[son[now][0]]; else if((!b[pos])&&c[pos])return count(son[now][1],pos+1); else return count(son[now][0],pos+1)+size[son[now][1]];}int main(){ //freopen("food.in","r",stdin); //freopen("food.out","w",stdout); freopen("d.in","r",stdin); freopen("d.out","w",stdout); scanf("%d",&n); fo(i,1,n) scanf("%lld",&a[i]); scanf("%d",&q); fo(i,1,q) scanf("%d%d",&qu[i].r,&qu[i].k),qu[i].num=i; sort(qu+1,qu+q+1,cmp); int tmp=0;insert(1,1); fo(i,1,q){ fo(j,qu[i-1].r+1,qu[i].r){ tmp^=a[j];int tmpp=tmp; fd(k,30,1){ b[k]=tmpp%2; tmpp/=2; } insert(1,1); } fd(j,30,1){ c[j]=qu[i].k%2; qu[i].k/=2; } ans[qu[i].num]=count(1,1); } fo(i,1,q)printf("%d\n",ans[i]); return 0;}
0 0
- 【jzoj4598】【准备食物】【字典树】
- 【JZOJ4598】准备食物
- [jzoj4598]【NOIP2016模拟7.9】准备食物
- 准备食物
- 准备食物
- 【JZOJ 4598】准备食物
- 【JZOJ 4598】 准备食物
- JZOJ4718. 准备食物2
- 准备食物(trie) 题解+代码
- 【JZOJ4718】准备食物2 题解
- 【GDOI2017模拟8.20】准备食物2
- 食物
- 食物
- jzoj 4718. 【GDOI2017模拟7.20】准备食物2 费用流
- 字典树(先把代码存一下,明天比赛,今天想准备下)
- 康康准备了 M 斤的食物, 准备跟舍长交换哲学之宝 ♂
- 最润肤的10种天然食物 为夏季做准备哟!
- 字典树
- EL表达式
- Mac添加环境变量全面解读
- 【JZOJ4597】现世斩
- 今天装逼只为明天引爆,小众营销不要死在大众路上
- 第6章 访问权限控制
- 【jzoj4598】【准备食物】【字典树】
- [数论] ACM 2015 Changchun B Count a*b
- 你创业为什么会失败?
- "围观"设计模式(27)--行为型之解释器模式(Interpreter Pattern)
- Ubuntu安装pycharm
- iOS-js与iOS的交互(基于WKWebViewJavascriptBridge第三方)
- SPI_FLASH高速读写
- iOS-解压zip
- C语言结构体(struct)常见使用方法