当前位置:网站首页>Weight line segment tree + line segment tree split/merge + CF1659D
Weight line segment tree + line segment tree split/merge + CF1659D
2022-07-30 04:34:00 【Follow a certain R in the distance】
如果一个01数组A进行n次排序,第i次sort前i个,can be formed in the processn个序列,Add the correspondences of these sequences to form a new sequenceC.
现在给定C,Find the original sequence that meets the requirementsA
The context of the topic is rather special,There is a more ingenious construction method,Each location was analyzed1Contribution to before and after.
重要的一点是:如果这个位置是0,但是对应的cThe array has a value at this position,Then a conclusion should be drawn,next positionC[i]-1All positions are1.
#include <iostream>
#define endl '\n'
using namespace std;
const int N=2e5+100;
int a[N],b[N];
int main()
{
int t;
for(cin>>t;t;t--)
{
int n,p=0;
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i];
b[i]=0;
}
for(int i=1;i<=n;i++)
{
if(p<i)
{
if(a[i])
b[i]=1;
else
b[i]=0;
}
if(b[i]==1)
{
for(++p;p<=a[i];++p)
b[p]=1;
b[p]=0;
}
else
{
for(++p;p<=a[i]+i-1;++p)
b[p]=1;
b[p]=0;
}
}
for(int i=1;i<=n;i++)
cout<<b[i]<<" ";
cout<<endl;
}
return 0;
}
The understanding of the split operation is really not very thorough,Still need to ponder,Other aspects are common operations such as:动态开点,nA line segment tree or something,There are also some operations on the weight segment tree.
#include <bits/stdc++.h>
#define int long long
#define ls lson[rt]
#define rs rson[rt]
#define mid ((l+r)>>1)
#define endl '\n'
#define fastio cout.tie(0);cin.tie(0);ios::sync_with_stdio(0)
using namespace std;
const int N = 2e5+100;
int gra[N<<5],tree[N<<5],lson[N<<5],rson[N<<5],cnt,tot,root[N<<5],a[N],seq=1,n,m;
int new_node()
{
return cnt?gra[cnt--]:++tot;
}
void del_node(int p)
{
gra[++cnt]=p;
tree[p]=0;lson[p]=0;rson[p]=0;
return ;
}
void fix(int &rt,int l,int r,int num,int x)
{
if(!rt)
rt=new_node();
tree[rt]+=x;
if(l==r)
return ;
if(num<=mid)
fix(ls,l,mid,num,x);
else
fix(rs,mid+1,r,num,x);
}
int query_num(int rt,int l,int r,int nl,int nr)
{
if(l>nr||r<nl)
return 0;
if(nl<=l&&r<=nr)
return tree[rt];
return query_num(ls,l,mid,nl,nr)+query_num(rs,mid+1,r,nl,nr);
}
int query_kth(int rt,int l,int r,int num)
{
if(l==r)
return l;
if(tree[ls]>=num)
return query_kth(ls,l,mid,num);
else
return query_kth(rs,mid+1,r,num-tree[ls]);
}
int merge(int x,int y)
{
if(!x||!y)
return x+y;
tree[x]+=tree[y];
lson[x]=merge(lson[x],lson[y]);
rson[x]=merge(rson[x],rson[y]);
del_node(y);
return x;
}
void split(int x,int &y,int k)
{
if(x==0)
return;
y=new_node();
int v=tree[lson[x]];
if(k>v)
split(rson[x],rson[y],k-v);
else
swap(rson[x],rson[y]);
if(k<v)
split(lson[x],lson[y],k);
tree[y]=tree[x]-k;
tree[x]=k;
return ;
}
signed main()
{
//fastio;
cin>>n>>m;
for(int i=1,temp;i<=n;i++)
{
cin>>temp;
fix(root[1],1,n,i,temp);
}
for(int i=1,op,p,k,x,q,y,t;i<=m;i++)
{
cin>>op;
if(op==0)
{
cin>>p>>x>>y;
int k1=query_num(root[p],1,n,1,y);
int k2=query_num(root[p],1,n,x,y);
int temp=0;
split(root[p],root[++seq],k1-k2);
split(root[seq],temp,k2);
root[p]=merge(root[p],temp);
}
if(op==1)
{
cin>>p>>t;
root[p]=merge(root[p],root[t]);
}
if(op==2)
{
cin>>p>>x>>q;
fix(root[p],1,n,q,x);
}
if(op==3)
{
cin>>p>>x>>y;
cout<<query_num(root[p],1,n,x,y)<<endl;
}
if(op==4)
{
cin>>p>>k;
if(tree[root[p]]<k)
{
cout<<-1<<endl;
continue;
}
cout<<query_kth(root[p],1,n,k)<<endl;
}
}
return 0;
}
/* 5 12 0 0 0 0 0 2 1 1 1 2 1 1 2 2 1 1 3 3 1 1 3 4 1 2 2 1 1 4 2 1 1 5 0 1 2 4 2 2 1 4 3 2 2 4 1 1 2 4 1 3 */
权值线段树,What I understand is resettable segment trees.An unordered array is given,An ordered segment tree is constructed after several insertion operations,Each node represents how many values this interval has,The leaf nodes represent the number of occurrences of each value.
常见的操作有:
离散化,动态开点,查询区间第k小/大,The number of query intervals,增加/删除x个点/数.
#include<bits/stdc++.h>
#define ls lson[rt]
#define rs rson[rt]
#define mid ((l+r)>>1)
using namespace std;
const int N = 1e7+100;
int m;
int lson[N<<2],rson[N<<2],tree[N<<2],cnt,root;
void up(int rt)
{
tree[rt]=tree[ls]+tree[rs];
}
void fix(int &rt,int l,int r,int p,int v)
{
if(!rt)
{
rt=++cnt;
}
if(l==r)
{
tree[rt]+=v;
return ;
}
if(p<=mid)
fix(ls,l,mid,p,v);
if(p>mid)
fix(rs,mid+1,r,p,v);
up(rt);
}
int query_num(int rt,int l,int r,int pl,int pr)
{
int ans=0;
if(pl<=l&&r<=pr)
return tree[rt];
if(pl<=mid)
ans+=query_num(ls,l,mid,pl,pr);
if(pr>mid)
ans+=query_num(rs,mid+1,r,pl,pr);
return ans;
}
int query_kth(int rt,int l,int r,int k)
{
if(l==r)
return l;
int s=tree[ls];
if(k<=s)
return query_kth(ls,l,mid,k);
else
return query_kth(rs,mid+1,r,k-s);
}
map<int,int> ma;
int main()
{
scanf("%d",&m);
while(m--)
{
int opt,x;scanf("%d%d",&opt,&x);
if(opt==1)
{
fix(root,1,N*2,x+N,1);//The absolute value given in the title is less than or equal to1e7So here we need to move the negative value field to the positive number
ma[x]++;
}
if(opt==2)
{
fix(root,1,N*2,x+N,-1);
ma[x]--;
if(ma[x]==0)
ma.erase(x);
}
if(opt==3)
{
printf("%d\n",query_num(root,1,N*2,1,x+N-1)+1);
}
if(opt==4)
{
printf("%d\n",query_kth(root,1,N*2,x)-N);
}
if(opt==5)
printf("%d\n",(--ma.lower_bound(x))->first);
if(opt==6)
printf("%d\n",(ma.upper_bound(x))->first);
}
return 0;
}
边栏推荐
- 解决go环境编译不了exe
- Chapter8 Support Vector Machines
- The implementation and basic operation of sub-database sub-table, ER table, global table, fragmentation rules, global sequence, etc. in MyCat
- C. Qualification Rounds(思维,特情)
- A must see for software testers!Database knowledge MySQL query statement Daquan
- MNIST of Dataset: MNIST (handwritten digital image recognition + ubyte.gz file) data set introduction, download, usage (including data enhancement) detailed guide
- MySQL data query (subtotal and sorting)
- 2.4希尔排序
- Go 学习笔记(84)— Go 项目目录结构
- Database Design of Commodity Management System--SQL Server
猜你喜欢
MYSQL unique constraint
The Azure developer news 丨 memorabilia in July
厦门感芯科技MC3172(1):介绍和环境搭建
解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b
2.6归并排序
Chapter8 支持向量机
我的Go+语言初体验——祝福留言小系统,让她也可以感受到你的祝福
swagger使用教程——快速使用swagger
图像视角矫正之透视变换矩阵(单应矩阵)/findHomography 与 getPerspectiveTransformd的区别
Database Design of Commodity Management System--SQL Server
随机推荐
解决报错SyntaxError: (unicode error) ‘utf-8‘ codec can‘t decode byte 0xb7 in position 0: invalid start b
MySQL 字符串拼接 - 多种字符串拼接实战案例
1. 获取数据-requests.get()
Notes on "The Law of Construction"---Chapter 10 Typical Users and Scenarios
Excellent MySQL interview questions, 99% must ask in preparation for August!I can't pass the interview
(题目练习)条件概率+权值线段树+FWT+后缀数组
cv2.polylines
Go 学习笔记(84)— Go 项目目录结构
PyG搭建R-GCN实现节点分类
Go study notes (84) - Go project directory structure
2021山东省网络搭建与应用赛项试题
state space representation
【翻译】Envoy Fundamentals,这是一个培训课程,使人们能够更快地采用Envoy Proxy。...
Charles 替换 接口响应信息
swagger使用教程——快速使用swagger
MySQL 操作语句大全(详细)
Simple experiment with BGP
KubeMeet Registration | The complete agenda of the "Edge Native" Online Technology Salon has been announced!
C. Qualification Rounds(思维,特情)
Image stitching (registration) case based on OpenCV