【LOJ】#2046. 「CQOI2016」路由表
题解
题面太长无法阅读系列……
这里说的选择改变指的是在下面区间里碰上了一个更长的可匹配的地址,如果可匹配但是匹配长度没有当前的值大,那么不算改变
我们建一个可持久化的trie,查询的时候先在前\(a - 1\)个里找到最长的可以得到的地址
然后再在区间的trie里找到那条链上,如果碰到一个结束点且比我们初始长度大的路径串,就丢进一个栈里,维护栈的递增
最后栈的长度就是答案
代码
#include <bits/stdc++.h>
#define enter putchar('\n')
#define space putchar(' ')
#define pii pair<int,int>
#define fi first
#define se second
#define MAXN 1000005
#define pb push_back
#define mp make_pair
#define eps 1e-8
//#define ivorysi
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
    res = 0;T f = 1;char c = getchar();
    while(c < '0' || c > '9') {
        if(c == '-') f = -1;
        c = getchar();
    }
    while(c >= '0' && c <= '9') {
        res = res * 10 + c - '0';
        c = getchar();
    }
    res *= f;
}
template<class T>
void out(T x) {
    if(x < 0) {x = -x;putchar('-');}
    if(x >= 10) out(x / 10);
    putchar('0' + x % 10);
}
int M;
char s[35];
struct node {
    int son[2],siz,ed;
}tr[MAXN * 32];
int Ncnt,rt[MAXN],cnt;
int sta[MAXN],top;
void Insert(int v,int l) {
    rt[++cnt] = ++Ncnt;
    int *y = &rt[cnt],x = rt[cnt - 1];
    tr[*y] = tr[x];
    for(int i = 31 ; i >= 31 - l + 1 ; i--) {
        int c = (v >> i) & 1;
        y = &tr[*y].son[c];x = tr[x].son[c];
        *y = ++Ncnt;
        tr[*y] = tr[x];
        tr[*y].siz++;
    }
    if(!tr[*y].ed) tr[*y].ed = cnt;
}
int Get_MaxL(int a,int v) {
    int p = rt[a];
    int res = 0;
    for(int i = 31 ; i >= 0 ; --i) {
        int c = (v >> i) & 1;
        if(tr[p].son[c]) p = tr[p].son[c];
        else break;
        if(tr[p].ed) res = max(res,31 - i + 1);
    }
    return res;
}
int Query(int L,int R,int v,int len) {
    top = 0;
    --L;
    int x = rt[L],y = rt[R];
    for(int i = 31 ; i >= 0 ; --i) {
        int c = (v >> i) & 1;
        if(tr[tr[y].son[c]].siz - tr[tr[x].son[c]].siz) {
            y = tr[y].son[c];x = tr[x].son[c];
        }
        else break;
        if(tr[y].ed && 31 - i + 1 > len) {
            while(top && sta[top] > tr[y].ed) --top;
            sta[++top] = tr[y].ed;
        }
    }
    return top;
}
void Solve() {
    read(M);
    int a,b;
    while(M--) {
        scanf("%s",s + 1);
        if(s[1] == 'A') {
            scanf("%s",s + 1);
            int L = strlen(s + 1);
            int v = 0,now = 0;
            for(int i = 1 ; i <= L ; ++i) {
                if(s[i] == '.' || s[i] == '/') {
                    v = v << 8 | now;
                    now = 0;
                }
                else now = now * 10 + s[i] - '0';
            }
            Insert(v,now);
        }
        else {
            scanf("%s",s + 1);
            int L = strlen(s + 1);
            int v = 0,now = 0;
            for(int i = 1 ; i <= L ; ++i) {
                if(s[i] == '.') {
                    v = v << 8 | now;
                    now = 0;
                }
                else now = now * 10 + s[i] - '0';
            }
            v = v << 8 | now;
            read(a);read(b);
            L = Get_MaxL(a - 1,v);
            out(Query(a,b,v,L));enter;
        }
    }
}
int main() {
#ifdef ivorysi
    freopen("f1.in","r",stdin);
#endif
    Solve();
    return 0;
}
【LOJ】#2046. 「CQOI2016」路由表的更多相关文章
- loj #2044. 「CQOI2016」手机号码
		#2044. 「CQOI2016」手机号码 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ... 
- loj2046 「CQOI2016」路由表
		大傻逼trie树,更傻逼的是我这都没独立想出来,以后要少看题解,多多思考 #include <algorithm> #include <iostream> #include & ... 
