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) { ]; ...
随机推荐
- 孤荷凌寒自学python第八十天开始写Python的第一个爬虫10
孤荷凌寒自学python第八十天开始写Python的第一个爬虫10 (完整学习过程屏幕记录视频地址在文末) 原计划今天应当可以解决读取所有页的目录并转而取出所有新闻的功能,不过由于学习时间不够,只是进 ...
- [2018 ACL Long] 对话系统
[NLG - E2E - knowledge guide generation] 1. Knowledge Diffusion for Neural Dialogue Generation ( Ci ...
- Paper Reading - Convolutional Image Captioning ( CVPR 2018 )
Link of the Paper: https://arxiv.org/abs/1711.09151 Motivation: LSTM units are complex and inherentl ...
- Struts2文件上传带进度条,虽然不是很完美
好久没有写东西,最近在做个项目,要用到文件h 传的,以前虽然也做上传,但是总觉得不好用 ,现在和队友合作做了一个带进度条的上传,觉得还行~~和大家分享一下. 首先说一下大概是这样实现的,在我们平时的上 ...
- 项目uml
[团队信息] 团队项目: 小葵日记--主打记录与分享模式的日记app 队名:日不落战队 队员信息及贡献分比例: 短学号 名 本次作业博客链接 此次作业任务 贡献分配 备注 501 安琪 http:// ...
- Spring Boot(五)启动流程分析
学习过springboot的都知道,在Springboot的main入口函数中调用SpringApplication.run(DemoApplication.class,args)函数便可以启用Spr ...
- C# .net 获取外网ip
public string GetIP() { string strUrl = "http://www.ip138.com/ip2city.asp"; //获得IP的网址了 Uri ...
- cURL和file_get_contents实现模拟post请求
以前面试时候,面试官问过我后端有没有跨域问题,但是不敢肯定,现在可以肯定的说没有. 不文用php的cURL和file_get_contents方法分别实现后端跨域.本文场景也是在tp5下实现的. 一, ...
- 修改IntelliJ IDEA代码头注释
- 关于如何解决PHPCMS V9内容搜索显示不全问题解决方案
站长朋友们都晓得只要是开源的PHP程序都会有漏洞存在.如果想完美的建站就需要自己去研究打补丁了.最近很多站长联系小编咨询用phpcms建站当在首页搜索内容的时候有的居然搜索不到.小编感到很是奇怪于是就 ...