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(贪心+异或基)的更多相关文章

  1. Codeforces Round #532 (Div. 2) F 线性基(新坑) + 贪心 + 离线处理

    https://codeforces.com/contest/1100/problem/F 题意 一个有n个数组c[],q次询问,每次询问一个区间的子集最大异或和 题解 单问区间子集最大异或和,线性基 ...

  2. Codeforces Round #529 (Div. 3) F. Make It Connected (贪心,最小生成树)

    题意:给你\(n\)个点,每个点都有权值,现在要在这\(n\)个点中连一颗最小树,每两个点连一条边的边权为两个点的点权,现在还另外给了你几条边和边权,求最小权重. 题解:对于刚开始所给的\(n\)个点 ...

  3. Codeforces Round #485 (Div. 2) F. AND Graph

    Codeforces Round #485 (Div. 2) F. AND Graph 题目连接: http://codeforces.com/contest/987/problem/F Descri ...

  4. Codeforces Round #486 (Div. 3) F. Rain and Umbrellas

    Codeforces Round #486 (Div. 3) F. Rain and Umbrellas 题目连接: http://codeforces.com/group/T0ITBvoeEx/co ...

  5. Codeforces Round #532 (Div. 2)

    Codeforces Round #532 (Div. 2) A - Roman and Browser #include<bits/stdc++.h> #include<iostr ...

  6. Codeforces Round #501 (Div. 3) F. Bracket Substring

    题目链接 Codeforces Round #501 (Div. 3) F. Bracket Substring 题解 官方题解 http://codeforces.com/blog/entry/60 ...

  7. Codeforces Round #532 (Div. 2) 题解

    Codeforces Round #532 (Div. 2) 题目总链接:https://codeforces.com/contest/1100 A. Roman and Browser 题意: 给出 ...

  8. Codeforces Round #499 (Div. 1) F. Tree

    Codeforces Round #499 (Div. 1) F. Tree 题目链接 \(\rm CodeForces\):https://codeforces.com/contest/1010/p ...

  9. Codeforces Round #532 (Div. 2) Solution

    A. Roman and Browser 签到. #include <bits/stdc++.h> using namespace std; ]; int get(int b) { ]; ...

随机推荐

  1. mysql bin log配置及查看

    mysql执行sql可以通过设置mysql bin 日志进行记录查看   mysql bin日志配置如下:   log_bin:on log_bin_basename:bin文件路径及名前缀(/var ...

  2. eclipse注释快捷键

    1.单行注释 注释: ctrl + / 取消注释: ctrl + / 多行注释 注释: ctrl shift + / 取消注释: ctrl shift + \

  3. 初涉 JavaScript

    网页是什么 网页 = Html+CSS+JavaScriptHtml:网页元素内容CSS:控制网页样式JavaScript:操作网页内容,实现功能或者效果 JavaScirpt 发展历史 参考 使用 ...

  4. 【Linux 运维】Centos7初始化网络配置

    设置网络 (1)动态获取一个IP地址 #dhclient        系统自动自动获取一个IP地址#ip addr         查看获取的ip地址(2)查看网关,子网掩码 虚拟机编辑>虚拟 ...

  5. some Commands OF CONSOLE

    不可避免地使用console,一旦与电脑打交道:入口就是help,而很多行就直接过掉了,却不能看到需要的地方,在那里停下来,实际是需要使用more  less grep等 在windows中,使用di ...

  6. (转载)IE8+兼容经验小结

    本文分享下我在项目中积累的IE8+兼容性问题的解决方法.根据我的实践经验,如果你在写HTML/CSS时候是按照W3C推荐的方式写的,然后下面的几点都关注过,那么基本上很大一部分IE8+兼容性问题都OK ...

  7. 6.azkban的监控

    azkaban自带的监控flow自带的邮件功能SLA总结写程序监控job情况监控azkaban的元数据库使用azkaban API监控总结 azkaban自带的监控 azkban目前仅仅支持邮件监控, ...

  8. codeforces 303C. Minimum Modular(数论+暴力+剪枝+贪心)

    You have been given n distinct integers a1, a2, ..., an. You can remove at most k of them. Find the ...

  9. 自测之Lesson7:设备文件操作

    题目:请编写一个输入密码(不回显)的程序,要求通过设置终端来完成. 完成代码: #include <stdio.h> #include <unistd.h> #include ...

  10. Crash使用参考

    整理自man 8 crash 1.简介 Crash工具可以用来分析一个正在运行的内核,也可以用来分析一个内核的crash dump文件,这里说的是内核代码异常产生的crash dump文件,不是应用层 ...