前言

来个不一样的做法,用到了 Trie 树和主席树,并且是可爱的在线算法。

题目链接:洛谷

题目分析

对于一个查询 \(\texttt{ip}\),只考虑所有前缀字符串规则。以时间建里横轴,匹配长度为纵轴,建出坐标系。易知坐标系中有 \(\Theta(n)\) 条线段。对于某一时刻 \(t\),根据题意,生效的匹配就是越过 \(t\) 的纵坐标最大的那条线段对应的前缀。用样例画张图吧。

红色的线代表每一个时刻生效的匹配,绿色框内就是发生变化的时刻,分别是第 \(3\) 次操作后,第 \(4\) 次操作后,第 \(5\) 次操作后。

考虑使用 01-Trie 解决前缀。从根节点往下一直到匹配不上,每次将这个前缀出现的所有时段赋值为这个前缀的长度,就得到了红色的折线。最后查询 \([l, r]\) 内红色的线的纵坐标变化了多少次。

这个非常线段树啊,区间覆盖,合并信息也很容易,或者用珂朵莉树也可以。但是,直接这样做时间上是错误的,为什么?因为枚举这个前缀出现的所有时段是 \(\Theta(n)\) 的,可以轻松卡到 \(\Theta(nq \log n)\)。但是,原数据太水了,可以见这个帖子

那么,做法呼之欲出了,预处理的时候把线段树可持久化就行了。但是要注意内存回收,注意结构体内存对齐,或者使用 #pragma pack(1),空间有些紧的。

这个算法时间复杂度是 \(\Theta((n + q)(w + \log n))\),空间复杂度是 \(\Theta(n (w + \log n))\)。

代码

略去了快读。

// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast", "inline", "-ffast-math")
// #pragma GCC target("avx", "sse2", "sse3", "sse4", "mmx")
#include <iostream>
#include <cstdio>
#define debug(a) cerr << "Line: " << __LINE__ << " " << #a << endl
#define print(a) cerr << #a << "=" << (a) << endl
#define file(a) freopen(#a".in", "r", stdin), freopen(#a".out", "w", stdout)
#define main Main(); signed main(){ return ios::sync_with_stdio(0), cin.tie(0), Main(); } signed Main
using namespace std; #include <vector>
#include <bitset> int n, q; struct node{
int son[2];
vector<pair<int, int> > tim;
} tree[100010 * 20];
int tot; void insert(char str[], int timer){
int now = 0;
for (int i = 0; str[i]; ++i){
int t = str[i] - '0';
if (!tree[now].son[t]) tree[now].son[t] = ++tot;
now = tree[now].son[t];
}
tree[now].tim.push_back({timer, n});
} void erase(char str[], int timer){
int now = 0;
for (int i = 0; str[i]; ++i){
int t = str[i] - '0';
now = tree[now].son[t];
}
int s = tree[now].tim.back().first;
tree[now].tim.pop_back();
tree[now].tim.push_back({s, timer});
} struct President_Segment_Tree{
struct node{
int lson, rson;
int val, lval, rval;
};
static node tree[200010 * 80];
static int tot;
static bitset<200010 * 80> tag; static inline void init(){
tot = 0;
} static inline int newNode(){
int res = ++tot;
tree[res] = {0, 0, 0, 0, 0};
return res;
} static inline int copyNode(int idx){
int res = newNode();
return tree[res] = tree[idx], tag[res] = tag[idx], res;
} static inline void pushup(int idx){
tree[idx].lval = tree[tree[idx].lson].lval;
tree[idx].rval = tree[tree[idx].rson].rval;
tree[idx].val = tree[tree[idx].lson].val + tree[tree[idx].rson].val + (tree[tree[idx].lson].rval != tree[tree[idx].rson].lval);
} static inline void pushtag(int idx, int v){
tag.set(idx);
tree[idx].lval = tree[idx].rval = v;
tree[idx].val = 0;
} static inline void pushdown(int idx){
if (!tag[idx]) return;
pushtag(tree[idx].lson = copyNode(tree[idx].lson), tree[idx].lval);
pushtag(tree[idx].rson = copyNode(tree[idx].rson), tree[idx].lval);
tag.reset(idx);
} int root[100010 * 20]; void build(int &idx, int l, int r){
idx = newNode();
if (l == r) return;
int mid = (l + r) >> 1;
build(tree[idx].lson, l, mid);
build(tree[idx].rson, mid + 1, r);
pushup(idx);
} void modify(int &idx, int trl, int trr, int l, int r, int val){
if (trl > r || trr < l) return;
idx = copyNode(idx);
if (l <= trl && trr <= r) return pushtag(idx, val);
pushdown(idx);
int mid = (trl + trr) >> 1;
modify(tree[idx].lson, trl, mid, l, r, val);
modify(tree[idx].rson, mid + 1, trr, l, r, val);
pushup(idx);
} struct Q{ int val, lval, rval; }; Q add(const Q & a, const Q & b){
if (a.val == -1) return b;
if (b.val == -1) return a;
return {
a.val + b.val + (a.rval != b.lval),
a.lval, b.rval
};
} Q query(int idx, int trl, int trr, int l, int r){
if (trl > r || trr < l) return {-1, 0, 0};
if (l <= trl && trr <= r) return {tree[idx].val, tree[idx].lval, tree[idx].rval};
pushdown(idx);
int mid = (trl + trr) >> 1;
return add(query(tree[idx].lson, trl, mid, l, r), query(tree[idx].rson, mid + 1, trr, l, r));
}
} yzh; President_Segment_Tree::node President_Segment_Tree::tree[200010 * 80];
int President_Segment_Tree::tot;
bitset<200010 * 80> President_Segment_Tree::tag; void dfs(int now, int dpt = 0){
for (const auto & [s, e] : tree[now].tim){
yzh.modify(yzh.root[now], 1, n, s, e, dpt);
}
tree[now].tim.clear();
tree[now].tim.shrink_to_fit();
if (tree[now].son[0]){
yzh.root[tree[now].son[0]] = yzh.root[now];
dfs(tree[now].son[0], dpt + 1);
}
if (tree[now].son[1]){
yzh.root[tree[now].son[1]] = yzh.root[now];
dfs(tree[now].son[1], dpt + 1);
}
} signed main(){
read(n, q);
for (int i = 1; i <= n; ++i){
static char op[5], str[50];
read(op, str);
if (*op == 'A') insert(str, i);
else erase(str, i - 1);
}
dfs(0);
for (int i = 1, l, r; i <= q; ++i){
static char str[50];
read(str, l, r);
int now = 0;
for (int j = 0; str[j]; ++j){
int t = str[j] - '0';
if (!tree[now].son[t]) break;
now = tree[now].son[t];
}
write(yzh.query(yzh.root[now], 1, n, l, r).val, '\n');
}
return 0;
}

