SOJ-2500(IP过滤,对区间排序,合并区间,然后对区间二分查找)

/******************************************************************************************************
 ** Copyright (C) 2011.07.01-2013.07.01
 ** Author: famousDT <[email protected]>
 ** Edit date: 2011-09-27
******************************************************************************************************/
#include <stdio.h>
#include <stdlib.h>//abs,atof(string to float),atoi,atol,atoll
#include <math.h>//atan,acos,asin,atan2(a,b)(a/b atan),ceil,floor,cos,exp(x)(e^x),fabs,log(for E),log10
#include <vector>
#include <queue>
#include <map>
#include <time.h>
#include <set>
#include <stack>
#include <string>
#include <iostream>
#include <assert.h>
#include <string.h>//memcpy(to,from,count
#include <ctype.h>//character process:isalpha,isdigit,islower,tolower,isblank,iscntrl,isprll
#include <algorithm>
using namespace std;

typedef long long ll;

#define MY_PI acos(-1)
#define MY_MAX(a, b) ((a) > (b) ? (a) : (b))
#define MY_MIN(a, b) ((a) < (b) ? (a) : (b))
#define MY_MALLOC(n, type) ((type *)malloc((n) * sizeof(type)))
#define MY_ABS(a) (((a) >= 0) ? (a) : (-(a)))
#define MY_INT_MAX 0x7fffffff

/*==========================================================*\
| 对区间排序,合并区间,然后对区间二分查找
\*==========================================================*/

struct segment
{
    ll low;
    ll high;
} seg[1000005];

ll pos;

bool cmp(segment a, segment b)
{
    return a.low < b.low;
}

bool search(ll x)
{
    ll l = 0;
    ll h = pos - 1;
    while (l <= h) {
        ll m = (l + h) >> 1;
        if (seg[m].low <= x && seg[m].high >= x) {
            return true;
        } else if (x < seg[m].low) {
            h = m - 1;
        } else {
            l = m + 1;
        }
    }
    return false;
}

ll char_number(char s[])
{
    ll a, b, c, d;
    sscanf(s, "%lld.%lld.%lld.%lld", &a, &b, &c, &d);
    return a * 1000 * 1000 * 1000 + b * 1000 * 1000 + c * 1000 + d;
}

int main()
{
    char s[50], t1[50], t2[50];
    ll index = 0;
    ll i, len;
    while (gets(s) != NULL && strcmp(s, "#") != 0) {
        sscanf(s, "%s %s", t1, t2);
        len = strlen(t1);
        seg[index].low = char_number(t1);
        len = strlen(t2);
        seg[index].high = char_number(t2);
        if (seg[index].low > seg[index].high)
            swap(seg[index].low, seg[index].high);
        ++index;
    }
    sort(seg, seg + index, cmp);
    pos = 0;
    ll t_low = seg[0].low;
    ll t_high = seg[0].high;
    for (i = 0; i < index; ++i) {
        if (seg[i].low <= t_high) {
            if (seg[i].high > t_high) {
                t_high = seg[i].high;
            }
        } else {
            seg[pos].low = t_low;
            seg[pos++].high = t_high;
            t_low = seg[i].low;
            t_high = seg[i].high;
        }
    }
    seg[pos].low = t_low;
    seg[pos++].high = t_high;

    while (gets(s) != NULL) {
        len = strlen(s);
        ll ans = char_number(s);
        printf("%s\n", search(ans) == true ? "yes" : "no");
    }
    return 0;
}


 

你可能感兴趣的:(c,Date,String,null,search,float)