【HDU - 5790 】Prefix(主席树+Trie树)
BUPT2017 wintertraining(15) #7C
题意
求[min((Z+L)%N,(Z+R)%N)+1,max((Z+L)%N,(Z+R)%N)+1]中不同前缀的个数,Z是上次询问的结果,N是字符串总个数。Q次询问。
题解
用主席树,即函数式线段树,维护前i个字符串的区间和(每个区间的前缀个数之和)。
读入每个字符串后,用Trie树给它的每个前缀分配ID,并记录每个前缀最后出现的位置pre[cur],如果当前的前缀出现过,则线段树中上一次出现的位置的值-1,相当于只把这种前缀记录在最后出现的位置上。然后当前位置(第几个字符串)贡献了字符串长度个前缀。
那么求[L,R]就相当于求前R个字符串对应的线段树的区间[L,n]的值,因为这样肯定不会包含只在R后面出现的前缀,而前R个字符串对应的线段树(主席树就相当于n个线段树),每个前缀只在最后出现的位置上有贡献,于是它[L,n]一定包含所有这个区间出现的前缀。
代码
#include <cstdio>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
const int N=100005;
const int M=N*40;
using namespace std;
namespace PST{
int T[N],lson[M],rson[M],tot,c[M],n;
void init(int _n){
tot=0;
n=_n;
memset(c,0,sizeof c);
}
int build(int l,int r){
++tot;
c[tot]=0;
if(l<r){
int mid=l+r>>1;
lson[tot]=build(l,mid);
rson[tot]=build(mid+1,r);
}
return tot;
}
int update(int root,int pos,int val){
int newroot=++tot, tmp=tot;
c[tmp]=c[root]+val;
int l=1,r=n;
while(l<r){
int mid=l+r>>1;
if(pos<=mid){
lson[newroot]=++tot; rson[newroot]=rson[root];
root=lson[root];
r=mid;
}
else{
rson[newroot]=++tot; lson[newroot]=lson[root];
root=rson[root];
l=mid+1;
}
newroot = tot;
c[newroot] = c[root] + val;
}
return tmp;
}
int query(int root,int pos){
int ret=0;
int l=1,r=n;
while(l<pos){
int mid=l+r>>1;
if(pos<=mid){
ret+=c[rson[root]];
r=mid;
root=lson[root];
}
else{
l=mid+1;
root=rson[root];
}
}
return c[root]+ret;
}
}
namespace Trie{
int node[N][27];
int tot, pre[N];
void Insert(string& s,int x){
int cur=0;
for(int i=0;i<s.size();++i){
int p=s[i]-'a';
if(node[cur][p]==0)
node[cur][p]=++tot;
cur=node[cur][p];
if(pre[cur]){
PST::T[x]=PST::update(PST::T[x], pre[cur], -1);
}
pre[cur]=x+1;
}
}
void init(){
tot=0;
memset(node,0,sizeof node);
memset(pre,0,sizeof pre);
}
}
string s;
int main(){
int n;
while(~scanf("%d",&n)){
int z=0;
Trie::init();
PST::init(n);
PST::T[0]=PST::build(1,n);
for(int i=0;i<n;++i){
if(i)PST::T[i]=PST::T[i-1];
cin>>s;
Trie::Insert(s, i);
PST::T[i]=PST::update(PST::T[i], i+1, s.size());
}
int q;
scanf("%d",&q);
while(q--){
int l,r;
scanf("%d%d",&l,&r);
l=(l+z)%n,r=(r+z)%n;
if(r<l)swap(l,r);
z=PST::query(PST::T[r],l+1);
printf("%d\n",z);
}
}
return 0;
}
【HDU - 5790 】Prefix(主席树+Trie树)的更多相关文章
- Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结
Atitit 常见的树形结构 红黑树 二叉树 B树 B+树 Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...
- 字典树(Trie树)的实现及应用
>>字典树的概念 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.与二叉查找树不同,Trie树的 ...
- [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序
一. 题目 487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 274040 Accepted: 48891 ...
- 洛谷$P4585\ [FJOI2015]$火星商店问题 线段树+$trie$树
正解:线段树+$trie$树 解题报告: 传送门$QwQ$ $umm$题目有点儿长我先写下题目大意趴$QwQ$,就说有$n$个初始均为空的集合和$m$次操作,每次操作为向某个集合内加入一个数$x$,或 ...
- luoguP6623 [省选联考 2020 A 卷] 树(trie树)
luoguP6623 [省选联考 2020 A 卷] 树(trie树) Luogu 题外话: ...想不出来啥好说的了. 我认识的人基本都切这道题了. 就我只会10分暴力. 我是傻逼. 题解时间 先不 ...
- HDU 5790 Prefix(字典树+主席树)
Prefix Time Limit: 2000/4000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Sub ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
- hdu 1251:统计难题[【trie树】||【map】
<题目链接> 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131 ...
- HDU 4825 Xor Sum (trie树处理异或)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)Total S ...
随机推荐
- Node.js api接口和SQL数据库关联
数据库表创建 服务器环境配置.连接 .操作.数据库 API接口 原则:
- webpack之loader和plugin简介
webpack之loader和plugin简介 webpack入门和实战(二):全面理解和运用loader和plugins webpack入门(四)——webpack loader 和plugin w ...
- 【学习总结】Git学习-参考廖雪峰老师教程八-使用GitHub
学习总结之Git学习-总 目录: 一.Git简介 二.安装Git 三.创建版本库 四.时光机穿梭 五.远程仓库 六.分支管理 七.标签管理 八.使用GitHub 九.使用码云 十.自定义Git 期末总 ...
- IdentityServer4【Introduction】之包和项目构建
包和项目构建 IdentityServer包含了以下的nuget包: IdentityServer4 nuget | github 这个包包含了IdentityServer核心的组成部分,有对象模型, ...
- findBugs安装
点击“Help->InstallNew Software”,如下图所示: 2 接着如下图所示: 3 Name”输入“findBugs”,“Location”输入“http://findbugs. ...
- [转帖]csdn windows 下载整理.
特别说明:本帖不提供任何密钥或激活方法,请大家也不要在帖内回复或讨论涉及版权的相关内容,仅提供原版ISO下载链接 https://bbs.csdn.net/topics/391111024?list= ...
- 【学亮IT手记】利用字节流复制文件
- mybatis两种开发方式
本文首先讲解从JDBC到mybatis的演变过程,然后是使用mybatis进行开发的两种方式. 一 JDBC的使用及其优化 1.使用JDBC进行数据库操作 加载JDBC驱动: 建立并获取数据库连接: ...
- Django的模板层
一 模版简介 你可能已经注意到我们在例子视图中返回文本的方式有点特别. 也就是说,HTML被直接硬编码在 Python代码之中. def current_datetime(request): now ...
- Log4j2配置与使用
依赖包: <!-- https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-api --> <depend ...