当前位置:网站首页>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;
}
}
}
边栏推荐
- Thinking about the integrated communication of air, space and earth based on the "7.20 Zhengzhou rainstorm"
- web安全入门-部署Snort开源IDS/IPS系统
- String type function transfer problem
- Openfoam heat flow boundary condition
- Prospect of 6G global convergence network
- OpenCV编程:OpenCV3.X训练自己的分类器
- Custom complex logic verification during adding and modifying -2022 new project
- Pytoch realizes multi-layer perceptron manually
- vulnhub inclusiveness: 1
- Introduction to virtualization troubleshooting
猜你喜欢

Avi Deployment Guide (2): overview of AVI architecture

Win10 start key click no response

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

Tencent cloud server uses image to deploy WordPress personal website!

ROS duplicate name

Pytoch framework learning record 1 cifar-10 classification

UE4 understanding of animation blueprint
![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]

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

PPDE第二季度迎新 | 欢迎22位AI开发者加入飞桨开发者技术专家计划!
随机推荐
Environment variable configuration of win10
Unity高版本退回低版本报错问题
Satellite network capacity improvement method based on network coding
2022/7/14
The concept of data guard broker and the configuration process of data guard broker
LeetCode 2325. Decrypt message (map)
Opencv programming: opencv3 X trains its own classifier
Explanation of tree chain dissection idea + acwing 2568 Tree chain dissection (DFS sequence + mountain climbing method + segment tree)
Paper notes: mind the gap an empirical evaluation of impaction ofmissing values techniques in timeseries
Modify the default path of jupyter see this article!
(2) Using MySQL
Game theory (Depu) and investment (40/100)
军品研制过程所需文件-进阶版
ThreadLocal变量使用及原理
Qt 两个重载QListWidget控件对象实现selectitem drag拖拽
最大半连通子图(tarjan缩点+拓扑排序+dp最长链)
Mobile keyboard (simulation question)
[in vivado middle note ILA IP core]
Connected graph (union search set)
Introduction to sap appgyver