HIHO #1079 : 离散化(线段树+离散化)
来源:互联网 发布:炫踪网络上市计划启动 编辑:程序博客网 时间:2024/05/17 02:18
题目链接
在线段树的通常用法中,线段树的节点是有2种不同的意义的,一种是离散型的,比如在Hiho一下 第二十周中,一个节点虽然描述的是一个区间[3, 9],但是实际上这样一个区间是{3, 4, 5, 6, 7, 8, 9}这样的意义。而另一种就是连续型的,比如就在这一周的问题中,一个节点如果描述的是一个区间[3, 9],它就确确实实描述的是在数轴上从3这个标记到9这个标记的这一段。
那么有的小朋友可能就要问了,这两种不同的意义有什么区别呢?
在小Hi看来,其实只有这样的几个区别:1.叶子节点:在离散型中,叶子节点是[i, i],而连续性中是[i, i +
1];2.分解区间:在离散型中,一段区间是分解成为[l, m], [m + 1, r],而在连续型中,是分解成为[l, m], [m,
r];3.其他所有类似的判定问题。
区间的范围比较大,进行离散化后,数据就是连续在一起的了,
但是这样是会有一个问题的 ,比如[1,2], [3,4]中间是有一个空白的[2,3],
解决这个问题:
A)离散化时候处理
B)线段树处理
#include<bits/stdc++.h>using namespace std;#define cl(a,b) memset(a,b,sizeof(a))#define LL long long#define pb push_back#define gcd __gcd#define For(i,j,k) for(int i=(j);i<k;i++)#define lowbit(i) (i&(-i))#define _(x) printf("%d\n",x)const int maxn = 1e5+10;const int inf = 1 << 28;vector<pair<int,int> > seg;int f[maxn<<1];struct node{ int color; int lazy; node(){lazy=color=0;}}p[maxn<<2];void push_down(int rt){ if(p[rt].lazy){ p[rt<<1].lazy=p[rt<<1|1].lazy = p[rt].lazy; p[rt<<1].color = p[rt<<1|1].color=p[rt].color; p[rt].lazy=0; }}void updata(int rt,int l,int r,int x,int y,int v){ if(x<=l&&r<=y){ p[rt].color = v; p[rt].lazy = v; return ; } int mid = l+r>>1; push_down(rt); if(x<=mid) updata(rt<<1,l,mid,x,y,v); if(y> mid) updata(rt<<1|1,mid+1,r,x,y,v);}set<int> s;void query(int rt,int l,int r,int x,int y){ if(l==r){ if(p[rt].color)s.insert(p[rt].color); return ; } if(p[rt].lazy){ s.insert(p[rt].color);return ; } int mid = l+r>>1; push_down(rt); if(x<=mid) query(rt<<1,l,mid,x,y); if(y> mid) query(rt<<1|1,mid+1,r,x,y);}int main(){ int n,m; scanf("%d%d",&n,&m); int tot = 0; for(int i=0;i<n;i++){ int x,y;scanf("%d%d",&x,&y); if(x>y)swap(x,y); seg.push_back(make_pair(x,y)); f[tot++] = x; f[tot++] = y; } sort(f,f+tot); tot = unique(f,f+tot) - f; for(int i=0;i<n;i++){ int a = lower_bound(f,f+tot,seg[i].first)-f+1; int b = lower_bound(f,f+tot,seg[i].second)-f+1; updata(1,1,tot,a,b-1,i+1);//更新时候右端点减一,弥补 } query(1,1,tot,1,tot); printf("%d\n",s.size()); return 0;}
0 0
- HIHO #1079 : 离散化(线段树+离散化)
- [hiho]#1079 : 离散化 线段树
- hiho 21 线段树 离散化
- hiho #1079 离散化
- hihoCoder - 1079 - 离散化 (线段树 + 离散化)
- hihoCoder 1079 离散化(线段树离散化)
- hiho一下第二十一周 #1079 : 离散化 【线段树储存区间状态】
- 线段树+离散化
- 离散化 + 线段树
- 线段树离散化
- 线段树离散化
- hihocoder 1079 离散化(线段树+区间离散化)
- hihcoder #1079 离散化 线段树+区间离散化
- HihoCoder 1079 线段树 + 离散化
- hihoCoder 1079 : 离散化(线段树之连续区间+离散化) 经典题型
- hiho一下 第二十一周 离散化与线段树回顾
- PKU 2528 (线段树 + 离散化)
- PKU 3277(线段树 + 离散化)
- Web前端学习总结
- instanceof和isInstance(Object obj) 和isAssignableFrom(Class cls)的区别和联系
- hadoop-3.0新特性
- my97 WdatePicker 使用大全
- 2.sed语法与常用选项
- HIHO #1079 : 离散化(线段树+离散化)
- 数据结构实验之二叉树五:层序遍历
- Android4.3 蓝牙BLE初步
- Openwrt下设置端口映射 DMZ 访问控制
- spring模板模式与callback
- Shell 字符串处理、获取文件名和后缀名
- vector<bool>效率问题的原因和替代方法
- 二叉排序树
- hdu2108Shape of HDU 计算几何