CodeForces-213E:Two Permutations(神奇的线段树+hash)
Rubik is very keen on number permutations.
A permutation a with length n is a sequence, consisting of n different numbers from 1 to n. Element number i (1 ≤ i ≤ n) of this permutation will be denoted as ai.
Furik decided to make a present to Rubik and came up with a new problem on permutations. Furik tells Rubik two number permutations: permutation a with length n and permutation b with length m. Rubik must give an answer to the problem: how many distinct integers d exist, such that sequence c (c1 = a1 + d, c2 = a2 + d, ..., cn = an + d) of length n is a subsequence of b.
Sequence a is a subsequence of sequence b, if there are such indices i1, i2, ..., in(1 ≤ i1 < i2 < ... < in ≤ m), that a1 = bi1, a2 = bi2, ..., an = bin, where n is the length of sequence a, and m is the length of sequence b.
You are given permutations a and b, help Rubik solve the given problem.
Input
The first line contains two integers n and m (1 ≤ n ≤ m ≤ 200000) — the sizes of the given permutations. The second line contains n distinct integers — permutation a, the third line contains m distinct integers — permutation b. Numbers on the lines are separated by spaces.
Output
On a single line print the answer to the problem.
Example
1 1
1
1
1
1 2
1
2 1
2
3 3
2 3 1
1 2 3
0
题意:给定全排列A(1-N),全排列B(1-M),问有多少个d,满足全排列A的每一位+d后,是B的子序列(不是字串)。
思路:对于全排列A,得到hashA,先在B中得到1-N的hashB。每次d++的新排列,等效于1到N+1的排列每一位减去1(hash实现)。
具体的代码一看就明白。
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef unsigned long long ull;
const int maxn=;
const int B=;
int N,M,pos[maxn],x;
ull g[maxn];
ull val,sum;
struct SegmentTree
{
ull hash[maxn<<];
int cnt[maxn<<];
void build(int Now,int l,int r)
{
hash[Now]=cnt[Now]=;
if(l==r) return ;
int mid=(l+r)>>;
build(Now<<,l,mid);
build(Now<<|,mid+,r);
}
void pushup(int Now)
{
hash[Now]=hash[Now<<]*g[cnt[Now<<|]]+hash[Now<<|];
cnt[Now]=cnt[Now<<]+cnt[Now<<|];
}
void update(int Now,int l,int r,int pos,int val,int num)
{
if(l==r)
{
hash[Now]+=num*val;
cnt[Now]+=num;
return;
}
int mid=(l+r)>>;
if(pos<=mid) update(Now<<,l,mid,pos,val,num);
else update(Now<<|,mid+,r,pos,val,num);
pushup(Now);
}
}Tree;
int main()
{
while(~scanf("%d%d",&N,&M))
{
val=sum=;
g[]=;
for(int i=;i<=N;i++) g[i]=g[i-]*B, sum+=g[i-];
for(int i=;i<=N;i++){
scanf("%d",&x);
val=val*B+x;
}
for(int i=;i<=M;i++){
scanf("%d",&x);
pos[x]=i;
}
Tree.build(,,M);
int ans=;
for(int i=;i<=M;i++)//把数值1~M按顺序加入线段树中
{
Tree.update(,,M,pos[i],i,);
if(i>=N)
{
if(Tree.hash[]-(sum*(i-N))==val) ans++;
Tree.update(,,M,pos[i-N+],i-N+,-);
}
}
printf("%d\n",ans);
}
return ;
}
CodeForces-213E:Two Permutations(神奇的线段树+hash)的更多相关文章
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- [Codeforces 280D]k-Maximum Subsequence Sum(线段树)
[Codeforces 280D]k-Maximum Subsequence Sum(线段树) 题面 给出一个序列,序列里面的数有正有负,有两种操作 1.单点修改 2.区间查询,在区间中选出至多k个不 ...
- codeforces 1217E E. Sum Queries? (线段树
codeforces 1217E E. Sum Queries? (线段树 传送门:https://codeforces.com/contest/1217/problem/E 题意: n个数,m次询问 ...
- [火星补锅] siano 神奇的线段树
前言: 本来以为很难打的,没想到主干一次就打对了,然而把输入的b和d弄混了,这sb错误调了两个小时... 解析: 神奇的线段树.注意到有一个性质,无论怎么割草,生长速度快的一定不会比生长速度慢的矮.因 ...
- BZOJ_2124_等差子序列_线段树+Hash
BZOJ_2124_等差子序列_线段树+Hash Description 给一个1到N的排列{Ai},询问是否存在1<=p1<p2<p3<p4<p5<…<pL ...
- bzoj2124: 等差子序列线段树+hash
bzoj2124: 等差子序列线段树+hash 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=2124 思路 找大于3的等差数列其实就是找等于 ...
- Codeforces 444 C. DZY Loves Colors (线段树+剪枝)
题目链接:http://codeforces.com/contest/444/problem/C 给定一个长度为n的序列,初始时ai=i,vali=0(1≤i≤n).有两种操作: 将区间[L,R]的值 ...
- Codeforces Gym 100513F F. Ilya Muromets 线段树
F. Ilya Muromets Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100513/probl ...
随机推荐
- CSU 1605 数独
题目大意: 9宫格每个位置都有对应的分数,填完数独后根据对应位置的分数相加之和求个最大值,不存在输出-1 说什么用位运算加速可以解决问题,但是对着标程还是T,最近学了dlx,发现这样解决数独快了很多 ...
- BZOJ3027 - [CEOI2004]Sweet
Portal Description 给出\(n(n\leq10),a,b(a,b\leq10^7)\)与\(\{c_n\}(c_i\leq10^6)\),求使得\(\sum_{i=1}^n x_i ...
- 【区间筛】2017多校训练四 HDU6069 Counting Divisors
http://acm.hdu.edu.cn/showproblem.php?pid=6069 [题意] 给定l,r,k,求 d(n)是n的因子个数 [思路] [Accepted] #include&l ...
- jvm参数设置 -vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M
-vmargs -Xms128M -Xmx512M -XX:PermSize=64M -XX:MaxPermSize=128M 这里有几个问题: 1. 各个参数的含义什么? 2. 为什么有的机器我将- ...
- 【BZOJ1031】字符加密Cipher(后缀数组)
题意:将一个长度为2n(复制粘贴后)的字符串的所有长度为n的后缀从小到大排序,并依次输出它们的最后一个字母. n<=100000 思路:裸SA,模板真难背 P党不得不写成C++风格 ..]of ...
- Python()- 面向对象三大特性----继承
继承: 继承是一种创建新类的方式,在python中,新建的类可以继承一个或多个父类(基类或超类),新建的类是所继承的类的(派生类或子类) 人类和狗 有相同的属性, 提取了一个__init__方法,在这 ...
- 利用Cufon技术渲染文字的简单示例
Cufon是一种能够根据指定的字体渲染文字的技术.今天试用了下,主要有几个步骤: 1.下载Cufon.js(http://cufon.shoqolate.com/generate/) 2.获取需要渲染 ...
- [洛谷U22156]未曾届到游览(矩阵树定理)
题目背景 又到了某任*堂开关中学一年一度的自主招生考试的时间了,在考试完后许多家长决定带着自己的孩子参观一下这所距千年名校还有890周年的百年学校: 题目描述 这所学校的布局非常奇怪,是一个由N 个点 ...
- [Bzoj1296][Scoi2009] 粉刷匠 [DP + 分组背包]
1296: [SCOI2009]粉刷匠 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2184 Solved: 1259[Submit][Statu ...
- NodeJS+MongoDB+AngularJS+Bootstrap书店示例
目录 一.Bootstrap 1.1.添加引用 1.2.在页面中使用BootStrap 1.3.可视化布局 二.使用MongoDB创建数据库 2.1.启动MongoDB数据库 2.2.启动数据库GUI ...