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 终极篇 ---django 认证
Django自带的用户认证 我们在开发一个网站的时候,无可避免的需要设计实现网站的用户系统.此时我们需要实现包括用户注册.用户登录.用户认证.注销.修改密码等功能,这还真是个麻烦的事情呢. Djang ...
- 375. Clone Binary Tree【LintCode java】
Description For the given binary tree, return a deep copy of it. Example Given a binary tree: 1 / \ ...
- Multi-task Correlation Particle Filter for Robust Object Tracking--论文随笔
摘要:在这篇论文中,作者提出一种鲁棒视觉跟踪的多任务相关粒子滤波琪跟踪算法(MCPF).作者首先向我们展示了多任务相关滤波器,该滤波器在训练滤波器模板的时候可以学习不同特征之间的联系.本文提出的MCP ...
- maven 教程二 深入
一:编写POM <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w ...
- ZOJ 3644 Kitty's Game(数论+DP)
Description Kitty is a little cat. She is crazy about a game recently. There arenscenes in the game( ...
- bootstrap使用中遇到的坑
一.例如: <div class="form-group"> <label class="control-label col-lg-3"> ...
- UVALive - 6868 Facility Locations 想法题
题目链接: http://acm.hust.edu.cn/vjudge/problem/88634 Facility Locations Time Limit: 3000MS 题意 给你一个m*n的矩 ...
- WIN7使用过360系统急救箱后出现的任务计划程序文件夹删除的办法
直接进主题(怀疑系统有问题用了下360系统急救箱,用完后发现计划任务多了个360superkiller文件夹,右键直接是删除不了的) 尝试了各种方法都是不爽,突然想到计划任务不是在在系统盘下的一个文件 ...
- C#中的unsafe
为了保持类型安全性,默认情况下,C# 不支持指针算法. 但是,通过使用 unsafe 关键字,可以定义可在其中使用指针的不安全上下文. 有关指针的详细信息,请参阅主题指针类型. 备注 在公共语言运行时 ...
- dwarf是怎样处理的栈帧?
dwarf是如何处理的栈帧呢? 首先看下非dwarf的情况是如何处理栈帧的: 1 3623804982590 0x3e90 [0xb0]: PERF_RECORD_SAMPLE(IP, 0x1): 1 ...