当前位置:网站首页>ACM寒假集训#5
ACM寒假集训#5
2022-07-28 10:13:00 【MJ的码农空间】
线段树
线段树是一种二叉搜索树,与区间树相似,它将一个区间划分成一些单元区间,每个单元区间对应线段树中的一个叶结点。
使用线段树可以快速的查找某一个节点在若干条线段中出现的次数,时间复杂度为O(logN)。而未优化的空间复杂度为2N,实际应用时一般还要开4N的数组以免越界,因此有时需要离散化让空间压缩。
对于线段树中的每一个非叶子节点[a,b],它的左儿子表示的区间为[a,(a+b)/2],右儿子表示的区间为[(a+b)/2+1,b]。因此线段树是平衡二叉树,最后的子节点数目为N,即整个线段区间的长度。
使用线段树可以快速的查找某一个节点在若干条线段中出现的次数,时间复杂度为O(logN)。而未优化的空间复杂度为2N,因此有时需要离散化让空间压缩。
基本结构
线段树是建立在线段的基础上,每个结点都代表了一条线段[a,b]。长度为1的线段称为元线段。非元线段都有两个子结点,左结点代表的线段为[a,(a + b) / 2],右结点代表的线段为[((a + b) / 2)+1,b]。
长度范围为[1,L] 的一棵线段树的深度为log (L) + 1。这个显然,而且存储一棵线段树的空间复杂度为O(L)。
线段树支持最基本的操作为插入和删除一条线段。下面以插入为例,详细叙述,删除类似。
将一条线段[a,b] 插入到代表线段[l,r]的结点p中,如果p不是元线段,那么令mid=(l+r)/2。如果b<mid,那么将线段[a,b] 也插入到p的左儿子结点中,如果a>mid,那么将线段[a,b] 也插入到p的右儿子结点中。
插入(删除)操作的时间复杂度为O(logn)。
基本代码
支持以下操作
1 x 若x不存在,插入x
2 x 若x存在,删除x
3 输出当前最小值,若不存在输出-1
4 输出当前最大值,若不存在输出-1
5 x 输出x的前驱,若不存在输出-1
6 x 输出x的后继,若不存在输出-1
7 x 若x存在,输出1,否则输出-1
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#define inf 1000000000
using namespace std;
int n,m;
struct seg{
int l,r,v;}t[3000005];
void build(int k,int l,int r) //建树 k:当前节点下标 线段左为l,线段右为r
{
t[k].l=l;t[k].r=r; //线段左端,线段右端
if(l==r)return; //线段长度为零,结束
int mid=(l+r)>>1; //取线段中点
build(k<<1,l,mid); //k<<1:下标为k节点的左儿子下标,线段左为l,线段右为mid k<<1==k*2
build(k<<1|1,mid+1,r); //k<<1|1:下标为k节点的右儿子下标,线段左为mid+1,线段右为r k<<1|1==k*2+1
}
int mn(int k)
{
if(!t[k].v)return -1;
int l=t[k].l,r=t[k].r;
if(l==r)return l;
if(t[k<<1].v)return mn(k<<1);
else return mn(k<<1|1);
}
int mx(int k)
{
if(!t[k].v)return -1;
int l=t[k].l,r=t[k].r;
if(l==r)return l;
if(t[k<<1|1].v)return mx(k<<1|1);
else return mx(k<<1);
}
void insert(int k,int val)
{
int l=t[k].l,r=t[k].r;
if(l==r){
t[k].v=1;return;}
int mid=(l+r)>>1;
if(val<=mid)insert(k<<1,val);
else insert(k<<1|1,val);
t[k].v=t[k<<1].v+t[k<<1|1].v;
}
int find(int k,int val)
{
int l=t[k].l,r=t[k].r;
if(l==r)
{
if(t[k].v)return 1;
return -1;
}
int mid=(l+r)>>1;
if(val<=mid)return find(k<<1,val);
else return find(k<<1|1,val);
}
void del(int k,int val)
{
int l=t[k].l,r=t[k].r;
if(l==r){
t[k].v=0;return;}
int mid=(l+r)>>1;
if(val<=mid)del(k<<1,val);
else del(k<<1|1,val);
t[k].v=t[k<<1].v+t[k<<1|1].v;
}
int findpr(int k,int val)
{
if(val<0)return -1;
if(!t[k].v)return -1;
int l=t[k].l,r=t[k].r;
if(l==r)return l;
int mid=(l+r)>>1;
if(val<=mid)return findpr(k<<1,val);
else
{
int t=findpr(k<<1|1,val);
if(t==-1)return mx(k<<1);
else return t;
}
}
int findsu(int k,int val)
{
if(!t[k].v)return -1;
int l=t[k].l,r=t[k].r;
if(l==r)return l;
int mid=(l+r)>>1;
if(val>mid)return findsu(k<<1|1,val);
else
{
int t=findsu(k<<1,val);
if(t==-1)return mn(k<<1|1);
else return t;
}
}
int main()
{
scanf("%d %d",&n,&m);
build(1,0,n);
int opt,x;
for(int i=1;i<=m;i++)
{
scanf("%d",&opt);
switch(opt)
{
case 1:scanf("%d",&x);if(find(1,x)==-1)insert(1,x);break;
case 2:scanf("%d",&x);if(find(1,x)==1)del(1,x);break;
case 3:printf("%d\n",mn(1));break;
case 4:printf("%d\n",mx(1));break;
case 5:scanf("%d",&x);printf("%d\n",findpr(1,x-1));break;
case 6:scanf("%d",&x);printf("%d\n",findsu(1,x+1));break;
case 7:scanf("%d",&x);printf("%d\n",find(1,x));break;
}
}
return 0;
}
边栏推荐
- Problem summary file
- 阿里云镜像地址
- 5. Dynamic programming -- Fibonacci series
- Detailed explanation of super complete knowledge points of instruction system
- 基于docker安装MySQL
- 13、哈希表——两个链表第一个公共节点
- 10、链表中倒数第k个节点
- 中兴通讯:5nm 5G基站芯片正在技术导入!
- Install MySQL under centos7, and the online articles are not accurate
- a different object with the same identifier value was already associated with the session
猜你喜欢

