当前位置:网站首页>Bucket of P (segment tree + linear basis)
Bucket of P (segment tree + linear basis)
2022-06-26 13:58:00 【__ LazyCat__】
Line segment tree + Linear base
link :P4839 P Brother's bucket - Luogu | New ecology of computer science education (luogu.com.cn)
The question : Insert data at a single point in the interval , Find the exclusive or sum of the largest subset of the interval .
Answer key : Consider the combination of linear bases , Let's take another linear base log Take out the numbers and merge them into the first linear basis , Because numbers that can be synthesized from another linear basis , After merging this linear basis into the first linear basis , The first linear basis can also be combined into a second linear basis , Then we can synthesize those numbers through the second linear basis . Then the union of these linear bases can be maintained by using the line segment tree . Since the linear basis merging complexity is l o g ∗ l o g log*log log∗log, Line tree single point modification , The interval query complexity is q l o g n qlogn qlogn, Then the overall complexity is q ∗ l o g n ∗ l o g 2 p q*logn*log^2p q∗logn∗log2p,q For the number of operations ,n Is the length of the sequence ,p Value range .
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=5e4+5;
int n,m,op,u,v,w;
struct node{
int l,r,p[32];
node():l(0),r(0){
memset(p,0,sizeof(p));}
}t[maxn<<2];
void insert(node&t,int x)
{
for(int i=31;i>=0;i--)
{
if(x>>i&1)
{
if(!t.p[i]){
t.p[i]=x; break;}
x^=t.p[i];
}
}
return;
}
void pushup(int k)
{
memcpy(t[k].p,t[k<<1].p,sizeof(t[k].p));
for(int i=31;i>=0;i--)
{
insert(t[k],t[k<<1|1].p[i]);
}
return;
}
void build(int k,int l,int r)
{
t[k].l=l,t[k].r=r;
if(l==r)return;
int mid=l+r>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
return;
}
void update(int k,int pos,int w)
{
int x=t[k].l,y=t[k].r;
if(x==y){
insert(t[k],w); return;}
int mid=x+y>>1;
if(pos<=mid)update(k<<1,pos,w);
else update(k<<1|1,pos,w);
pushup(k); return;
}
node query(int k,int l,int r)
{
int x=t[k].l,y=t[k].r;
if(l<=x&&y<=r)return t[k];
int mid=x+y>>1,flag=0; node res;
if(l<=mid)res=query(k<<1,l,r),flag=1;
if(mid<r)
{
if(!flag)res=query(k<<1|1,l,r);
else
{
node q=query(k<<1|1,l,r); res.r=q.r;
for(int i=31;i>=0;i--)insert(res,q.p[i]);
}
}
return res;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>m,build(1,1,m);
for(int i=1;i<=n;i++)
{
cin>>op>>u>>v;
if(op==1)update(1,u,v);
else
{
node q=query(1,u,v); int ans=0;
for(int i=31;i>=0;i--)ans=max(ans,ans^q.p[i]);
cout<<ans<<"\n";
}
}
return 0;
}
You can find , This kind of merger is really right , Complexity is also correct . But consider , When we update, we merge from the bottom to the top , Each time, the number will be added to the original linear basis , No fewer numbers . So there is no need to merge , You only need to insert the traversed points when updating .
//#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
const int maxn=5e4+5;
int n,m,op,u,v,w;
struct node{
int l,r,p[32];
node():l(0),r(0){
memset(p,0,sizeof(p));}
}t[maxn<<2];
void insert(node&t,int x)
{
for(int i=31;i>=0;i--)
{
if(x>>i&1)
{
if(!t.p[i]){
t.p[i]=x; break;}
x^=t.p[i];
}
}
return;
}
void build(int k,int l,int r)
{
t[k].l=l,t[k].r=r;
if(l==r)return;
int mid=l+r>>1;
build(k<<1,l,mid);
build(k<<1|1,mid+1,r);
return;
}
void update(int k,int pos,int w)
{
int x=t[k].l,y=t[k].r; insert(t[k],w);
if(x==y)return;
int mid=x+y>>1;
if(pos<=mid)update(k<<1,pos,w);
else update(k<<1|1,pos,w);
return;
}
void query(node&q,int k,int l,int r)
{
int x=t[k].l,y=t[k].r;
if(l<=x&&y<=r)
{
for(int i=31;i>=0;i--)insert(q,t[k].p[i]);
return;
}
int mid=x+y>>1;
if(l<=mid)query(q,k<<1,l,r);
if(mid<r)query(q,k<<1|1,l,r);
return;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin>>n>>m,build(1,1,m);
for(int i=1;i<=n;i++)
{
cin>>op>>u>>v;
if(op==1)update(1,u,v);
else
{
node q; query(q,1,u,v); int ans=0;
for(int i=31;i>=0;i--)ans=max(ans,ans^q.p[i]);
cout<<ans<<"\n";
}
}
return 0;
}
Query operation small optimization : It is found that it takes too much time to re create the structure every time each point is queried . Then create one initially and insert it slowly .
边栏推荐
- 基于PyTorch的生成对抗网络实战(7)——利用Pytorch搭建SGAN(Semi-Supervised GAN)生成手写数字并分类
- Gee - Global Human Settlements grid data 1975-1990-2000-2014
- 【系统分析师之路】第十五章 复盘数据库系统(数据库案例分析)
- Embedded virlog code running process
- MediaPipe手势(Hands)
- CVPR 2022文档图像分析与识别相关论文26篇汇集简介
- Pointer
- [path of system analyst] Chapter 15 double disk database system (database case analysis)
- GC is not used in D
- [hcsd application development training camp] one line of code second cloud evaluation article - experience from the experiment process
猜你喜欢
ES6 module
Tips for using nexys A7 development board resources
What is the use of index aliases in ES
character constants
Ring queue PHP
Basic type of typescript
There are many contents in the widget, so it is a good scheme to support scrolling
MongoDB系列之Window环境部署配置
33、使用RGBD相机进行目标检测和深度信息输出
Pytorch based generation countermeasure Network Practice (7) -- using pytorch to build SGAN (semi supervised GaN) to generate handwritten digits and classify them
随机推荐
ES基於Snapshot(快照)的數據備份和還原
MySQL configuration improves data insertion efficiency
ES中索引别名(alias)的到底有什么用
Select tag - uses the default text as a placeholder prompt but is not considered a valid value
7-1 n queen problem
计算两点之间的距离(二维、三维)
7-16 monetary system I
微信小程序注册指引
Lamp compilation and installation
It is better and safer to choose which securities company to open an account for flush stock
7-2 picking peanuts
Es common grammar I
三维向量的夹角
Gee - Global Human Settlements grid data 1975-1990-2000-2014
【MySQL从入门到精通】【高级篇】(二)MySQL目录结构与表在文件系统中的表示
Luogu p3426 [poi2005]sza-template solution
Postman自动化接口测试
古瑞瓦特冲刺港交所上市:创下“多个第一”,获IDG资本9亿元投资
[hcsd application development training camp] one line of code second cloud evaluation article - experience from the experiment process
array