- LoibreOJ 2042. 「CQOI2016」不同的最小割 最小割树 Gomory-Hu tree
		2042. 「CQOI2016」不同的最小割 内存限制:256 MiB时间限制:1000 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ... 
- Loj #2192. 「SHOI2014」概率充电器
		Loj #2192. 「SHOI2014」概率充电器 题目描述 著名的电子产品品牌 SHOI 刚刚发布了引领世界潮流的下一代电子产品--概率充电器: 「采用全新纳米级加工技术,实现元件与导线能否通电完 ... 
- Loj #3096. 「SNOI2019」数论
		Loj #3096. 「SNOI2019」数论 题目描述 给出正整数 \(P, Q, T\),大小为 \(n\) 的整数集 \(A\) 和大小为 \(m\) 的整数集 \(B\),请你求出: \[ \ ... 
- Loj #3093. 「BJOI2019」光线
		Loj #3093. 「BJOI2019」光线 题目描述 当一束光打到一层玻璃上时,有一定比例的光会穿过这层玻璃,一定比例的光会被反射回去,剩下的光被玻璃吸收. 设对于任意 \(x\),有 \(x\t ... 
- Loj #3089. 「BJOI2019」奥术神杖
		Loj #3089. 「BJOI2019」奥术神杖 题目描述 Bezorath 大陆抵抗地灾军团入侵的战争进入了僵持的阶段,世世代代生活在 Bezorath 这片大陆的精灵们开始寻找远古时代诸神遗留的 ... 
- Loj #2542. 「PKUWC2018」随机游走
		Loj #2542. 「PKUWC2018」随机游走 题目描述 给定一棵 \(n\) 个结点的树,你从点 \(x\) 出发,每次等概率随机选择一条与所在点相邻的边走过去. 有 \(Q\) 次询问,每次 ... 
- Loj #3059. 「HNOI2019」序列
		Loj #3059. 「HNOI2019」序列 给定一个长度为 \(n\) 的序列 \(A_1, \ldots , A_n\),以及 \(m\) 个操作,每个操作将一个 \(A_i\) 修改为 \(k ... 
随机推荐
- linux服务器上没有jar命令
			在linux服务器上用jar命令解压jar包时,提示找不到jar命令. 但是用java -version查看jdk版本,又可以显示出jdk版本. echo $JAVA_HOME查看环境变量路径,找不到 ... 
- 移动端UI
			mui:http://dev.dcloud.net.cn/mui/ saltUI:https://salt-ui.github.io 
- Diabetic Retinopathy Winner's Interview: 1st place, Ben Graham
			Diabetic Retinopathy Winner's Interview: 1st place, Ben Graham Ben Graham finished at the top of the ... 
- CSS3 颜色模式
			HSL .test{background-color:hsl(<length>, <percentage>, <percentage>);} 1.length 表示 ... 
- .NET 定时器类及使用方法
			Timer类实现定时任务 //2秒后开启该线程,然后每隔4s调用一次 System.Threading.Timer timer = new System.Threading.Timer((n) =&g ... 
- 《大型网站SEO优化实践》学习分享
			本文主要内容源自2013年阿里技术嘉年华中阿里巴巴周文君分享<大型网站SEO优化实践>.学习过后,受益匪浅,特作笔记,经常回顾吸收学习. 大型网站SEO的特点&优势&挑战 ... 
- python核心编程笔记——Chapter7
			Chapter7.映像和集合类型 最近临到期末,真的被各种复习,各种大作业缠住,想想已经荒废了python的学习1个月了.现在失去了昔日对python的触觉和要写简洁优雅代码的感觉,所以临到期末毅然继 ... 
- WPF控件收集
			1.Extended WPF Toolkit 2.Fluent Ribbon Control Suite 3.WPF Ribbon Control 4.Telerik RadControls for ... 
- 20145234黄斐《Java程序设计》第七周
			教材学习内容总结 第十二章部分 - Lambda 认识Lambda语法 Lambda去可以重复,符合DRY原则,而且Lambda表达式可读性更好,操作更简单 匿名类型最大的问题就在于其冗余的语法,la ... 
- url添加时间戳
			http://blog.csdn.net/qq_36769100/article/details/54564784 URL后面添加随机数通常用于防止客户端(浏览器)缓存页面. 浏览器缓存是基于url进 ... 