IDEA创建我的第一个项目

什么样的知识付费系统功能,更有利于平台与讲师发展?

API 网关 APISIX 在Google Cloud T2A 和 T2D 的性能测试

15. Judge whether the target value exists in the two-dimensional array

Performance test of API gateway APIs IX in Google cloud T2a and T2D

C语言 二级指针详解及示例代码
![[wechat applet] project practice - lottery application](/img/7b/a0545f077259b3dc971dc246813177.png)
[wechat applet] project practice - lottery application

11. Linked list inversion

Typora tutorial

IDEA打包jar包及运行jar包命令
随机推荐
6、双指针——递增数组两数之和与目标数相等
Install MySQL under centos7, and the online articles are not accurate
什么样的知识付费系统功能,更有利于平台与讲师发展?
13. Hash table - the first common node of two linked lists
Uni app advanced life cycle
️雄关漫道真如铁,而今迈步从头越️
死锁算法:银行家算法和安全性算法
Sort - quick sort (fast and slow pointer Implementation)
The IPO of SMIC International Technology Innovation Board passed smoothly, and its market value is expected to exceed 200billion!
Huawei takes a 10% stake in fullerene technology, a graphene material manufacturer
发力大核、独显!英众科技2020十代酷睿独显产品发布
指令系统超全知识点详解
Etcd (highly available kV database)
不登高山,不知天之高也;不临深溪,不知地之厚也
SQL Server 2016 学习记录 ---视图
SQL Server 2016 学习记录 --- 数据定义
11、链表反转
Detailed explanation of thread synchronization volatile and synchronized
Multithreading and high concurrency (III) -- source code analysis AQS principle
巧用ngx_lua做流量分组