Ivan and Burgers CodeForces - 1100F (线性基)
大意: 给定n元素序列, m个询问$(l,r)$, 求$[l,r]$中选出任意数异或后的最大值
线性基沙茶题, 直接线段树暴力维护两个log还是能过的
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 5e5+10; int n, q, ql, qr;
int a[N]; struct _ {
int a[22];
void ins(int x) {
REP(i,1,*a) x=min(x,a[i]^x);
if (x) a[++*a]=x;
}
_ operator + (const _ &rhs) const {
_ r;
REP(i,0,*a) r.a[i]=a[i];
REP(i,1,rhs.a[0]) r.ins(rhs.a[i]);
return r;
}
} tr[N<<2]; void build(int o, int l, int r) {
if (l==r) return ({
int t;
scanf("%d", &t);
tr[o].ins(t);
});
build(ls),build(rs);
tr[o]=tr[lc]+tr[rc];
}
_ query(int o, int l, int r) {
if (ql<=l&&r<=qr) return tr[o];
if (mid>=qr) return query(ls);
if (mid<ql) return query(rs);
return query(ls)+query(rs);
} int main() {
scanf("%d", &n);
build(1,1,n);
scanf("%d", &q);
REP(i,1,q) {
scanf("%d%d",&ql,&qr);
auto t = query(1,1,n);
int ans = 0;
REP(i,1,t.a[0]) ans=max(ans,ans^t.a[i]);
printf("%d\n", ans);
}
}
考虑一下一个log的做法, 对于每个基维护一个最后出现的位置, 贪心尽量让高位的位置最大就好了
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
//head const int N = 1e5+10;
int n, q;
int a[N][21], p[N][21]; int main() {
scanf("%d", &n);
REP(i,1,n) {
int t;
scanf("%d", &t);
REP(j,0,20) a[i][j]=a[i-1][j],p[i][j]=p[i-1][j];
int pos = i;
PER(j,0,20) if (t>>j&1) {
if (p[i][j]<pos) swap(a[i][j],t),swap(p[i][j],pos);
if (!t) break;
t ^= a[i][j];
}
}
scanf("%d", &q);
REP(i,1,q) {
int l, r;
scanf("%d%d", &l, &r);
int ans = 0;
PER(j,0,20) if (p[r][j]>=l) ans=max(ans,a[r][j]^ans);
printf("%d\n", ans);
}
}
Ivan and Burgers CodeForces - 1100F (线性基)的更多相关文章
- Codeforces 1100F(线性基+贪心)
题目链接 题意 给定序列,$q(1\leq q \leq 100000) $次询问,每次查询给定区间内的最大异或子集. 思路 涉及到最大异或子集肯定从线性基角度入手.将询问按右端点排序后离线处理询问, ...
- Codeforces1100F. Ivan and Burgers(离线+线性基)
题目链接:传送门 思路: 按查询的右端点离线. 然后从左到右维护线性基. 每个基底更新为最右边的方案,可以让尽量多的查询享受到这个基底. 用ci维护后更新右端点为i的答案. 代码(析构1000ms,别 ...
- Codeforces 1101G(线性基)
题目链接 题意 将序列尽可能分成多段使得任意$x \geq 1$段内的所有元素的异或和大于$0$问最多多少段 思路 首先,如果所有元素异或和等于$0$答案显然为$-1$,否则构造整个序列的线性基,这个 ...
- Codeforces 1100 F - Ivan and Burgers
F - Ivan and Burgers 思路:线性基+贪心,保存线性基中每一位的最后一个 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #p ...
- CodeForces - 1100F:Ivan and Burgers (线性基&贪心)(离线 在线)
题意:给定N个数,Q次询问,求区间最大异或和. 思路:一开始想的线性基+线段树.单次线性基合并的复杂度为20*20,结合线段树,复杂度为O(NlogN*20*20):显然,超时. 超时代码: #inc ...
- codeforces 1100F Ivan and Burgers 线性基 离线
题目传送门 题意: 给出 n 个数,q次区间查询,每次查询,让你选择任意个下标为 [ l , r ] 区间内的任意数,使这些数异或起来最大,输出最大值. 思路:离线加线性基. 线性基学习博客1 线性基 ...
- CodeForces 1100F Ivan and Burgers
CodeForces题面 Time limit 3000 ms Memory limit 262144 kB Source Codeforces Round #532 (Div. 2) Tags da ...
- Codeforces Round #532 (Div. 2):F. Ivan and Burgers(贪心+异或基)
F. Ivan and Burgers 题目链接:https://codeforces.com/contest/1100/problem/F 题意: 给出n个数,然后有多个询问,每次回答询问所给出的区 ...
- F. Ivan and Burgers(线性基,离线)
题目链接:http://codeforces.com/contest/1100/problem/F 题目大意:首先输入n,代表当前有n个数,然后再输入m,代表m次询问,每一次询问是询问区间[l,r], ...
随机推荐
- python之路----进程三
IPC--PIPE管道 #创建管道的类: Pipe([duplex]):在进程之间创建一条管道,并返回元组(conn1,conn2),其中conn1,conn2表示管道两端的连接对象,强调一点:必须在 ...
- python之路----面向对象的多态特性
多态 多态指的是一类事物有多种形态 动物有多种形态:人,狗,猪 import abc class Animal(metaclass=abc.ABCMeta): #同一类事物:动物 @abc.abstr ...
- java输出重定向
Java的标准输入,输出分别是通过System.in和System.out来代表.默认情况下他们分别代表键盘和显示器. System类里提供了3个重定向标准输入,输出的方法. static void ...
- Python入门之面向对象编程(四)Python描述器详解
本文分为如下部分 引言——用@property批量使用的例子来引出描述器的功能 描述器的基本理论及简单实例 描述器的调用机制 描述器的细节 实例方法.静态方法和类方法的描述器原理 property装饰 ...
- Python3.x(windows系统)安装libxml2库
Python3.x(windows系统)安装libxml2库 cmd安装命令: pip install lxml 执行结果: 再执行命令: pip install virtualenv 执行结果:
- 根据wsdl文件,soupUI生成webservice客户端代码
根据wsdl文件,soupUI生成webservice客户端代码 功能介绍: 对于面向WebServie接口开发时,当我们已经获取到WSDL文件后,可以使用soapUI工具生成对应的客户端和服务端代码 ...
- linux django 知识点 安装mysql数据库 和 pycharm
django 命令及相关知识点 1. 启动 pycharm 命令:sh pycharm.sh 2. 创建 django 项目 : django-admin.py startproject Hello ...
- ubuntu服务器安装FTP服务
目录 ubuntu服务器安装FTP服务 一.实验环境 二.安装配置FTP 下载ftp 配置环境 新建用户 ubuntu服务器安装FTP服务 参考教程 [ubuntu16.04搭建ftp服务器 一.实验 ...
- HTML语法分析
什么是HTML htyper text markup language 即超文本标记语言HTML是一个网页的主体部分,也是一个网页的基础.因为一个网页可以没有样式,可以没有交互,但是必须要有网页需要呈 ...
- Leading and Trailing(数论/n^k的前三位)题解
Leading and Trailing You are given two integers: n and k, your task is to find the most significant ...