[BJOI2016] IP地址 题解的更多相关文章

  1. BJOI2016 IP地址

    题目链接 Description 给定 \(n\) 个 \(01\) 模式串.\(q\) 次询问: 每次询问给定一个 \(01\) 串: 设给这个串匹配的串是在模式串中存在的他的最长前缀 问 \([a ...

  2. LeetCode:复原IP地址【93】

    LeetCode:复原IP地址[93] 题目描述 给定一个只包含数字的字符串,复原它并返回所有可能的 IP 地址格式. 示例: 输入: "25525511135" 输出: [&qu ...

  3. leetcode-解题记录 1108. IP 地址无效化

    题目: 给你一个有效的 IPv4 地址 address,返回这个 IP 地址的无效化版本. 所谓无效化 IP 地址,其实就是用 "[.]" 代替了每个 ".". ...

  4. CentOS:ECDSA host key "ip地址" for has changed and you have requested strict checking(转)

    原文地址:http://blog.csdn.net/ausboyue/article/details/52775281 Linux SSH命令错误:ECDSA host key "ip地址& ...

  5. 烂泥:VMWare Workation双网卡配置IP地址

    本文由ilanniweb提供友情赞助,首发于烂泥行天下 想要获得更多的文章,可以关注我的微信ilanniweb 前几天给一个客户做远程项目实施,客户那边的服务器是Windows OS的,我们这边的业务 ...

  6. xamarin android,UWP 网络类型和IP地址

    App开发经常要判断网络连通情况,并判断网络类型,获取网络IP.xamarin中可以使用Dependencies提供各平台下的方法,现把各平台代码记录如下: using System; using S ...

  7. C#服务器获取客户端IP地址以及归属地探秘

    背景:博主本是一位Windows桌面应用程序开发工程师,对网络通信一知半解.一日老婆逛完某宝,问:"为什么他们知道我的地址呢,他们是怎么获取我的地址的呢?" 顺着这个问题我们的探秘 ...

  8. windows下获取IP地址的两种方法

    windows下获取IP地址的两种方法: 一种可以获取IPv4和IPv6,但是需要WSAStartup: 一种只能取到IPv4,但是不需要WSAStartup: 如下: 方法一:(可以获取IPv4和I ...

  9. 【Win 10 应用开发】获取本机的IP地址

    按照老规矩,也是朋友的建议,老周今天在吹牛之前,先讲一个小故事. 有朋友问我,老周,你现在还发短信吗,你每个月用多少电话费?唉,实话说,现在真的发短信不多了,套餐送的130条短信,每月都发不了一条.至 ...

  10. 计算机网络学习笔记--网络层之IP地址与子网

    IPv4地址: 我们知道在网络层(TCP/IP体系结构的网际互联层),最重要的一个协议就是IP协议,现在正处于IPv4和IPv6的过渡时期,但目前来说,IPv4仍为主流,所以主要讲Ipv4. IP地址 ...

随机推荐

  1. 2. Elasticsearch 使用插件和kibana操作

    引言 在上一篇文章中1. Elasticsearch 入门安装与部署 已经教了大家如何在linux系统中安装和启动Elasticsearch,本文就带大家一起学习如何操作 Elasticsearch. ...

  2. 状态模式(Sate Pattern)

    一.模式动机 状态模式(State Pattern)是一种较为复杂的行为型模式.它用于解决系统中复杂对象的状态转换以及不同状态下行为的封装问题.当系统中某个对象存在多个状态,这些状态之间可以进行转换, ...

  3. ENSP的VirtualBox虚拟网卡不能用

    VirtualBox 安装好后本地会新建一个名为 "VirtualBox Host-Only Network" 的虚拟网卡,虚拟机可以通过这个虚拟网卡网卡和物理机通信.但ENSP有 ...

  4. 动环监控方案,为什么推荐79元全志T113-i国产平台?

    什么是动环监控系统? 通信电源及机房环境监控系统(简称"动环监控系统"),是对分布在各机房的电源柜.UPS.空调.蓄电池等多种动力设备,及门磁.红外.窗破.水浸.温湿度.烟感等机房 ...

  5. 深度学习领域的名词解释:SOTA、端到端模型、泛化、RLHF、涌现 ..

    SOTA (State-of-the-Art) 在深度学习领域,SOTA指的是"当前最高技术水平"或"最佳实践".它用来形容在特定任务或领域中性能最优的模型或方 ...

  6. [UG 二次开发 python] 生成略缩图并保存

    保存到零件同名的文件夹下,名称相同,类型是 jpg 用到 numpy,PIL,cv2 blockstyler 文件略 # nx: threaded __version__ = "0.0.1& ...

  7. manage.py“Couldn't import Django”报错的问题解决

    问题分析: 在pyharm中项目可以正常运行但是在终端 终端输入python manage.py runserver首次测试项目时,出现了无法引用Django的错误. Traceback (most ...

  8. C#计算两个日期的天数

    private int DateDiff(DateTime dateStart, DateTime dateEnd) { DateTime start = Convert.ToDateTime(dat ...

  9. redis基本数据结构-列表

    redis基本数据结构-列表list 特性 每个列表键最多存储 2^32 - 1个字符串元素 元素在列表中有序 元素在列表中不唯一 向列表左侧添加元素 lpush key value lpush nu ...

  10. CF452C 题解

    洛谷链接&CF 链接 题目简述 有 \(m \times n\) 张牌,有 \(n\) 个种类,每个种类有 \(m\) 张,现在抽一张放回,再抽一张,求这张牌与第一张抽出的牌种类相同的概率. ...