https://codeforces.com/contest/1100/problem/F

题意

一个有n个数组c[],q次询问,每次询问一个区间的子集最大异或和

题解

  • 单问区间子集最大异或和,线性基能处理,但是这次多次询问,假如每次重新建立基向量会超时
  • 考虑区间的优先级,假如我只插入不删除的话,区间的优先级和左端点没有关系
  • 贪心一下,只保留后面插入的基,这样就可以离线解决询问,然后查询的时候需要判基的位置是不是在左端点后面

代码

#include<bits/stdc++.h>
#define ft first
#define se second
#define pii pair<int,int>
#define mk make_pair
#define MAXN 500005
using namespace std;
vector<pii>G[MAXN];
int c[MAXN],n,i,q,j,ans[MAXN],B[30],pos[30],l,r; void add(int x,int p){
for(int i=20;i>=0;i--){
if(x>>i&&1){
if(!B[i]){
pos[i]=p;
B[i]=x;
}
if(p>pos[i])swap(p,pos[i]),swap(x,B[i]);
x^=B[i];
}
}
}
int main(){
cin>>n;
for(i=1;i<=n;i++)scanf("%d",&c[i]);
cin>>q;
for(i=1;i<=q;i++){
scanf("%d%d",&l,&r);
G[r].push_back(mk(i,l));
}
for(i=1;i<=n;i++){
add(c[i],i);
for(auto x:G[i]){
for(j=20;j>=0;j--){
if(pos[j]>=x.se)ans[x.ft]=max(ans[x.ft]^B[j],ans[x.ft]);
}
}
}
for(i=1;i<=q;i++)printf("%d\n",ans[i]);
}

Codeforces Round #532 (Div. 2) F 线性基(新坑) + 贪心 + 离线处理的更多相关文章

  1. Codeforces Round #548 (Div. 2) E 二分图匹配(新坑) or 网络流 + 反向处理

    https://codeforces.com/contest/1139/problem/E 题意 有n个学生,m个社团,每个学生有一个\(p_i\)值,然后每个学生属于\(c_i\)社团, 有d天,每 ...

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

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

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

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

  4. Codeforces Round #532 (Div. 2)

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 如何自定义Django数据库中的字段

    新建完项目之后,想要自定义字段 # 创建一个自定义的字段 class MycharField(models.Field): def __inif__(self, max_lenth, *args, * ...

  2. ES match match_phrase term willcard的查询原理

    比如:要求实现SQL中like “%xxxx%”的匹配效果. wildcard通配 这种效果在ES中最匹配的做法是用wildcard query通配,这种情况不会对query分词,而是直接遍历倒排索引 ...

  3. Vue数据交互

    注意:本地json文件必须放在static目录下面,读取或交互数据前,请先安装vue-resource.点击前往  -->(vue-resource中文文档) 一.Vue读取本地JSON数据 c ...

  4. checkpoint NGFW VM安装

    step1:在VMworkstation中创建虚拟机向导,选择Linux 2.6内核 64位如下图: 虚拟机的配置建议如下: RAM:至少8GB Disk:120G CPU:四核 step2:使用IO ...

  5. excel 上传读写到数据库

    <HTML> <div class="input-group"> <form id="abc" action="http ...

  6. Hillstone设备管理-设备软件Stone-OS升级

    1.通过sysloader进行StoneOS升级 1)给设备上电按提示按ESC并且进入 Sysloader.参照以下操作提示: 2)在下面选择对应的选项升级os,可以通过tftp.ftp.usb.系统 ...

  7. checkpoint防火墙SmartDashboard登录出错

    SmartDashboard登录是报错:fingerprint不匹配 原因:主备机切换导致 解决:选择凌晨不影响业务的时间拔掉原备机的电源线.

  8. 用BlockingQueue实现的简单发布订阅模式

  9. 4-16 css

    1.背景是图片 <style> body {background-image:url('bgdesert.jpg');} </style> 2. 十六进制 - 如:" ...

  10. JavaSE基础知识(5)—面向对象(5.4面向对象三大特征:封装、继承、多态)

    面向对象编程具有三大特征: 封装 继承 多态 一.封装 1.好处 狭义的封装:也就是属性的封装,避免了任意赋值的危险,提高了数据的安全性! ①隐藏一个类中不需要对外提供的实现细节 ②使用者只能通过实现 ...