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树)的更多相关文章

  1. Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结

    Atitit 常见的树形结构 红黑树  二叉树   B树 B+树  Trie树 attilax理解与总结 1.1. 树形结构-- 一对多的关系1 1.2. 树的相关术语: 1 1.3. 常见的树形结构 ...

  2. 字典树(Trie树)的实现及应用

    >>字典树的概念 Trie树,又称字典树,单词查找树或者前缀树,是一种用于快速检索的多叉树结构,如英文字母的字典树是一个26叉树,数字的字典树是一个10叉树.与二叉查找树不同,Trie树的 ...

  3. [POJ] #1002# 487-3279 : 桶排序/字典树(Trie树)/快速排序

    一. 题目 487-3279 Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 274040   Accepted: 48891 ...

  4. 洛谷$P4585\ [FJOI2015]$火星商店问题 线段树+$trie$树

    正解:线段树+$trie$树 解题报告: 传送门$QwQ$ $umm$题目有点儿长我先写下题目大意趴$QwQ$,就说有$n$个初始均为空的集合和$m$次操作,每次操作为向某个集合内加入一个数$x$,或 ...

  5. luoguP6623 [省选联考 2020 A 卷] 树(trie树)

    luoguP6623 [省选联考 2020 A 卷] 树(trie树) Luogu 题外话: ...想不出来啥好说的了. 我认识的人基本都切这道题了. 就我只会10分暴力. 我是傻逼. 题解时间 先不 ...

  6. HDU 5790 Prefix(字典树+主席树)

    Prefix Time Limit: 2000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

  7. hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法

    Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...

  8. hdu 1251:统计难题[【trie树】||【map】

    <题目链接> 统计难题                        Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131 ...

  9. HDU 4825 Xor Sum (trie树处理异或)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)Total S ...

随机推荐

  1. Linux下安装redis的详细过程(redis版本为4.0.10)

    1.安装redis步骤 1.推荐进入到linux路径/usr/local/src 2.$ wget http://download.redis.io/releases/redis-4.0.10.tar ...

  2. 深入解读Promise对象

    promise对象初印象: promise对象是异步编程的一种解决方案,传统的方法有回调函数和事件,promise对象是一个容器,保存着未来才会结束的事件的结果 promise对象有两个特点: 1.p ...

  3. C# Note33: 总结C# 6.0/7.0 新特性

    先注明,本文主体参考自:C# 6.0新特性 目前代码中使用了很多C#6.0的新特性,下面以Point类来做相关叙述: public class Point { public int X { get; ...

  4. 转《基于Ionic3实现微信支付和支付宝支付》

    在Ionic应用里实现支付并不难,但是有的坑真是不爬不知道. 一:支付宝支付 网上关于支付宝支付cordova插件真是非常多,但是大多会报一些让你很无语的错误.比如sdk早已过时不是最新的,或者没有出 ...

  5. Python 基础知识----数据类型

    一.Number 类型(数值类型) 二.String 类型 (字符串类型) 三.List 类型 (列表类型) 是一种常用的序列类型簇,List 用中括号 [ ] 表示,不同的元素(任意类型的值)之间以 ...

  6. com.fasterxml.jackson.databind.exc.InvalidDefinitionException: No serializer found for class cn.edu.

    详细信息   https://www.cnblogs.com/xuwenjin/p/8832522.html 解决办法: 在实体类上面加上注解 @JsonIgnoreProperties(value ...

  7. php5.6.x到php7.0.x特性

    php5.6.x到php7.0.x特性 1.标量类型声明 字符串(string), 整数 (int), 浮点数 (float), 布尔值 (bool),callable,array,self,Clas ...

  8. python3高级编程

    1. SMTP发送邮件 internet相关协议: http:网页访问相关,httplib,urllib,xmlrpclib ftp:文件传输相关, ftplib, urllib nntp:新闻和帖子 ...

  9. 去掉AMD锐龙和Intel Kaby Lake的不支持的硬件的提示

    Windows 7和Windows 8.1都不支持AMD Ryzen 锐龙系列和Intel最新的Kaby Lake系列,Windows Update 更新之后总是会提示“不支持的硬件(Unsuppor ...

  10. Play framework框架中通过post方式发送请求

    搞了好久这个最终还是在play官方文档中看见的发送请求的方式,国内好像很少有使用这个框架的,加之自己不是太愿意宣传,好东西总归是好东西,不说废话了. 在play中发送请求有两种常用的方式,一种get, ...