Problem:

Give a series of IP segments, for example, [0.0.0.1-0.0.0.3], [123.234.232.21-123.245.21.1]...

Now there is a new IP, find which IP segment it's in ?

Solution:

First, we could map the ends of IP segments into some intervals, since the IP address could be represented by a unsigned int. This is called discretization.

Second, setup the segment tree. Every leaf node is [x, x+1], it means whether an IP segment includes the part. We could use a vector<int> to record the interval index for searching. So the core code is:

#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <limits.h>
#include <assert.h>
#include <stdio.h>
using namespace std; class Interval {
public:
int l, r;
Interval(int ll = 0, int rr = 0) : l(ll), r(rr) {}
};
class Node {
public:
int l, r;
Node *lc, *rc;
vector<int> IPs; Node(int ll = 0, int rr = 0) : l(ll), r(rr), lc(NULL), rc(NULL) {}
}; Node* build(int l, int r) {
Node *root = new Node();
root->l = l, root->r = r;
if (l + 1 >= r)
return root;
int mid = (l + r) / 2;
root->lc = build(l, mid);
root->rc = build(mid, r);
return root;
}
void insert(Node* root, int l, int r, int idx) {
if (l <= root->l && r >= root->r) {
root->IPs.push_back(idx);
return;
}
int mid = (root->l + root->r) / 2;
if (l < mid) // Take care here!
insert(root->lc, l, mid, idx);
if (r > mid) // Take care here!
insert(root->rc, mid, r, idx);
}
void search(Node* root, int l, int r, vector<int>& res) {
if (r <= root->r && l >= root->l) {
for (int i = 0; i < root->IPs.size(); ++i)
res.push_back(root->IPs[i]);
}
int mid = (root->l + root->r) / 2;
if (r <= mid)
search(root->lc, l, mid, res);
if (l >= mid)
search(root->rc, mid, r, res);
}
int main()
{
Node* rt = build(1, 7);
Interval inters[] = {Interval(1,3),Interval(2,4),Interval(1,5), Interval(5,7)};
int size = sizeof(inters) / sizeof(inters[0]);
for (int i = 0; i < size; ++i)
insert(rt, inters[i].l, inters[i].r, i);
vector<int> res;
search(rt, 2, 3, res);
return 0;
}

线段树+离散化 IP地址段检查 SEGMENT TREE的更多相关文章

  1. POJ2528Mayor's posters[线段树 离散化]

    Mayor's posters Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 59683   Accepted: 17296 ...

  2. POJ1151Atlantis 矩形面积并[线段树 离散化 扫描线]

    Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21734   Accepted: 8179 Descrip ...

  3. POJ 2528 Mayor's posters(线段树+离散化)

    Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...

  4. [poj2528] Mayor's posters (线段树+离散化)

    线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...

  5. BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针

    BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...

  6. D - Mayor's posters(线段树+离散化)

    题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...

  7. POJ 1151Atlantis 矩形面积并[线段树 离散化 扫描线]

    Atlantis Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 21734   Accepted: 8179 Descrip ...

  8. hdu1542 矩形面积并(线段树+离散化+扫描线)

    题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...

  9. Mayor's posters (线段树+离散化)

    Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...

随机推荐

  1. 第3章 DOM

    1.节点,dom有3种节点,元素节点,文本节点,属性节点 2.元素节点是dom的原子,所有的属性节点和文本节点都被元素包含,但并不是所有的元素都包含他们 3.继承,节点树上的元素将继承父元素的样式和属 ...

  2. 联想 K5 Pro(L38041)免解锁BL 免rec 保留数据 ROOT Magisk Xposed 救砖 ZUI 5.0.188

    >>>重点介绍<<< 第一:本刷机包可卡刷可线刷,刷机包比较大的原因是采用同时兼容卡刷和线刷的格式,所以比较大第二:[卡刷方法]卡刷不要解压刷机包,直接传入手机后用 ...

  3. 【C++】朝花夕拾——中缀转后缀

    对于简单的四则运算而言,后缀表达式可以通过使用栈(stack)快速算出结果 ==================================我是分割线======================= ...

  4. python3 操作excel表

    python操作excel主要用到xlrd和xlwt这两个库,即xlrd是读excel,xlwt是写excel的库可从这里下载https://pypi.python.org/pypi.下面分别记录py ...

  5. string 字符串--------redis

    APPEND 语法:APPEND KEY VALUE 如果key已经存在并且是一个字符串,append 命令将value追加到key原来的值的末尾. 如果key不存在,append就简单地将给定key ...

  6. JAVA基础——异常--解析

      简介 异常处理是java语言的重要特性之一,<Three Rules for effective Exception Handling>一文中是这么解释的:它主要帮助我们在debug的 ...

  7. 完善本地搭建的jekyll环境(Windows)

    序:上篇文章虽然在本地搭建好了jekyll环境,但是却存在一些问题,如通过jekyll new创建的站点无法正常跑起来.中文编码有问题.这说明之前搭建的环境有不周之处. PS:因之前自己搭建环境时并未 ...

  8. 关于C++中字符串输入get与getline的区别

    最近使用C++中自己老是忘记的一个点,get与getline的区别. 1.get与getline get和getline所属iostream类,作用是读取一整行,通过换行符确定读取结束,他们都可以读取 ...

  9. python3 判断大小写

    转自http://wangwei007.blog.51cto.com/68019/1134323 # 一.pyhton字符串的大小写转换, 常用的有以下几种方法: # 1.对字符串中所有字符(仅对字母 ...

  10. 封装的一些常见的JS DOM操作和数据处理的函数.

    //用 class 获取元素 function getElementsByClass(className,context) { context = context || document; if(do ...