当前位置:网站首页>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;
}
}
}
边栏推荐
- SVN学习
- NVIDIA uses AI to design GPU: the latest H100 has been used, which reduces the chip area by 25% compared with traditional EDA
- SAP S4 material management inventory module mard database table reading technical details
- Paper notes: mind the gap an empirical evaluation of impaction ofmissing values techniques in timeseries
- leetcode-08
- 火箭大机动运动欧拉角解算的探讨
- 英伟达用AI设计GPU:最新H100已经用上,比传统EDA减少25%芯片面积
- Detailed explanation of multiple linear regression
- The difference between journal log and oplog log
- Use testeract JS offline recognition picture text record
猜你喜欢

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

树链剖分思想讲解 + AcWing 2568. 树链剖分(dfs序 + 爬山法 + 线段树)

早期单片机加密的一些方法 【评论区领取资料】
![Some methods of early MCU encryption [get data in the comment area]](/img/14/8e1dcb799d8a3c0aefcac09be9dc51.png)
Some methods of early MCU encryption [get data in the comment area]

Unity dropdown (editable, inputable) drop-down selection box with Text Association

E-commerce sales data analysis and prediction (date data statistics, daily statistics, monthly statistics)

Pytoch learning record 2 linear regression (tensor, variable)

ENVI_ Idl: use the inverse distance weight method to select the nearest n points for interpolation (bottom implementation) and output them to GeoTIFF format (the effect is equivalent to the inverse di

37. Flex layout

Leetcode ugly number problem solution
随机推荐
剑指 Offer II 041. 滑动窗口的平均值
Unity3d 读取mpu9250 例子原代码
(二)使用MySQL
Huawei machine test: number of continuous licensing
leetcode-08
Svn learning
LeetCode 745. 前缀和后缀搜索
The case has been solved --- no matter how to change the code from the logic of MQ consumption, it will not take effect
空天地海一体化网络体系架构与网络切片技术
Win10安装Apache Jena 3.17
Goldfish rhca memoirs: cl210 describes the openstack control plane -- identify the overcloud control platform service + chapter experiment
Input number pure digital input limit length limit maximum value
Environment variable configuration of win10
Game theory (Depu) and investment (40/100)
6G全域融合网络展望
Avi Deployment Guide (2): overview of AVI architecture
Pytoch realizes multi-layer perceptron manually
Use and principle of ThreadLocal variable
空天地海一体化网络体系架构与网络切片技术
Pytoch and weight decay (L2 norm)