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 ...
随机推荐
- Java集合操作类Collections的一些常用方法
public static void main(String[] args) { List<Integer> list = new ArrayList<Integer>(); ...
- CentOS6和CentOS7服务开机启动
CentOS 6和CentOS 7都可以定义开机启动哪些服务,但CentOS 6的命令是chkconfig,CentOS 7是systemctl. 本文将介绍两种命令的实现方式. 一.CentOS 6 ...
- GPIO编程1:用文件IO的方式操作GPIO
概述 通过 sysfs 方式控制 GPIO,先访问 /sys/class/gpio 目录,向 export 文件写入 GPIO 编号,使得该 GPIO 的操作接口从内核空间暴露到用户空间,GPIO 的 ...
- Rails多个复选框--check_box_tag
一.简单粗暴的解决方法 view <% @roles.each do |role| %> <%= check_box_tag 'roles[]', role.id%> < ...
- DotNetBar 第三方控件使用
1.BalloonTip(气泡提醒) 效果: 代码: balloonTip1.SetBalloonCaption(txtusername, "提示"); ba ...
- CentOS配置LDAP服务器
环境:centos 5.8 安装: 1.yum安装oepnldap.openldap-servers.openldap-clients.openldap-devel [root@hao-linux ~ ...
- 动态Result配置
步骤一:建立DynaAction,主要代码如下: package com.asm; public class DynaAction extends ActionSupport { private St ...
- codeforces educational round25
A #include<bits/stdc++.h> using namespace std; typedef long long ll; int main(){ ; string s; c ...
- mongodb教程国外
https://docs.mongodb.com/manual/reference/operator/aggregation/lookup/
- Angular10 组件之间的通讯
1 父组件和子组件之间的通讯 2 利用中间组件实现两个组件之间的通讯 3 利用服务实现两个组件之间的通讯 2017年8月26日20:09:13 待更新... 1 组件之间的关系图 1.1 父子关系 1 ...