当前位置:网站首页>CodeForces - 587E(线性基+线段树+差分)
CodeForces - 587E(线性基+线段树+差分)
2022-07-17 14:00:00 【吃花椒的妙酱】
题意:n个数,两种操作,操作1是区间异或上k,操作2是求区间[l,r]选任意数所能表示出的不同异或和的数量。
思路:由操作2不难想到线段树上维护线性基。
但是由于操作1是区间修改,区间修改后的线性基合并似乎有点困难?
有个很nb的做法,对数组进行异或差分,这样操作一就转化成两个单点修改的操作了。差分数组的线性基是和原数组等价。
转化方式如下:
设原数组为a,差分数组为d,其中d[i] = a[i]^a[i-1]
假设我们要用d数组得到a[l…r]的线性基,我们先求得a[l],然后向线性基里加入d[l+1],就能得到a[l+1],以此类推直到加入d[r]。得到的线性基,就和a[l…r]的等价了!(不用顺序加入的)。这样,维护差分数组,再用树状数组维护a[i]即可快速修改和维护区间线性基。非常牛逼的性质!
关于线性基的合并:最无脑的实现就是把左儿子右儿子线性基里的元素加入到父亲的线性基即可,反正才30的常数233。
赛时没想到差分,太牛逼了这题!
#include<iostream>
#include<algorithm>
#include<math.h>
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define IOS ios::sync_with_stdio(false),cin.tie(0)
#define _for(i,a,b) for(int i=(a) ;i<=(b) ;i++)
#define _rep(i,a,b) for(int i=(a) ;i>=(b) ;i--)
#define mst(v,s) memset(v,s,sizeof(v))
#define pii pair<int ,int >
#define pb(v) push_back(v)
#define all(v) v.begin(),v.end()
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
#define endl "\n"
#define fi first
#define se second
#define ls p<<1
#define rs p<<1|1
#define lson p<<1,l,mid
#define rson p<<1|1,mid+1,r
#define AC return 0
#define ldb long double
const int N = 2e5+5;
const int mx = 29;
int A[N],B[N];
int n;
struct TreeArray{
int c[N],maxn;//需要预先设定
int lowbit(int x){
return x&(-x);}
int sum(int i ){
if( !i ) return 0;//需要0的话,全体+1
int ans=0;
while( i ){
ans ^= c[i];
i -= lowbit(i);
}
return ans;
}
void add(int i ,int val){
while( i<=maxn ){
c[i] ^= val;
i += lowbit(i);
}
return;
}
}ta;
struct LB{
int a[30+1],temp[30+1];
int siz=0;
LB(){
mst(a,0),mst(temp,0);
}
bool insert(int t){
for(int i=mx ;i>=0; i--){
if( !(t&(1<<i))) continue;
if(!a[i]){
a[i]=t;
temp[++siz] = a[i];
return true;
}
t ^= a[i];
}
return false;
}
void clear(){
_for(i,0,mx){
a[i]=0;
}
siz = 0;
}
}L[N<<2];
void pu(int p){
L[p].clear();
_for(i,1,L[ls].siz) L[p].insert(L[ls].temp[i]);
_for(i,1,L[rs].siz) L[p].insert(L[rs].temp[i]);
}
void build(int p,int l,int r){
if( l==r ){
L[p].insert(A[l]);
return;
}
int mid = (l+r)>>1;
build(lson),build(rson);
pu(p);
}
void update(int p,int l,int r,int x,int wei){
if( l==r ){
A[l] ^= wei;
L[p].clear();
L[p].insert(A[l]);
return;
}
int mid = (l+r)>>1;
if( x<=mid ) update(lson,x,wei);
else update(rson,x,wei);
pu(p);
}
LB Q(int p,int l,int r,int x,int y){
if( x<=l && r<=y ){
return L[p];
}
int mid = (l+r)>>1;
LB t1,t2,t3;
if( x<=mid ) t1 = Q(lson,x,y);
if( y>mid ) t2 = Q(rson,x,y);
_for(i,1,t1.siz) t3.insert(t1.temp[i]);
_for(i,1,t2.siz) t3.insert(t2.temp[i]);
return t3;
}
signed main(){
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif
IOS;
int q;
cin>>n>>q;
ta.maxn = n+2;
_for(i,1,n) cin>>B[i];
_for(i,1,n){
A[i] = B[i]^B[i-1];
}
build(1,1,n);
while( q-- ){
int op;cin>>op;
if( op==1 ){
int l,r;cin>>l>>r;
int x;cin>>x;
ta.add(l,x);
ta.add(r+1,x);
update(1,1,n,l,x);
if(r+1<=n) update(1,1,n,r+1,x);
}
else {
int l,r;cin>>l>>r;
int h = B[l]^ta.sum(l);
if( l==r ){
if( h ) h = 1;
cout<<(1<<h)<<endl;
continue;
}
auto ans = Q(1,1,n,l+1,r);
int t = ans.siz;
if( ans.insert(h) )t++;
cout<<(1<<t)<<endl;
}
}
}
边栏推荐
- mpu9250 ky9250姿态、角度模块和mpu9250 mpl dma对比
- 剑指 Offer II 041. 滑动窗口的平均值
- [handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code
- Unity dropdown (editable, inputable) drop-down selection box with Text Association
- 2022/7/15
- Win10的环境变量配置
- Environment variable configuration of win10
- 基于“7·20郑州特大暴雨”对空天地一体化通信的思考
- NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices
- Definable 6G security architecture
猜你喜欢

Environment variable configuration of win10

Journal日志与oplog日志的区别

剑指 Offer II 041. 滑动窗口的平均值

LeetCode 558. 四叉树交集

如何在 RHEL 9 中更改和重置忘记的root密码

【手写数字识别】基于Lenet网络实现手写数字识别附matlab代码

Beego framework realizes file upload + seven cattle cloud storage

Win10 start key click no response

Mysql优化系列之limit查询

Pytoch framework learning record 1 cifar-10 classification
随机推荐
(二)使用MySQL
今日睡眠质量记录79分
[design process] Net ORM FreeSQL wheredynamicfilter dynamic table query function
Explanation of tree chain dissection idea + acwing 2568 Tree chain dissection (DFS sequence + mountain climbing method + segment tree)
(2) Using MySQL
antd 下拉多选传值到后台做查询操作
Openfoam heat flow boundary condition
Custom complex logic verification during adding and modifying -2022 new project
ThreadLocal变量使用及原理
腾讯云服务器利用镜像部署WordPress个人网站!
XSS.haozi.me刷题
Svn learning
String type function transfer problem
Mysql索引的类型(单列索引、组合索引 btree索引 聚簇索引等)
Pytoch realizes multi-layer perceptron manually
Integrated network architecture and network slicing technology of air, earth and sea
Daily question brushing record (26)
Journal日志与oplog日志的区别
Use testeract JS offline recognition picture text record
How can enterprise telecommuting be more efficient?