当前位置:网站首页>Codeforces - 587e (linear basis + segment tree + difference)
Codeforces - 587e (linear basis + segment tree + difference)
2022-07-19 11:10:00 【Wonderful sauce with pepper】
The question :n Number , Two kinds of operations , operation 1 Is interval XOR k, operation 2 Is to find the interval [l,r] Choose the number of different XORs and sums that any number can represent .
Ideas : Operated by 2 It is not difficult to think of maintaining a linear basis on the line segment tree .
But because of the operation 1 It is interval modification , The merging of interval modified linear bases seems a little difficult ?
There is a very nb How to do it , XOR the array , In this way, operation 1 will be transformed into two single point modification operations . The linear basis of the difference array is equivalent to the original array .
The transformation method is as follows :
Set the original array to a, The difference array is d, among d[i] = a[i]^a[i-1]
Suppose we're going to use d Array a[l…r] The linear base , Let's get a[l], Then add d[l+1], You can get a[l+1], And so on until you join d[r]. The obtained linear basis , Just like a[l…r] Is equivalent to !( Not in sequence ). such , Maintain differential arrays , Then maintain the tree array a[i] Can quickly modify and maintain interval linear basis . Very awesome nature !
On the combination of linear bases : The most brainless realization is to add the elements in the linear basis of the left son and the right son to the linear basis of the father , Just... Anyway 30 The constant 233.
I didn't expect a difference during the game , This question is so awesome !
#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;// It needs to be preset
int lowbit(int x){
return x&(-x);}
int sum(int i ){
if( !i ) return 0;// need 0 Words , All of you +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;
}
}
}
边栏推荐
- (二)使用MySQL
- 空天地海一体化网络体系架构与网络切片技术
- LeetCode 2335. Minimum total time required to fill the cup
- Daily question brushing record (26)
- 2022/7/16
- XSS.haozi.me刷题
- Mysql索引的类型(单列索引、组合索引 btree索引 聚簇索引等)
- Introduction to sap appgyver
- NPC, Microsoft, etc. proposed inclusivefl: inclusive federal learning on heterogeneous devices
- OpenCV编程:OpenCV3.X训练自己的分类器
猜你喜欢

Win10 install Apache Jena 3.17

腾讯云服务器利用镜像部署WordPress个人网站!

Evaluation method of machine learning model

vSphere 下借助 vDS 或 NSX 做端口镜像的方法总结

leetcode-08

发明闪存能赚多少钱?这是一个日本的狗血故事
![[handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code](/img/17/97b46355dbfa02608af2f91d7d6409.png)
[handwritten numeral recognition] handwritten numeral recognition based on lenet network with matlab code

Establishment of redis cluster, one master, two slave and three Sentinels

Antd drop-down multiple options to transfer values to the background for query operations

Svn learning
随机推荐
[leetcode weekly replay] 302 weekly 20220717
Rotation in unity3d
6G中的卫星通信高效天基计算技术
37. Flex layout
论文笔记:Mind the Gap An Experimental Evaluation of Imputation ofMissing Values Techniques in TimeSeries
Huawei machine test: Message decompression
一个报错, Uncaught TypeError: ModalFactory is not a constructor
6G smart endogenous: technical challenges, architecture and key features
博弈论(depu)与投资(40/100)
Mysql 自增id、uuid与雪花id
ThreadLocal变量使用及原理
军品研制过程所需文件-进阶版
Over fitting and under fitting
String type function transfer problem
IP SAN has an independent file system. After the application server accesses the IP SAN through the network sharing protocol, it can read and write the files in the file system
Use testeract JS offline recognition picture text record
Nombre d'entrées nombre d'entrées numériques pures limite de longueur maximale
Beego framework realizes file upload + seven cattle cloud storage
Integrated network architecture and network slicing technology of air, earth and sea
Qt 两个重载QListWidget控件对象实现selectitem drag拖拽