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. 网络编程-C/S架构

    什么是C/S架构 C指的是client(客户端软件),S指的是Server(服务端软件),本章的重点就是教大家写一个C/S架构的软件,实现服务端软件与客户端软件基于网络通信. 计算机基础知识 作为应用 ...

  2. C++ string中的find()函数

    1.string中find()返回值是字母在母串中的位置(下标记录),如果没有找到,那么会返回一个特别的标记npos.(返回值可以看成是一个int型的数) #include<cstring> ...

  3. python 中的super()继承,搜索广度为先

    一.python中类的继承 1.1 单继承 在python 中我们可以这样来定义一个类:及继承它的子类 class Father: def __init__(self, mes): #1 父类的ini ...

  4. js总结:JavaScript入门

    1. JavaScript-输出内容(document.write) 2.在html中引用js文件 3.定义变量 语法:var 变量名 说明: ⑴变量必须使用字母.下划线(_)或者美元符($)开始. ...

  5. asp.net mvc area实现多级controller和多级view

    经常需要描述这样的项目结构 ~:. //web根目录├─.admin   //管理员功能目录│  └─index.html    //管理员目录页面├─.user                  / ...

  6. 设置永久环境变量linux

    ========================================================================== http://www.cnblogs.com/Bi ...

  7. MySQL的SQL语句优化-group by语句的优化

    原文:http://bbs.landingbj.com/t-0-243202-1.html 默认情况下,MySQL排序所有GROUP BY col1, col2, ....,查询的方法如同在查询中指定 ...

  8. css3特殊图形(气泡)

    一.气泡 效果: body{ background: #dd5e9d; height: 100%; } .paopao { position: absolute; width: 200px; heig ...

  9. MyBatis映射文件6

    之前说了由Employee找Department,这一节讲一讲由Department找Employee,显然前者是多对一的关系,而后者是一对多的关系. Department的JavaBean: pri ...

  10. Centos6.8 安装nginx

    1.安装相关依赖 (1)yum install gcc 备注:可以通过gcc -v 查看版本信息,来确定是否安装过. (2)yum install pcre-devel (3)yum install ...