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 ...
随机推荐
- python-001 第一个Python3.x程序 hello world
我们可以使用以下命令来查看我们使用的Python版本: (d:\ProgramData\Anaconda3) C:\Users\Administrator.2016-20160920ET>pyt ...
- 服务器端架构及实战 — C#分享
简介 此文是我2008年读研究生期间做的C#项目技术分享,给计算机专业学生的一些经验分享. 当时工作2年后读研. 计算机基础了解及介绍 了解计算机的核心课程和大家的理解 二进制的历史和原理 数字逻辑及 ...
- [转]android 如何获取第三方app的sha1值
对于android 应用的sha1值和md5值的获取,如果是我们自己的应用,不论是获取测试的和正式的都是比较方便的.但是如何去获取别人开发的app的sha1和md5呢,并且我们只有apk有没有相关的文 ...
- 【2018 Multi-University Training Contest 3】
01:https://www.cnblogs.com/myx12345/p/9420198.html 02: 03: 04:https://www.cnblogs.com/myx12345/p/940 ...
- python监控tomcat日记文件
最近写了一个用python监控tomcat日记文件的功能 实现的功能: 监控日记文件中实时过来的记录,统计每分钟各个接口调用次数,统计结果插入oracle #!/usr/bin/python # -* ...
- BZOJ 2440 莫比乌斯函数+容斥+二分
2440: [中山市选2011]完全平方数 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5473 Solved: 2679[Submit][Sta ...
- Palindrome--poj 1159(最长公共子字符串+滚动数字)
http://poj.org/problem?id=1159 题目大意: 给你一个n 代表n个字符 第二行给你一个字符串 求使这个字符串变成回文字符串 最少需要添加多少个字符 分析: 原 ...
- 通过XMLHttpRequest,ActiveXObject实现ajax请求
今天学习了原生的ajax请求.我将涉及到的ajax请求方法封装成了一个对象: var xhr={ getXHR:function(){ var XHR = null; ...
- Maven使用tomcat7-maven-plugin插件run时出现错误: A child container failed during start java.util.concurrent.ExecutionException: org.apache.catalina.LifecycleException: Failed to start component
错误如下: A child container failed during startjava.util.concurrent.ExecutionException: org.apache.catal ...
- dtrace
http://blog.csdn.net/lw1a2/article/details/7389323