设置 \(\sqrt{n}\) 个关键点,维护出关键点到每个右端点之间的答案以及Pam的左指针,每次暴力向左插入元素即可,为了去重,还需要记录一下Pam上每个节点在每个关键点为左端点插入到时候到最左边出现位置,总复杂度 \(O(n\sqrt{n})\)。

/*program by mangoyang*/
#pragma GCC optimize("Ofast", "inline")
#include<bits/stdc++.h>
#define inf ((int)(1e9))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
typedef long long ll;
using namespace std;
template <class T>
inline void read(T &x){
int f = 0, ch = 0; x = 0;
for(; !isdigit(ch); ch = getchar()) if(ch == '-') f = 1;
for(; isdigit(ch); ch = getchar()) x = x * 10 + ch - 48;
if(f) x = -x;
}
const int N = 100005;
char s[N];
namespace PAM{
int fa[N], ch[N][26], trans[N][26], len[N], size, tail, head;
inline void init(){
fa[0] = 1, len[1] = -1, tail = head = size = 1;
for(int i = 0; i < 26; i++) trans[0][i] = 1;
}
inline int newnode(int x){ return len[++size] = x, size; }
inline void pushback(int l, int r){
int c = s[r] - 'a', p = tail;
while(r - len[p] - 1 < l || s[r-len[p]-1] != s[r]) p = fa[p];
if(!ch[p][c]){
int np = newnode(len[p] + 2); fa[np] = ch[trans[p][c]][c];
memcpy(trans[np], trans[fa[np]], sizeof(trans[np]));
trans[np][s[r-len[fa[np]]]-'a'] = fa[np], ch[p][c] = np;
}
tail = ch[p][c];
if(len[tail] == r - l + 1) head = tail;
}
inline void pushfront(int l, int r){
int c = s[l] - 'a', p = head;
while(l + len[p] + 1 > r || s[l+len[p]+1] != s[l]) p = fa[p];
if(!ch[p][c]){
int np = newnode(len[p] + 2); fa[np] = ch[trans[p][c]][c];
memcpy(trans[np], trans[fa[np]], sizeof(trans[np]));
trans[np][s[l+len[fa[np]]]-'a'] = fa[np], ch[p][c] = np;
}
head = ch[p][c];
if(len[head] == r - l + 1) tail = head;
}
} int bel[N], pos[700][N], pre[700][N], ans[700][N], ti[N], n, type, Q, Ans, tim; int main(){
read(type), read(n), read(Q);
scanf("%s", s + 1);
int S = (int) min(n, 150), block = (n / S) + (n % S > 0);
PAM::init();
for(int i = 1; i <= n; i++) bel[i] = (i - 1) / S + 1;
for(int i = 1; i <= block; i++){
PAM::tail = PAM::head = 1, ++tim;
for(int j = (i - 1) * S + 1; j <= n; j++){
PAM::pushback((i - 1) * S + 1, j);
if(ti[PAM::tail] != tim) {
ti[PAM::tail] = tim, pos[i][PAM::tail] = j, ans[i][j]++;
}
ans[i][j] += ans[i][j-1], pre[i][j] = PAM::head;
}
}
while(Q--){
int l, r; read(l), read(r);
l ^= Ans * type, r ^= Ans * type, ++tim;
if(bel[l] == bel[r]){
PAM::head = PAM::tail = 1, Ans = 0;
for(int i = l; i <= r; i++){
PAM::pushback(l, i);
if(ti[PAM::tail] != tim) ti[PAM::tail] = tim, Ans++;
}
printf("%d\n", Ans); continue;
}
int c = bel[l] + 1;
PAM::head = pre[c][r], Ans = ans[c][r];
for(int i = (c - 1) * S; i >= l; i--){
PAM::pushfront(i, r);
if(ti[PAM::head] != tim){
ti[PAM::head] = tim;
if(!pos[c][PAM::head] || pos[c][PAM::head] > r) Ans++;
}
}
printf("%d\n", Ans);
}
return 0;
}

