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. Unity笔记(3)自学第二天

    学习记录: 界面使用: 脚本使用: 脚本注意点:

  2. Android 你知道界面布局嵌套多少层之后会Crash吗

    我们先放一张Hierarchy Viewer的图:(模拟器Android4.4) 看到数字6了吗,那个RelativeLayout是MainActivity的根ViewGroup, 而在Relativ ...

  3. OC语言Block

    OC语言Block 一.Block (一)简介  Block是什么?苹果推荐的比较特殊的数据类型,效率高,在运行中保存代码.用来封装和保存代码,有点像函数,BLOCK可以在任何时候执行. Block和 ...

  4. java读取03、07版EXCEL

    03版excel,需要用到jxl.jar这个jar包 package test.poi; import java.io.File; import java.io.IOException; import ...

  5. Java实现打包文件

    把文件打包到压缩包里 public void zip (String... files) throws IOException { //创建文件打包流对象 ZipOutputStream zos = ...

  6. JavaScript 实现页面中录音功能

    页面中实现录音需要使用浏览器提供的 Media​Recorder API,所以前提是需要浏览器支持 MediaStream Recording 相关的功能. 以下代码默认工作在 Chrome 环境中. ...

  7. HDU多校Round 4

    Solved:3 rank:405................................. B. Harvest of Apples 知道了S(n,m) 可以o(1)的求S(n - 1, m ...

  8. Redis多实例配置以及主从同步

    一.多实例配置 1.准备俩配置文件,开两个就准备两个 redis-6380.conf redis-6381.conf 2.分别写入配置信息(这里简化了配置) # 运行在6380端口 bind 172. ...

  9. python面向对象的特点,类定义等,私有属性、公有属性、成员属性

    引子:类的对象在内存中的表示def dog(name,dog_type): def bark(d): print(d,'wang wang wang ...') data = { 'name':nam ...

  10. Effective C++ 一些记录和思考

    Effective C++ Iter 3 - 尽可能使用 const 一个反逻辑的 bitwise const class Text { ... char& operator[](std::s ...