当前位置:网站首页>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;
}
}
}
边栏推荐
- Modify the default path of jupyter see this article!
- 人大、微软等提出InclusiveFL:异构设备上的包容性联邦学习
- The case has been solved --- no matter how to change the code from the logic of MQ consumption, it will not take effect
- PPDE第二季度迎新 | 欢迎22位AI开发者加入飞桨开发者技术专家计划!
- input number 纯数字输入 限制长度 限制 最大值
- Explanation of tree chain dissection idea + acwing 2568 Tree chain dissection (DFS sequence + mountain climbing method + segment tree)
- [handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code
- [Huawei cloud IOT] reading notes, "Internet of things: core technology and security of the Internet of things", Chapter 3 (2)
- 虚拟化排错概论
- Nombre d'entrées nombre d'entrées numériques pures limite de longueur maximale
猜你喜欢

LeetCode 745. 前缀和后缀搜索

How to build dashboard and knowledge base in double chain note taking software? Take the embedded widget library notionpet as an example

Detailed explanation of Euler angle, axis angle, quaternion and rotation matrix

Getting started with web security - deploy snort open source ids/ips system

Today's sleep quality record 79 points

ROS duplicate name

Mobile keyboard (simulation question)

Paper notes: mind the gap an empirical evaluation of impaction ofmissing values techniques in timeseries

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

火箭大机动运动欧拉角解算的探讨
随机推荐
反向散射通信的未来应用与技术挑战
基于网络编码的卫星网络容量提升方法
[PostgreSQL] PostgreSQL 15 optimizes distinct
Introduction to virtualization troubleshooting
最大半连通子图(tarjan缩点+拓扑排序+dp最长链)
Getting started with web security - deploy snort open source ids/ips system
论文笔记:Mind the Gap An Experimental Evaluation of Imputation ofMissing Values Techniques in TimeSeries
Rotation in unity3d
2022/7/15
[leetcode weekly replay] 302 weekly 20220717
微服务上线规范
Game theory (Depu) and investment (40/100)
腾讯云服务器利用镜像部署WordPress个人网站!
空天地海一体化网络体系架构与网络切片技术
vSphere 下借助 vDS 或 NSX 做端口镜像的方法总结
NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices
NVIDIA uses AI to design GPU: the latest H100 has been used, which reduces the chip area by 25% compared with traditional EDA
Pytoch and weight decay (L2 norm)
Some methods of early MCU encryption [get data in the comment area]
6G smart endogenous: technical challenges, architecture and key features