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 首万词 ...
随机推荐
- 【Linux命令063】Linux非常简单常用的入门命令
Linux常用命令 这是一篇我在公众号上发布的文章,还算较为受欢迎. 博客园这边荒废好长时间了,主要是最近一年经常撰写的文章都是Linux相关的入门文章. 不知道是否能通过博客园的首页审核. 1.cd ...
- 软件工程个人博客作业-软件案例分析:VS与VS Code
项目 内容 本作业属于北航 2020 年春软件工程 博客园班级连接 本作业是本课程个人项目作业 作业要求 我在这个课程的目标是 提高软件开发能力.团队协作能力 这个作业在哪个具体方面帮助我实现目标 提 ...
- 计算机网络之传输层(传输层提供的服务及功能概述、端口、套接字--Socket、无连接UDP和面向连接TCP服务)
文章转自:https://blog.csdn.net/weixin_43914604/article/details/105451022 学习课程:<2019王道考研计算机网络> 学习目的 ...
- SpringCloud微服务实战——搭建企业级开发框架(九):使用Nacos发现、配置和管理微服务
Nacos是一个更易于构建云原生应用的动态服务发现.配置管理和服务管理平台,Nacos 致力于帮助您发现.配置和管理微服务.Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现.服务配置 ...
- SQLServer聚集索引导致的插入性能低
1,新表默认会在主键上建立聚集索引.对于非专业DBA, 默认配置在大多数情况下够用. 2,当初为了优化查询速度. 把聚集索引建立在非自增主键的唯一索引列. 数据量上千万后,插入性能开始显现不足. 随着 ...
- 测试开发【提测平台】分享13-远程搜索和路由$route使用实现新建提测需求
微信搜索[大奇测试开],关注这个坚持分享测试开发干货的家伙. 本篇继续提测平台开发,按惯例先给出学习的思维导图,以便快速了解学习知识和平台功能实现的重点. 基本知识点学习 远程搜索 显示的数据通过输入 ...
- 从零开始,无DNS vcenter 6.7 vmotion热迁移,存储集群部署文档。
1,环境准备 准备:Vmware workstation环境 IP地址段规划 ESXI主机IP地址段 192.168.197.4-192.168.197.10 Vcenter Server集群IP地址 ...
- nohup java -jar xx.jar & ,关闭窗口后退出进程
nohup java -jar dw-report..jar > dw-report.log & 自动退出命令在后台运行 xx.jar程序 明明已经加了"&" ...
- LeetCode 199. 二叉树的右视图 C++ 用时超100%
/** * Definition for a binary tree node. * struct TreeNode { * int val; * TreeNode *left; * TreeNode ...
- [python]django的mode设置表结构和serializers序列化数据
框架使用的库版本 python3.6.5 Django-2.0.6 djangorestframework-3.8.2 mysqlclient-1.3.12 1.项目结构声明,数据库在setting. ...