当初竟然看成子串了$qwq$,不过老师的$ppt$也错了$qwq$


由于子序列一定是的排列,所以考虑插入$1$到$m$到$n-m+1$到$n$;

如何判断呢?可以用哈希$qwq$;

我们用线段树维护哈希值,合并时用就把左子树的哈希值$x[ls]$在$B$进制下左移$sum[rs]$位,即$x[tr]=x[ls]*p[sum[rs]]+x[rs]$;

此时就可以向上更新哈希值。

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<vector>
#include<queue>
#include<map>
#include<set>
#define ull unsigned long long
#define ll long long
#define R register int
#define md ((l+r)>>1)
#define ls (tr<<1)
#define rs (tr<<1|1)
#define pause (for(R i=1;i<=10000000000;++i))
#define OUT freopen("out.out","w",stdout)
using namespace std;
namespace Fread {
static char B[<<],*S=B,*D=B;
#define getchar() (S==D&&(D=(S=B)+fread(B,1,1<<15,stdin),S==D)?EOF:*S++)
inline int g() {
R ret=,fix=; register char ch; while(!isdigit(ch=getchar())) fix=ch=='-'?-:fix;
do ret=ret*+(ch^); while(isdigit(ch=getchar())); return ret*fix;
} inline bool isempty(const char& ch) {return ch<=||ch>=;}
inline void gs(char* s) {register char ch; while(isempty(ch=getchar())); do *s++=ch; while(!isempty(ch=getchar()));}
}using Fread::g; using Fread::gs;
const int B=,N=;
int n,m,s[N],a[N],pos[N],ans;
ull x[N<<],sum[N<<],p[N],cnt,hsh;
inline void upd(int tr,int l,int r) {
sum[tr]=sum[ls]+sum[rs];
x[tr]=x[ls]*p[sum[rs]]+x[rs];
}
inline void change(int tr,int l,int r,int pos,int d) {
if(l==r) {sum[tr]+=(d?:-); x[tr]=d; return ;}
if(pos<=md) change(ls,l,md,pos,d);
else change(rs,md+,r,pos,d); upd(tr,l,r);
}
signed main() {
#ifdef JACK
freopen("NOIPAK++.in","r",stdin);
#endif
m=g(),n=g(); p[]=; for(R i=;i<=m;++i) p[i]=p[i-]*B;
for(R i=;i<=m;++i) s[i]=g(),hsh=hsh*B+s[i],cnt+=p[i-];
for(R i=;i<=n;++i) a[i]=g(),pos[a[i]]=i;
for(R i=;i<=n;++i) {
if(i>m) change(,,n,pos[i-m],);
change(,,n,pos[i],i);
R d=i-m; if(d>=&&x[]==d*cnt+hsh) ++ans;
} printf("%d\n",ans);
}

2019.06.15

CF213E Two Permutations 线段树维护哈希值的更多相关文章

  1. 【URAL 1989】 Subpalindromes(线段树维护哈希)

    Description You have a string and queries of two types: replace i'th character of the string by char ...

  2. 【uoj#164】[清华集训2015]V 线段树维护历史最值

    题目描述 给你一个长度为 $n$ 的序列,支持五种操作: $1\ l\ r\ x$ :将 $[l,r]$ 内的数加上 $x$ :$2\ l\ r\ x$ :将 $[l,r]$ 内的数减去 $x$ ,并 ...

  3. 【bzoj3064】Tyvj 1518 CPU监控 线段树维护历史最值

    题目描述 给你一个序列,支持4种操作:1.查询区间最大值:2.查询区间历史最大值:3.区间加:4.区间赋值. 输入 第一行一个正整数T,表示Bob需要监视CPU的总时间. 然后第二行给出T个数表示在你 ...

  4. 滑动窗口(poj,线段树维护区间最值)

    题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...

  5. [CSP-S模拟测试]:椎(线段树维护区间最值和单调栈)

    题目描述 虽不能至,心向往之. $Treap=Tree+Heap$ 椎$=$树$+$堆 小$\pi$学习了计算机科学中的数据结构$Treap$. 小$\pi$知道$Treap$指的是一种树. 小$\p ...

  6. SPOJ 1557 GSS2 - Can you answer these queries II (线段树+维护历史最值)

    都说这题是 GSS 系列中最难的,今天做了一下,名副其实 首先你可以想到各种各样的在线乱搞想法,线段树,主席树,平衡树,等等,但发现都不太可行. 注意到题目也没有说强制在线,因此可以想到离线地去解决这 ...

  7. zoj 3888 Twelves Monkeys 二分+线段树维护次小值

    链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=3888 Twelves Monkeys Time Limit: 5 ...

  8. LOJ 164 【清华集训2015】V——线段树维护历史最值

    题目:http://uoj.ac/problem/164 把操作改成形如 ( a,b ) 表示加上 a 之后对 b 取 max 的意思. 每个点维护当前的 a , b ,还有历史最大的 a , b 即 ...

  9. cf213E 线段树维护hash

    链接 https://codeforces.com/contest/213/problem/E 题目大意 给出两个排列a.b,长度分别为n.m,你需要计算有多少个x,使 得\(a_1 + x; a_2 ...

随机推荐

  1. 【LeetCode】084. Largest Rectangle in Histogram

    题目: Given n non-negative integers representing the histogram's bar height where the width of each ba ...

  2. 洛谷【P2431】正妹吃月饼

    二进制前置技能:https://www.cnblogs.com/AKMer/p/9698694.html 题目传送门:https://www.luogu.org/problemnew/show/P24 ...

  3. bzoj 1185 最小矩形覆盖 —— 旋转卡壳

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1185 枚举一条边,维护上.左.右方的点: 上方点到这条边距离最远,所以用叉积求面积维护: 左 ...

  4. NetScaler VPX在Azure上的部署(二)

    本文是Citrix的工程师协助完成.主要是Citrix的VPX的配置. 导入License   进入NetScaler 中点击管理许可   导入后将有提示,请确认重启.   配置Azure HA 由于 ...

  5. web攻击之七:常见CDN回源问题总结

    1. URL链接出现非法链接 (如 */./Play/show/id/349281 ); 2. CDN接收未知Referer处理失效(目前搜索引擎的状态码为499); 3. CDN抓取服务器 Cach ...

  6. 杂项-Log:log4net

    ylbtech-杂项-Log:log4net log4net库是Apache log4j框架在Microsoft .NET平台的实现,是一个帮助程序员将日志信息输出到各种目标(控制台.文件.数据库等) ...

  7. MAXOS安装FFMPEG

    安装brew 安装方法:命令行输入 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/ins ...

  8. springMVC绑定json参数之二(2.1.1)

    二.springmvc 接收不同格式的json字符串 1.首先扫盲几个知识点: 例子如下: 前台传递json对象(这里uu[0]的名字uu要和Test对象中的属性List<User>名称对 ...

  9. window下rails4.1 发生TZInfo::DataSourceNotFound 错误 - smallbottle

    在官网上学习rails 4.1 ,启动rails server之后发生了如下错误 $ rails server Booting WEBrick Rails 4.1.0 application star ...

  10. C语言连接mysql -select

    C语言实现查询mysql数据库的行数,列的属性,以及每条记录. /* select.c */ #include <stdio.h> #include <stdlib.h> #i ...