当初竟然看成子串了$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. H264 NALU 使用PS封装 RTP发送

    最近由于项目平台需求,要将H264 NALU封装为PS再用RTP发送,PS封装按照ISO DEC-13818-1标准.一个PS包包含PS Header, PES Header, PS system h ...

  2. P2056 采花

    题目描述 萧芸斓是 Z国的公主,平时的一大爱好是采花. 今天天气晴朗,阳光明媚,公主清晨便去了皇宫中新建的花园采花.花园足够大,容纳了 n 朵花,花有 c 种颜色(用整数 1-c 表示) ,且花是排成 ...

  3. C/C++面试题总结(2)

    C++部分: 1.static(静态)变量有什么作用? 2.virtual关键字用法 3.const有哪些作用 或<王道程序员求职宝典>P95 4.new/delete与malloc/fr ...

  4. 洛谷【P1601】A+B Problem(高精)

    题目传送门:https://www.luogu.org/problemnew/show/P1601 高精度加法板子.我们灵性地回忆一波小学学加法列竖式的场景(从\(6\)岁开始口算从未打过草稿的大佬请 ...

  5. 三 Flask web开发快速入门

    1:会话: from flask import Flask, url_for, request, render_template, session from werkzeug.utils import ...

  6. linux 时间处理 + 简单写log

    1s ==1000ms == 1,000,000us == 1,000,000,000 nanosecond uname -a Linux scott-Z170X 4.15.0-34-generic ...

  7. Python模块-subprocess模块

    Run()方法 >>> a = subprocess.run(['df','-h']) 文件系统 容量 已用 可用 已用% 挂载点 udev 468M 0 468M 0% /dev ...

  8. CCNet说明文档

    1.CCNet安装步骤 1)    安装CCNet服务器端:CruiseControl.NET-1.8.5.0-Setup.exe 2)    安装CCNet客户端:CruiseControl.NET ...

  9. java中的equals方法

    这个方法首先比较的是两个对象的地址是否相同,如果相同直接返回true, 否则, (1)如果是string类型的先比较是否是string类型,是的话,再比较是否长度相同,相同的话再比较,每个字符是否相同 ...

  10. css 雪碧图

    CSS Sprites在国内很多人叫css精灵,是一种网页图片应用处理方式.它允许你将一个页面涉及到的所有零星图片都包含到一张大图中去,这样一来,当访问 该页面时,载入的图片就不会像以前那样一幅一幅地 ...