BSoj : 4608 【CQOI2016】路由表

4608 – 【CQOI2016】路由表
Description
這裏寫圖片描述
Input

這裏寫圖片描述
Output
這裏寫圖片描述
Sample Input
11
A 0.0.0.0/8
Q 1.2.3.4 1 1
A 1.0.0.0/9
A 1.128.0.0/10
A 1.0.0.0/10
A 1.0.0.0/8
Q 1.2.3.4 1 5
A 1.2.0.0/16
A 1.2.3.1/32
Q 1.2.3.4 5 7
Q 1.2.3.1 5 7
Sample Output
0
2
1
2

Hint
數據範圍:
設一條表項的掩碼長度爲L,數據保證將目的地址轉爲二進制串後,末尾的32-L位均爲0。
這裏寫圖片描述
對於一次查詢的一種理解方式是:無視其它所有查詢操作,只看添加操作。先清空路由表,然後執行第1到a-1次添加操作。之後再執行第a到b次添加操作過程中,統計匹配改變的次數。

應該是省選最菜的題了吧,
我也只能A這種菜題了。。。。
具體的做法是:
維護一顆TRIE樹,
每個節點只有兩個兒子,
分別代表0和1。
最深只有32層。
根據數據範圍,
應該是能存下的。
插入的時候轉換一下,
只保留掩碼長度的前幾位。
正常插入就行了。
標記的時候要注意,
用當前詢問的次數作爲標記的值。
查詢是一段區間,
用一個單調棧來維護。
從根節點起按照地址遍歷,
碰到打了標記的並且標記 右區間就進棧。
由於是單調棧,
標記的值是單調遞增的。
最後從棧頂找到棧裏第一個小於左區間的值的位置k
top-k就是答案。
代碼如下:

#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cstdlib>
using namespace std;
int Stack[1000];
int top=0;
struct Trie{
    int l;//1
    int r;//0
    int flag;
    void init(){
        l=0;
        r=0;
        flag=0;
    }
}Tree[20000000];
int cnt=0;
int length=0;
bool get[35];
int Root;
void build(int &root){
    root=++cnt;
    Tree[root].init();
}
void insert(int &root,int len,int flag){
    if(!root){root=++cnt;Tree[root].init();}
    if(len==length){Tree[root].flag=flag;return ;}
    if(get[len+1]==1){
        insert(Tree[root].l,len+1,flag);
    }
    else {
        insert(Tree[root].r,len+1,flag);
    }
}
void query(int root,int x,int y,int len){
    if(Tree[root].flag!=0&&Tree[root].flag<=y){
        while(top>0&&Stack[top]>=Tree[root].flag)top--;
        top++;
        Stack[top]=Tree[root].flag;
    }
    if(len==32)return;
    if(get[len+1]==1){
        if(Tree[root].l)query(Tree[root].l,x,y,len+1);
    }
    else {
        if(Tree[root].r)query(Tree[root].r,x,y,len+1);
    }
}
int main(){
//  freopen("route.in","r",stdin);
//  freopen("route.out","w",stdout);
    int n;
    char ch;
    scanf("%d",&n);
    build(Root);
    int t=1;
    for(int i=1;i<=n;i++){
        ch=getchar();
        while(ch!='A'&&ch!='Q')ch=getchar();
        if(ch=='A'){
            int a,b,c,d;
            scanf("%d.%d.%d.%d/%d",&a,&b,&c,&d,&length);
            memset(get,0,sizeof(get));
            for(int j=7;j>=0;j--)if((1<<j)&a)get[8-j]=1;
            for(int j=7;j>=0;j--)if((1<<j)&b)get[16-j]=1;
            for(int j=7;j>=0;j--)if((1<<j)&c)get[24-j]=1;
            for(int j=7;j>=0;j--)if((1<<j)&d)get[32-j]=1;
            insert(Root,0,t);
            t++;
        }
        if(ch=='Q'){
            int a,b,c,d,x,y;
            scanf("%d.%d.%d.%d%d%d",&a,&b,&c,&d,&x,&y);
            memset(get,0,sizeof(get));
            for(int j=7;j>=0;j--)if((1<<j)&a)get[8-j]=1;
            for(int j=7;j>=0;j--)if((1<<j)&b)get[16-j]=1;
            for(int j=7;j>=0;j--)if((1<<j)&c)get[24-j]=1;
            for(int j=7;j>=0;j--)if((1<<j)&d)get[32-j]=1;
            memset(Stack,0,sizeof(Stack));
            top=0;
            query(Root,x,y,0);int ans=0;
            while(Stack[top]>=x)ans++,top--;
            printf("%d\n",ans);
        }
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章