线段树+离散化 IP地址段检查 SEGMENT TREE
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的更多相关文章
- POJ2528Mayor's posters[线段树 离散化]
Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 59683 Accepted: 17296 ...
- POJ1151Atlantis 矩形面积并[线段树 离散化 扫描线]
Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21734 Accepted: 8179 Descrip ...
- POJ 2528 Mayor's posters(线段树+离散化)
Mayor's posters 转载自:http://blog.csdn.net/winddreams/article/details/38443761 [题目链接]Mayor's posters [ ...
- [poj2528] Mayor's posters (线段树+离散化)
线段树 + 离散化 Description The citizens of Bytetown, AB, could not stand that the candidates in the mayor ...
- BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针
BZOJ_4653_[Noi2016]区间_线段树+离散化+双指针 Description 在数轴上有 n个闭区间 [l1,r1],[l2,r2],...,[ln,rn].现在要从中选出 m 个区间, ...
- D - Mayor's posters(线段树+离散化)
题目: The citizens of Bytetown, AB, could not stand that the candidates in the mayoral election campai ...
- POJ 1151Atlantis 矩形面积并[线段树 离散化 扫描线]
Atlantis Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 21734 Accepted: 8179 Descrip ...
- hdu1542 矩形面积并(线段树+离散化+扫描线)
题意: 给你n个矩形,输入每个矩形的左上角坐标和右下角坐标. 然后求矩形的总面积.(矩形可能相交). 题解: 前言: 先说说做这道题的感受: 刚看到这道题顿时就懵逼了,几何 烂的渣渣.后来从网上搜题解 ...
- Mayor's posters (线段树+离散化)
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
随机推荐
- C++学习笔记(一)之指针
指向指针的引用 ; int * p; int *&r = p; //r为对指针p的引用 r = &i; //r为对p的引用,故对r赋值即将p指向i *r = ; //更新i的值 通过* ...
- MySql备份表数据
一:根据user表创建user_backup表 drop table if exists user_backup; create table user_backup like user; // lik ...
- 百度人脸识别AI实践.doc
0, 前言 百度开放了很多AI能力,其中人脸识别就是其中之一. 本文对百度人脸识别AI进行实践检验,看看其使用效果如何. 鉴于是最为基础的实践,基本都是在其接口范例代码修改而来. 百度人脸识别AI网站 ...
- vue基础---计算属性和侦听器
[一]计算属性 模板内的表达式非常便利,但是设计它们的初衷是用于简单运算的.在模板中放入太多的逻辑会让模板过重且难以维护.例如: <div id="example"> ...
- 配置redis三主三从
主从环境 centos7.6 redis4.0.1 主 从 192.168.181.139:6379 192.168.181.136:6379 192.168.181.136:6380 192.168 ...
- Spring Boot 2.0的属性绑定
Spring Boot2.0的属性绑定 原文从Spring boot第一个版本以来,我们可以使用@ConfigurationProperties注解将属性绑定到对象.也可以指定属性的各种不同格式.比如 ...
- 第二节:Css重写样式
一丶 进入浏览器---->F12----->找到要修改的区域的Style 进行重写Css样式 二丶打开新页面 window.open("/Persitent/OtherIndex ...
- vue的路由配置
路由,其实就是指向的意思,当我点击页面上的home按钮时,页面中就要显示home的内容,如果点击页面上的about 按钮,页面中就要显示about 的内容.Home按钮 => home 内容, ...
- 1043 输出PATest (20 分)
题目链接:1043 输出PATest (20 分) 这道题目很简单,遍历整个字符串,统计相应字符的个数,然后按照题目要求进行输出即可. #include <bits/stdc++.h> u ...
- java基础学习日志---File方法分析
package FunDemo; import java.io.File; import java.io.IOException; import java.util.Arrays; public cl ...