HDU 4760 Good Firewall (Trie)

來源:互聯網
上載者:User

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <vector>#define pii pair<int,LL>using namespace std;typedef long long LL;const int N = 100005;struct Trie {    Trie *next[2];    vector<pii> a;}*root , s[N << 2];char ope[10];int tot = 0 , can[N] , ok[1025] , idx = 0;LL ip (int a , int b , int c , int d) {    LL ret = 0;    ret |= (LL)a << 24;    ret |= (LL)b << 16;    ret |= (LL)c << 8;    ret |= d;    return ret;}Trie *newnode () {    Trie *t = &s[tot ++];    t -> next[0] = t -> next[1] = NULL;    t -> a.clear();    return t;}void insert (Trie *p , LL ip , int l , int id) {    for (int i = 0 ; i < l ; i ++) {        int c = (ip >> (31 - i)) & 1;        if (p -> next[c] == NULL)            p -> next[c] = newnode ();        p = p -> next[c];    }    LL num = ip & ((1 << (32 - l)) - 1);    p -> a.push_back (make_pair (id , num));}void down (LL ip) {    Trie *p = root;    for (int i = 31 ; i >= 0 ; i --) {        int c = (ip >> i) & 1;        if (p -> next[c] == NULL) return ;        p = p -> next[c];        for (int j = 0 ; j < p -> a.size() ; j ++) {            if (can[p -> a[j].first] && p -> a[j].second <= (ip & ((1 << (i - 1)) - 1)))                ok[p -> a[j].first] = idx;        }    }}bool up (LL ip) {    Trie *p = root;    for (int i = 31 ; i >= 0 ; i --) {        int c = (ip >> i) & 1;        if (p -> next[c] == NULL) return false;        p = p -> next[c];        for (int j = 0 ; j < p -> a.size() ; j ++) {            if (can[p -> a[j].first] && p -> a[j].second <= (ip & ((1 << (i - 1)) - 1)) && ok[p -> a[j].first] == idx)                return true;        }    }    return false;}int main () {    #ifndef ONLINE_JUDGE        freopen ("input.txt" , "r" , stdin);        // freopen ("output.txt" , "w" , stdout);    #endif    root = newnode ();    while (scanf ("%s" , ope) != EOF) {        if (ope[0] == 'E') {            int id , k , a , b , c , d , u;            scanf ("%d %d" , &id , &k);            for (int i = 0 ; i < k ; i ++) {                scanf ("%d.%d.%d.%d/%d" , &a , &b , &c , &d , &u);                insert (root , ip (a , b , c , d) , u , id);            }            can[id] = 1;        }        else if (ope[0] == 'D') {            int id;scanf ("%d" , &id);            can[id] = 0;        }        else {            idx ++;            int a , b , c , d;            scanf ("%d.%d.%d.%d" , &a , &b , &c , &d);            down (ip (a , b , c , d));            scanf ("%d.%d.%d.%d" , &a , &b , &c , &d);            puts (up (ip (a , b , c , d)) ? "F" : "D");        }    }    return 0;}       


相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.