CF1437G Death DBMS
题意简述:给出 \(n\) 个字符串 \(s_i\),每个 \(s_i\) 初始权值为 \(0\)。\(q\) 次操作:修改 \(s_i\) 的权值;查询给出字符串 \(q\) 能匹配的所有 \(s_i\) 的最大权值。
看到 “字符串匹配” 就应该想到 ACAM 了吧。根据 ACAM 的性质,所有后缀能匹配 \(s_i\) 的字符串都应该在 fail 树上以 \(end_i\) 为根的子树里(\(end_i\) 是 \(s_i\) 的结束位置)。那么对于 \(s_i\),将 \([dfn_{end_i},dfn_{end_i}+sz_{end_i}-1]\) 的所有位置都加入其权值,那么如果每个位置用 multiset 维护权值,再用线段树辅助修改(修改的时候直接跑到线段树上 \([l,r]\) 所分解成的所有区间,将原来的值从该点的 multiset 里面删除,再插入新的权值)和查询(将 \(q\) 跑到的每一个位置查询该位置上 multiset 的最大值,即在线段树上从根跑到该叶子节点所经过的所有区间的 multiset 最大值),就可以做到 \(\log^2\) 修改 \(\log\) 查询。实际上就是线段树套平衡树(set)。
时间复杂度 \(\mathcal{O}((m+n)\log^2 n)\)。
不用快读:TLE(2s+);用了快读:700ms。
/*
Powered by C++11.
Author : Alex_Wei.
*/
#pragma GCC optimize(3)
#include <bits/stdc++.h>
using namespace std;
namespace IO{
char buf[1<<21],*p1=buf,*p2=buf,obuf[1<<23],*O=obuf;
#ifdef __WIN32
#define gc getchar()
#else
#define gc (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<20,stdin),p1==p2)?EOF:*p1++)
#endif
#define pc(x) (*O++=x)
#define flush() fwrite(obuf,O-obuf,1,stdout)
inline int read(){
int x=0,sign=0; char s=gc;
while(!isdigit(s))sign|=s=='-',s=gc;
while(isdigit(s))x=(x<<1)+(x<<3)+(s-'0'),s=gc;
return sign?-x:x;
}
inline string reads(){
string t; char s=gc;
while(!isalpha(s))s=gc;
while(isalpha(s))t+=s,s=gc;
return t;
}
inline void print(int x){
if(x<0)return pc('-'),print(-x),void();
if(x>9)print(x/10);
pc(x%10+'0');
}
} using namespace IO;
const int N=3e5+5;
const int S=26;
int cnt,v[N],f[N],son[N][S],ed[N];
int dn,dfn[N],sz[N];
vector <int> e[N];
void ins(string s,int id){
int p=0;
for(char it:s){
if(!son[p][it-'a'])son[p][it-'a']=++cnt;
p=son[p][it-'a'];
} ed[id]=p;
} void build(){
queue <int> q;
for(int i=0;i<26;i++)if(son[0][i])q.push(son[0][i]);
while(!q.empty()){
int t=q.front(); q.pop();
for(int i=0;i<26;i++)
if(son[t][i])f[son[t][i]]=son[f[t]][i],q.push(son[t][i]);
else son[t][i]=son[f[t]][i];
e[f[t]].emplace_back(t);
}
} void dfs(int id){
dfn[id]=++dn,sz[id]=1;
for(int it:e[id])dfs(it),sz[id]+=sz[it];
}
multiset <int> val[N<<2];
void modify(int l,int r,int ql,int qr,int ori,int nw,int x){
if(ql<=l&&r<=qr){
auto it=val[x].find(ori);
if(it!=val[x].end())val[x].erase(it);
val[x].insert(nw);
return;
} int m=l+r>>1;
if(ql<=m)modify(l,m,ql,qr,ori,nw,x<<1);
if(m<qr)modify(m+1,r,ql,qr,ori,nw,x<<1|1);
} int query(int l,int r,int p,int x){
int ans=val[x].size()?*--val[x].end():-1;
if(l==r)return ans;
int m=l+r>>1;
if(p<=m)ans=max(ans,query(l,m,p,x<<1));
else ans=max(ans,query(m+1,r,p,x<<1|1));
return ans;
}
int n,q;
string s;
int main(){
cin>>n>>q;
for(int i=1;i<=n;i++)ins(reads(),i);
build(),dfs(0);
for(int i=1;i<=n;i++)modify(1,dn,dfn[ed[i]],dfn[ed[i]]+sz[ed[i]]-1,0,0,1);
for(int i=1;i<=q;i++){
int t=read();
if(t==1){
int p=read(),x=read();
modify(1,dn,dfn[ed[p]],dfn[ed[p]]+sz[ed[p]]-1,v[p],x,1),v[p]=x;
} else{
int p=0,ans=-1; s=reads();
for(char it:s){
p=son[p][it-'a'];
ans=max(ans,query(1,dn,dfn[p],1));
} print(ans),pc('\n');
}
}
return flush(),0;
}
CF1437G Death DBMS的更多相关文章
- 题解 CF1437G Death DBMS
这题感觉不是很难,但是既然放在 \(\texttt{EDU}\) 的 \(\texttt{G}\) 题,那么还是写写题解吧. \(\texttt{Solution}\) 首先看到 "子串&q ...
- ACAM 题乱做
之前做了不少 ACAM,不过没怎么整理起来,还是有点可惜的. 打 * 的是推荐一做的题目. I. *CF1437G Death DBMS 见 我的题解. II. *CF1202E You Are Gi ...
- Educational Codeforces Round 97 (Rated for Div. 2)
补了一场Edu round. A : Marketing Scheme 水题 #include <cstdio> #include <algorithm> typedef lo ...
- 【清北学堂】 死亡(death)
M个位置可以打sif,N+1个人等着打sif,已知前N个人的时间,问第N+1个人什么时候才能打sif(不能插队,即必须按顺序来打sif) 输入N,M以及每个人所需要的时间:输出第N+1个人所需的时间 ...
- HDU 5860 Death Sequence(死亡序列)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- Ping of Death
[Ping of Death] The ping of death attack, or PoD, can cripple a network based on a flaw in the TCP/I ...
- Teamcenter 2007 "由于某些意外的 DBMS 错误失败"
PLM系统实施过程中经常需要做整个系统的克隆备份.Teamcenter2007在做基于Oracel的系统克隆时,用exp功能导出数据表,部署在目标机器上时会遇到DBMS错误,导致后续DB写操作无法进行 ...
- 2016暑假多校联合---Death Sequence(递推、前向星)
原题链接 Problem Description You may heard of the Joseph Problem, the story comes from a Jewish historia ...
- 2015年第5本(英文第4本):Death on the Nile尼罗河上的惨案
书名:Death on the Nile 作者: Agatha Christie 单词数:7.9万(读完后发现网上还有一个版本,总共2.7万单词,孩子都能读懂,看来是简写版) 词汇量:6700 首万词 ...
随机推荐
- 第五课第四周实验一:Embedding_plus_Positional_encoding 嵌入向量加入位置编码
目录 变压器预处理 包 1 - 位置编码 1.1 - 位置编码可视化 1.2 - 比较位置编码 1.2.1 - 相关性 1.2.2 - 欧几里得距离 2 - 语义嵌入 2.1 - 加载预训练嵌入 2. ...
- BUAAOO第四单元总结
---恢复内容开始--- 一.本单元两次作业的架构设计 第十三次作业:本次作业我创建了四个类,除去官方提供的Main和MyUmlInteraction类之外,还有Uclass和Ulinterface分 ...
- STM32 学习笔记之中断应用概览--以f103为例
异常类型 F103 在内核水平上搭载了一个异常响应系统, 支持为数众多的系统异常和外部中断.其中系统异常有8 个(如果把Reset 和HardFault 也算上的话就是10 个),外部中断有60个.除 ...
- 便宜的回文串(区间DP)
题目链接:便宜的回文串 这道题刚开始其实还是没有思路的.没办法,只能看题解了... 其实我们在思考问题时,考虑到一段串增或减时会改变它的长度,所以转移时会麻烦... 但其实不用考虑那么多的问题,我们只 ...
- Linux的inode与block
1,inode包含文件的元信息,具体来说有以下内容: 文件的字节数 文件拥有者的User ID 文件的Group ID 文件的读.写.执行权限 文件的时间戳,共有三个:ctime指inode上次文件属 ...
- Matlab 中 arburg 函数的理解与实际使用方法
1. 理解 1.1 Matlab 帮助: a = arburg(x,p)返回与输入数组x的p阶模型相对应的归一化自回归(AR)参数. 如果x是一个向量,则输出数组a是一个行向量. 如果x是矩阵,则参数 ...
- JavaScript 事件循环
JavaScript 事件循环 事件循环 任务队列 async/await 又是如何处理的呢 ? 定时器问题 阻塞还是非阻塞 实际应用案例 拆分 CPU 过载任务 进度指示 在事件之后做一些事情 事件 ...
- JavaScript数组方法大集合
JavaScript数组方法集合 本文总结一下js数组处理用到的所有的方法.自己做个笔记. 数组方法 concat() 合并两个或多个数组 concat()能合并两个或者多个数组,不会更改当前数组,而 ...
- SpringCloud升级之路2020.0.x版-31. FeignClient 实现断路器以及线程隔离限流的思路
本系列代码地址:https://github.com/JoJoTec/spring-cloud-parent 在前面一节,我们实现了 FeignClient 粘合 resilience4j 的 Ret ...
- c++学习笔记(九)
引用(reference) 概念 引用变量是一个别名,也就是说,它是某个已存在变量的另一个名字. 一旦把引用初始化为某个变量,就可以使用该引用名称或变量名称来指向变量. 用法 变量名称是变量附属在内存 ...