题目传送门

题意:

  给出 n 个数,q次区间查询,每次查询,让你选择任意个下标为 [ l , r ] 区间内的任意数,使这些数异或起来最大,输出最大值。

思路:离线加线性基。

线性基学习博客1

线性基学习博客2

对于此题,先把区间按照 r 从小到大排序,然后依次处理这些区间,每次插入线性基时,优先保留下标比较大的线性基。查询时,只异或上下标大于 l 的值。

记住异或的符号的优先级很低,所以  if( res^p[i] > res )这样的代码是会wa死的,要注意(这道题这么写,样例都过不了)

#include<bits/stdc++.h>
#define clr(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int maxn=5e5+;
int a[maxn],q,n,p[],pos[],ans[maxn];
struct node{
int l,r,id;
friend bool operator<(const node &a,const node &b)
{
return a.r<b.r;
}
}op[maxn];
void init(){
clr(p,);
}
void add(int val,int id){
for(int i=;i>=;i--)
{
if(val&(<<i))
{
if(!p[i]){
p[i]=val,pos[i]=id;
break;
}
if(pos[i]<id){
swap(pos[i],id),swap(val,p[i]);
}
val^=p[i];
}
}
}
int query(int l)
{
int res=;
for(int i=;i>=;i--)
{
if(pos[i]>=l)
{
if((res^p[i])>res)
{
res=res^p[i];
}
}
}
return res;
}
int main(){
while(cin>>n)
{
init();
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
}
cin>>q;
for(int i=;i<=q;i++)
{
scanf("%d%d",&op[i].l,&op[i].r);
op[i].id=i;
}
sort(op+,op++q);
int l=;
for(int i=;i<=q;i++)
{
while(l<=op[i].r&&l<=n)
{
add(a[l],l);
l++;
}
ans[op[i].id]=query(op[i].l);
}
for(int i=;i<=q;i++)
{
printf("%d\n",ans[i]);
}
}
}

codeforces 1100F Ivan and Burgers 线性基 离线的更多相关文章

  1. CodeForces 1100F Ivan and Burgers

    CodeForces题面 Time limit 3000 ms Memory limit 262144 kB Source Codeforces Round #532 (Div. 2) Tags da ...

  2. Codeforces 938G 线段树分治 线性基 可撤销并查集

    Codeforces 938G Shortest Path Queries 一张连通图,三种操作 1.给x和y之间加上边权为d的边,保证不会产生重边 2.删除x和y之间的边,保证此边之前存在 3.询问 ...

  3. 【题解】 Codeforces 662A Gambling Nim (线性基)

    662A,戳我戳我 Solution: 我们先取\(ans=a[1] \bigoplus a[2] \bigoplus ... \bigoplus a[n]\),然后我们定义\(c[i]=a[i] \ ...

  4. CodeForces - 1100F:Ivan and Burgers (线性基&贪心)(离线 在线)

    题意:给定N个数,Q次询问,求区间最大异或和. 思路:一开始想的线性基+线段树.单次线性基合并的复杂度为20*20,结合线段树,复杂度为O(NlogN*20*20):显然,超时. 超时代码: #inc ...

  5. Codeforces1100F. Ivan and Burgers(离线+线性基)

    题目链接:传送门 思路: 按查询的右端点离线. 然后从左到右维护线性基. 每个基底更新为最右边的方案,可以让尽量多的查询享受到这个基底. 用ci维护后更新右端点为i的答案. 代码(析构1000ms,别 ...

  6. F. Ivan and Burgers(线性基,离线)

    题目链接:http://codeforces.com/contest/1100/problem/F 题目大意:首先输入n,代表当前有n个数,然后再输入m,代表m次询问,每一次询问是询问区间[l,r], ...

  7. Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)

    F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...

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

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

  9. Codeforces 1100F(线性基+贪心)

    题目链接 题意 给定序列,$q(1\leq q \leq 100000) $次询问,每次查询给定区间内的最大异或子集. 思路 涉及到最大异或子集肯定从线性基角度入手.将询问按右端点排序后离线处理询问, ...

随机推荐

  1. win10 Kinect2 Visualstudio2015 opencv3环境搭建

    1.下载kinect SDK ( Kinect for Windows SDK 2.0 ):  https://www.microsoft.com/en-us/download/details.asp ...

  2. 724. Find Pivot Index 找到中轴下标

    [抄题]: Given an array of integers nums, write a method that returns the "pivot" index of th ...

  3. Python学习笔记_操作Excel

    Python 操作Exel,涉及下面几个库: 1.xlrd 读取Excel文件 2.xlwt 向Excel文件写入,并设置格式 3.xlutils 一组Excel高级操作工具,需要先安装xlrd和xl ...

  4. 嵌入式Qt开发环境的搭建详解

    一.嵌入式Qt开发环境的搭建前奏 1.下载arm-linux-gcc-4.4.3-20100728.tar.gz 2.下载qt-everywhere-opensource-src-4.8.5.tar. ...

  5. Entity Framework Tutorial Basics(41):Multiple Diagrams

    Multiple Diagrams in Entity Framework 5.0 Visual Studio 2012 provides a facility to split the design ...

  6. IIS关闭Trace、OPTIONS方法

    方法(1):web.config 在<configuration>节点下添加如下代码: <system.webServer> <security> <requ ...

  7. NET上传大文件出现网页无法显示的问题 默认的上传文件大小是4M

    需要在配置文件处进行修改 web.config中的<system.web></system.web>内加入如下代码: <httpRuntime executionTime ...

  8. angular 第二种依赖注入

    import { Injectable } from '@angular/core'; import { ProductServiceService, Product } from './produc ...

  9. SQLServer如何在批量插入后,获取批量插入的自增列的值

    解决方法如下: Use the OUTPUT functionality to grab all the INSERTED Id back into a table. 使用output 功能获取所有插 ...

  10. 快速下载android源码

    众所周知的原因,android源码被墙了,还好国内有不少镜像,这里使用清华提供的镜像. 以下内容转自: https://wiki.tuna.tsinghua.edu.cn/MirrorUsage/an ...