Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)
F. Ivan and Burgers
题目链接:https://codeforces.com/contest/1100/problem/F
题意:
给出n个数,然后有多个询问,每次回答询问所给出的区间的异或和最大值。
题解:
考虑离线做法,先把所有的询问区间按照右端点进行排序,然后从1开始逐个将ai插入,插入线性基的同时记录一下位置,最后扫一下,看看哪些的位置是不小于li的即可加入答案。
这种做法在时间复杂度上面是可行的,但是需要注意的是,如果在i这个位置插入最高位为x的线性基,同时在j这个位置又有一个最高位为x的线性基,那么对于最高位为x的线性基,我们是肯定保留后面的位置是更优的。在替换位置过后,注意一下插入的线性基也发生了变化。
代码如下:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 5e5+;
int n,q;
int c[N],ans[N],pos[N];
int a[];
struct P{
int l,r,id;
bool operator < (const P &A)const{
return r<A.r;
}
}p[N];
void insert(int val,int id){
for(int i=;i>=;i--){
if(!((<<i)&val)) continue ;
if(!a[i]){
a[i]=val;
pos[i]=id;
break ;
}
if(pos[i]<id) swap(pos[i],id),swap(a[i],val);
val^=a[i];
}
}
int query(int l){
int res=;
for(int i=;i>=;i--){
if(((res^a[i])>res) && pos[i]>=l) res^=a[i];
}
return res;
}
int main(){
ios::sync_with_stdio(false);cin.tie();
cin>>n;
for(int i=;i<=n;i++) cin>>c[i];
cin>>q;
for(int i=;i<=q;i++) cin>>p[i].l>>p[i].r,p[i].id=i;
sort(p+,p+q+);
int r=;
for(int i=;i<=q;i++){
while(r<=p[i].r) insert(c[r],r),r++;
ans[p[i].id]=query(p[i].l);
}
for(int i=;i<=q;i++) cout<<ans[i]<<'\n';
return ;
}
Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)的更多相关文章
- Codeforces Round #532 (Div. 2) F 线性基(新坑) + 贪心 + 离线处理
https://codeforces.com/contest/1100/problem/F 题意 一个有n个数组c[],q次询问,每次询问一个区间的子集最大异或和 题解 单问区间子集最大异或和,线性基 ...
- Codeforces Round #529 (Div. 3) F. Make It Connected (贪心,最小生成树)
题意:给你\(n\)个点,每个点都有权值,现在要在这\(n\)个点中连一颗最小树,每两个点连一条边的边权为两个点的点权,现在还另外给了你几条边和边权,求最小权重. 题解:对于刚开始所给的\(n\)个点 ...
- Codeforces Round #485 (Div. 2) F. AND Graph
Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...
- Codeforces Round #486 (Div. 3) F. Rain and Umbrellas
Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...
- Codeforces Round #532 (Div. 2)
Codeforces Round #532 (Div. 2) A - Roman and Browser #include<bits/stdc++.h> #include<iostr ...
- Codeforces Round #501 (Div. 3) F. Bracket Substring
题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...
- Codeforces Round #532 (Div. 2) 题解
Codeforces Round #532 (Div. 2) 题目总链接:https://codeforces.com/contest/1100 A. Roman and Browser 题意: 给出 ...
- Codeforces Round #499 (Div. 1) F. Tree
Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...
- Codeforces Round #532 (Div. 2) Solution
A. Roman and Browser 签到. #include <bits/stdc++.h> using namespace std; ]; int get(int b) { ]; ...
随机推荐
- Zookeeper 分布式应用
简介 这篇文章是旨在为那些想要利用zookeeper协调服务能力进行分布式应用创建的开发者的入门指导,包括一些理论性和实践性的内容. 文章的前四部分系统的介绍了zookeeper的相关概念,对于理解z ...
- TW实习日记:第29-30天
这两天挺忙,赶工期,改bug.项目现场的同事说客户火大得不行.可是谁叫你们谈工期谈的这么紧,完全不考虑开发的情况,真的是烦人这种事情.这两天遇到的最有难度的一个点就是附件预览,搞这个改到晚上11点. ...
- 【转载】IOS之禁用UIWebView的默认交互行为
原文地址 :IOS之禁用UIWebView的默认交互行为 http://my.oschina.net/hmj/blog/111344 UIKit提供UIWebView组件,允许开发者在App中嵌入We ...
- 使用Zabbix监控rabbitmq服务
添加rabbitmq脚本 [root@controller rabbitmq]# cd /etc/zabbix/script/rabbitmq [root@controller rabbitmq]# ...
- facebook演讲
任何为了更大愿景工作的人,可能会被称为疯子,即使你最终获得成功. 任何为了复杂问题工作的人,都会因为不能全面了解挑战而被指责,即使你不可能事先了解一切. 任何抓住主动权先行一步的人,都会因为步子太快而 ...
- Too many open files错误与解决方法
致前辈:该问题的解决思路给了我很大的启发,文章作者Lis, Linux资深技术专家. 问题现象:这是一个基于Java的web应用系统,在后台添加数据时提示无法添加,于是登陆服务器查看Tomcat 日志 ...
- vuejs学习之 项目打包之后的首屏加载优化
vuejs学习之 项目打包之后的首屏加载优化 一:使用CDN资源 我们在打包时,会将package.json里,dependencies对象里插件打包起来,我们可以将其中的一些使用cdn的方式加载,例 ...
- android项目中导入actionbarsherlock 需要注意的地方
1,在导入actionbarsherlock 这个library时,如果一直报" Invalid Project Description" ; 解决办法: android中li ...
- 用 C# 实现文件信息统计(wc)命令行程序
软件的需求分析 程序处理用户需求的模式为: wc.exe [parameter][filename] 在[parameter]中,用户通过输入参数与程序交互,需实现的功能如下: 1.基本功能 支持 - ...
- HDU 2117 Just a Numble
http://acm.hdu.edu.cn/showproblem.php?pid=2117 Problem Description Now give you two integers n m, yo ...