• loj2046 「CQOI2016」路由表


    大傻逼trie树,更傻逼的是我这都没独立想出来,以后要少看题解,多多思考

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <vector>
    using namespace std;
    int m, cnt, s[20000005][2], now, orz[35], tag[20000005], faq;
    char ss[15];
    struct Node{
    	int idx, dis;
    }nd[35];
    void rn(int &x){
    	char ch=getchar();
    	x = 0;
    	while(ch<'0' || ch>'9')	ch = getchar();
    	while(ch>='0' && ch<='9'){
    		x = x * 10 + ch - '0';
    		ch = getchar();
    	}
    }
    void insert(){
    	int uu, uv, uw, ux, uy;
    	rn(uu); rn(uv); rn(uw); rn(ux); rn(uy);
    	now++;
    	for(int i=7; i>=0; i--)
    		orz[24+i] = (uu>>i)&1;
    	for(int i=7; i>=0; i--)
    		orz[16+i] = (uv>>i)&1;
    	for(int i=7; i>=0; i--)
    		orz[8+i] = (uw>>i)&1;
    	for(int i=7; i>=0; i--)
    		orz[i] = (ux>>i)&1;
    	int u=0;
    	for(int i=31; 31-i+1<=uy; i--){
    		if(!s[u][orz[i]])	s[u][orz[i]] = ++cnt;
    		u = s[u][orz[i]];
    	}
    	tag[u] = now;
    }
    bool cmp(Node x, Node y){
    	return x.idx<y.idx;
    }
    int query(){
    	int uu, uv, uw, ux, uy, uz;
    	rn(uu); rn(uv); rn(uw); rn(ux); rn(uy); rn(uz);
    	for(int i=7; i>=0; i--)
    		orz[24+i] = (uu>>i)&1;
    	for(int i=7; i>=0; i--)
    		orz[16+i] = (uv>>i)&1;
    	for(int i=7; i>=0; i--)
    		orz[8+i] = (uw>>i)&1;
    	for(int i=7; i>=0; i--)
    		orz[i] = (ux>>i)&1;
    	faq = 0;
    	int u=0;
    	for(int i=31; i>=0; i--){
    		if(!s[u][orz[i]])	break;
    		u = s[u][orz[i]];
    		if(tag[u])
    			nd[++faq] = (Node){tag[u], 31-i+1};
    	}
    	sort(nd+1, nd+1+faq, cmp);
    	int re=0, maxdis=0;
    	for(int i=1; i<=faq; i++){
    		if(nd[i].idx>=uy && nd[i].idx<=uz && (i==1 || nd[i].dis>maxdis))
    			re++;
    		maxdis = max(maxdis, nd[i].dis);
    	}
    	return re;
    }
    int main(){
    	cin>>m;
    	while(m--){
    		scanf("%s", ss);
    		if(ss[0]=='A')	insert();
    		else	printf("%d
    ", query());
    	}
    	return 0;
    }
    
  • 相关阅读:
    LeetCode.152
    python
    python
    python
    python
    python
    python
    python
    PyCharm2020.1激活码破解教程
    Python GUI编程(Tkinter)17、Menu鼠标右键菜单
  • 原文地址:https://www.cnblogs.com/poorpool/p/8955146.html
Copyright © 2020-2023  润新知