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. JavaScript开发心得--如何传递某行数据给下一页

    1, 应用场景 在某个html页面显示一批数据,如20个用户的名称.年龄等,每行都要一个编辑按钮,点击编辑后,将此行数据带入某个专门的编辑页进行显示,修改后保存. 问题是 点击编辑按钮后,如何得知要编 ...

  2. 如何让一个div里面的div垂直居中?

    如何让一个div里面的div垂直居中? 如何让上面灰色有文字那个div和背景图标垂直居中,不管屏幕大小有好大,始终在垂直方向上的中间.上面有整个布局和样式表,谢谢高手指点 CSS3时代当然要用CSS3 ...

  3. 网络编程基础_4.1TCP_服务端

    TCP_服务端 #include <stdio.h> // 1. 包含必要的头文件和库, 必须位于 windows之前 #include <WinSock2.h> #pragm ...

  4. Java代码实现WORD转PDF

    第一步: 安装OpenOffice   在此良心提供windows版本安装文件 链接:https://pan.baidu.com/s/17pPCkcS1C46VtLhevqSgPw  密码:vmlu ...

  5. vue学习总结(简单介绍)

    声明式渲染 Vue.js 的核心是一个允许采用简洁的模板语法来声明式地将数据渲染进 DOM 的系统: <div id="app"> {{ message }} < ...

  6. ThinkPHP---thinkphp会话支持和文件载入

    [一]会话控制 会话支持一般指cookie和session,在ThinkPHP里为了方便开发,封装了cookie和session方法. (1)session方法 在函数库封装了session方法 se ...

  7. UVA - 12325 Zombie's Treasure Chest (分类搜索)

    题目: 有一个体积为N的箱子和两种数量无限的宝物.宝物1的体积为S1,价值为V1:宝物2的体积为S2,价值为V2.输入均为32位带符号整数.计算最多能装多大价值的宝物,每种宝物都必须拿非负整数个. 思 ...

  8. chrome://plugins 无法打开的解决方法,同时解决“该网页已屏蔽插件-adobe flash player”

    chrome打开想要看视频时提示该网页已屏蔽插件-adobe flash player,在网上查了半天说在chrome plugins里面打开就可以了.可是chrome://plugins 无法打开, ...

  9. calculate Cp history (from Fluent) using Matlab

    input data : unscaled time history of moment/thrust from ANSYS fluent example of input data, "m ...

  10. 【03】WAMPServer集成环境下载和安装

    WAMPServer集成环境下载和安装1.W:windows,A:Apache,M:MySQL,P:PHP2.下载WAMP开发包网址:www.wampserver.com           3.安装 ...