「2017 山东一轮集训 Day4」基因的更多相关文章

  1. Loj #6069. 「2017 山东一轮集训 Day4」塔

    Loj #6069. 「2017 山东一轮集训 Day4」塔 题目描述 现在有一条 $ [1, l] $ 的数轴,要在上面造 $ n $ 座塔,每座塔的坐标要两两不同,且为整点. 塔有编号,且每座塔都 ...

  2. Loj 6068. 「2017 山东一轮集训 Day4」棋盘

    Loj 6068. 「2017 山东一轮集训 Day4」棋盘 题目描述 给定一个 $ n \times n $ 的棋盘,棋盘上每个位置要么为空要么为障碍.定义棋盘上两个位置 $ (x, y),(u, ...

  3. loj6068. 「2017 山东一轮集训 Day4」棋盘 二分图,网络流

    loj6068. 「2017 山东一轮集训 Day4」棋盘 链接 https://loj.ac/problem/6068 思路 上来没头绪,后来套算法,套了个网络流 经典二分图 左边横,右边列 先重新 ...

  4. 「2017 山东一轮集训 Day4」棋盘(费用流)

    棋盘模型 + 动态加边 #include<cstdio> #include<algorithm> #include<iostream> #include<cs ...

  5. [LOJ#6068]. 「2017 山东一轮集训 Day4」棋盘[费用流]

    题意 题目链接 分析 考虑每个棋子对对应的横向纵向的极大区间的影响:记之前这个区间中的点数为 \(x\) ,那么此次多配对的数量即 \(x\) . 考虑费用流,\(S\rightarrow 横向区间 ...

  6. LOJ 6068「2017 山东一轮集训 Day4」棋盘

    题意 一个 \(n\times n\) 的棋盘上面有若干障碍物. 定义两个棋子可以互相攻击当且仅当这两个棋子的横坐标或纵坐标相等而且中间不能隔着障碍物.(可以隔棋子) 有 \(q\) 次询问,每次询问 ...

  7. Loj #6073.「2017 山东一轮集训 Day5」距离

    Loj #6073.「2017 山东一轮集训 Day5」距离 Description 给定一棵 \(n\) 个点的边带权的树,以及一个排列$ p\(,有\)q $个询问,给定点 \(u, v, k\) ...

  8. 「2017 山东一轮集训 Day5」苹果树

    「2017 山东一轮集训 Day5」苹果树 \(n\leq 40\) 折半搜索+矩阵树定理. 没有想到折半搜索. 首先我们先枚举\(k\)个好点,我们让它们一定没有用的.要满足这个条件就要使它只能和坏 ...

  9. 【LOJ#6066】「2017 山东一轮集训 Day3」第二题(哈希,二分)

    [LOJ#6066]「2017 山东一轮集训 Day3」第二题(哈希,二分) 题面 LOJ 题解 要哈希是很显然的,那么就考虑哈希什么... 要找一个东西可以表示一棵树,所以我们找到了括号序列. 那么 ...

随机推荐

  1. ASP.NET导入EXCEL方法汇总

    1.由dataset生成 public void CreateExcel(DataSet ds,string typeid,string FileName) { HttpResponse resp; ...

  2. 移动端meta声明

    <!DOCTYPE html> <!-- 使用 HTML5 doctype,不区分大小写 --><html lang="zh-cmn-Hans"> ...

  3. 避免无用的渲染绘制(Avoiding Unnecessary Paints)

    本文翻译自html5rock上的文章,文章英文原版地址在最后给出. 文中的Paints我翻译成渲染绘制,我自己是这么理解. 开始 绘制(渲染)一个网站或者一个应用的元素对浏览器来说开销是很大的,它会对 ...

  4. perf + 火焰图分析程序性能

    1.perf命令简要介绍 性能调优时,我们通常需要分析查找到程序百分比高的热点代码片段,这便需要使用 perf record 记录单个函数级别的统计信息,并使用 perf report 来显示统计结果 ...

  5. java中的matches -> 完全匹配

    matches是完全匹配.跟matcher不一样, matcher像perl正则, 能匹配到符合的都会返回true, 而这个matches要完全一模一样才行. import java.util.reg ...

  6. MAC和PHY的区别 (转自http://www.cnblogs.com/feitian629/archive/2013/01/25/2876857.html)

    一块以太网网卡包括OSI(开方系统互联)模型的两个层.物理层和数据链路层.物理层定义了数据传送与接收所需要的电与光信号.线路状态.时钟基准.数据编码和电路等,并向数据链路层设备提供标准接口.数据链路层 ...

  7. openstack环境下的虚拟机通过浮动IP访问后能ping通外网IP不能ping通域名

    1.环境简介 openstack环境下构造Ubuntu系统的VM,VM配置受管子网和自管子网,同时绑定浮动IP 2.通过浮动IP访问VM后,ping www.baidu.com失败,但是通过IP地址p ...

  8. POJ 2752 Seek the Name, Seek the Fame(KMP求公共前后缀)

    题目链接:http://poj.org/problem?id=2752 题目大意:给你一串字符串s找到所有的公共前后缀,即既是前缀又是后缀的子串. 解题思路: 如图所示 假设字符串pi与jq为符合条件 ...

  9. 读书笔记|Windows 调试原理学习|持续更新

    关于调试方面的学习笔记,主要来源于<软件调试>的读书笔记和梦织未来论坛的视频教程 1.调试器使用一个死循环监听调试信息. DebugActiveProcess(PID);while(TRU ...

  10. jmeter-----如何安装插件管理?

    1.下载插件管理jar文件,http://www.jmeter-plugins.org/wiki/PluginsManager/ 2. 拷贝这jar文件到 \lib\ext文件夹里 3. 重新打开JM ...