CF213E Two Permutations 线段树维护哈希值
当初竟然看成子串了$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 线段树维护哈希值的更多相关文章
- 【URAL 1989】 Subpalindromes(线段树维护哈希)
Description You have a string and queries of two types: replace i'th character of the string by char ...
- 【uoj#164】[清华集训2015]V 线段树维护历史最值
题目描述 给你一个长度为 $n$ 的序列,支持五种操作: $1\ l\ r\ x$ :将 $[l,r]$ 内的数加上 $x$ :$2\ l\ r\ x$ :将 $[l,r]$ 内的数减去 $x$ ,并 ...
- 【bzoj3064】Tyvj 1518 CPU监控 线段树维护历史最值
题目描述 给你一个序列,支持4种操作:1.查询区间最大值:2.查询区间历史最大值:3.区间加:4.区间赋值. 输入 第一行一个正整数T,表示Bob需要监视CPU的总时间. 然后第二行给出T个数表示在你 ...
- 滑动窗口(poj,线段树维护区间最值)
题目描述 现在有一堆数字共N个数字(N<=10^6),以及一个大小为k的窗口.现在这个从左边开始向右滑动,每次滑动一个单位,求出每次滑动后窗口中的最大值和最小值. 例如: The array i ...
- [CSP-S模拟测试]:椎(线段树维护区间最值和单调栈)
题目描述 虽不能至,心向往之. $Treap=Tree+Heap$ 椎$=$树$+$堆 小$\pi$学习了计算机科学中的数据结构$Treap$. 小$\pi$知道$Treap$指的是一种树. 小$\p ...
- SPOJ 1557 GSS2 - Can you answer these queries II (线段树+维护历史最值)
都说这题是 GSS 系列中最难的,今天做了一下,名副其实 首先你可以想到各种各样的在线乱搞想法,线段树,主席树,平衡树,等等,但发现都不太可行. 注意到题目也没有说强制在线,因此可以想到离线地去解决这 ...
- zoj 3888 Twelves Monkeys 二分+线段树维护次小值
链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do? problemCode=3888 Twelves Monkeys Time Limit: 5 ...
- LOJ 164 【清华集训2015】V——线段树维护历史最值
题目:http://uoj.ac/problem/164 把操作改成形如 ( a,b ) 表示加上 a 之后对 b 取 max 的意思. 每个点维护当前的 a , b ,还有历史最大的 a , b 即 ...
- cf213E 线段树维护hash
链接 https://codeforces.com/contest/213/problem/E 题目大意 给出两个排列a.b,长度分别为n.m,你需要计算有多少个x,使 得\(a_1 + x; a_2 ...
随机推荐
- ACM学习历程—HDU 5443 The Water Problem(RMQ)(2015长春网赛1007题)
Problem Description In Land waterless, water is a very limited resource. People always fight for the ...
- 【LeetCode】019. Remove Nth Node From End of List
Given a linked list, remove the nth node from the end of list and return its head. For example, Give ...
- [IBM]掌握Ajax,Ajax中的高级请求和响应
掌握 Ajax, Ajax 中的高级请求和响应 http://www.ibm.com/developerworks/cn/xml/wa-ajaxintro1.html http://www.ibm.c ...
- Linux mount指令
-o,是指option,可以指定username,password:当时我们就碰到一个坎,如何来避免输入用户名密码,其实本质并不是避免输入用户名米吗,而是某种可知的方式来进行权限控制:解决的方式就是采 ...
- ssh-keygen和ssh-copy-id的简单使用
实验环境是CentOS7: ssh-keygen产生公钥和私钥对. ssh-copy-id:将本机的公钥使用ssh协议复制到远程的客户端,ssh协议的公钥和私钥一般存放于~/.ssh下 #主机 [ro ...
- 报错apachectl restart
httpd not running, trying to start(98)Address already in use: make_sock: could not bind to address [ ...
- 每天一道算法题(1) ——不用乘除法求和1+2+…+n
题目:求1+2+-+n,要求不能使用乘除法.for.while.if.else.switch.case等关键字以及条件判断语句(A?B:C). 方法1:使用函数指针. typedef int (*fu ...
- 你所必须知道的HTML
只有在MySQL中可以使用"utf-8"的别名"utf8",但是在其他地方一律使用大写"UTF-8". htm 与 html 的区别 两种 ...
- 树莓派 Learning 002 装机后的必要操作 --- 06 共用键鼠 之 windows和树莓派
树莓派 装机后的必要操作 - 使用Synergy软件 共用键鼠 之 windows和树莓派 我的树莓派型号:Raspberry Pi 2 Model B V1.1 装机系统:NOOBS v1.9.2 ...
- PCLVisualizer可视化类(5)
博客转载自:http://www.pclcn.org/study/shownews.php?lang=cn&id=171 自定义交互 多数情况下,默认的鼠标和键盘交互设置不能满足用户的需求,用 ...