E. Two Permutations
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

Examples
Input
1 1
1
1
Output
1
Input
1 2
1
2 1
Output
2
Input
3 3
2 3 1
1 2 3
Output
0

【题解】

由于a和b都是排列,其实就是找b中i...i + n - 1的相对位置与a中1...n的相对位置是否相同

康托展开显然是不好做的(反正我不会)

于是我们可以hash,拿线段树做即可

线段树下标是排列位置,线段数内的值是该位置的值

先把1...n的位置放进去,然后查行不行

然后把1拿出来,把n + 1的位置放进去,看看行不行

(注意由于每个数字都增加1,因此hash值应增加B^0 + B^1 + B^2.. + B^n,前缀和处理即可)

以此类推

因为没有膜够所以WA了好几发

具体看代码

 #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
inline void swap(long long &a, long long &b)
{
long long tmp = a;a = b;b = tmp;
}
inline void read(long long &x)
{
x = ;char ch = getchar(), c = ch;
while(ch < '' || ch > '')c = ch, ch = getchar();
while(ch <= '' && ch >= '')x = x * + ch - '', ch = getchar();
if(c == '-')x = -x;
} const long long MAXN = + ;
const long long MOD1 = ;
const long long MOD2 = ;
const long long B = ;
const long long MOD = ; long long data[][MAXN], ans, sum1, sum2, sum[MAXN], val1, val2, bit1[MAXN], bit2[MAXN], cnt[MAXN << ], size1[MAXN], size2[MAXN], n, m; //在权值p这个位置,[x == 1]增加k,[x == -1]减小k
void modify(long long p, long long k, long long x, long long o = , long long l = , long long r = m)
{
if(l == r && p == l)
{
data[][o] += k * x % MOD1;
data[][o] += k * x % MOD2;
size1[o] += x, size2[o] += x;
return;
}
long long mid = (l + r) >> ;
if(mid >= p) modify(p, k, x, o << , l, mid);
else modify(p, k, x, o << | , mid + , r);
data[][o] = ((data[][o << | ] * bit1[size1[o << ]]) % MOD1 + data[][o << ]) % MOD1;
data[][o] = ((data[][o << | ] * bit2[size2[o << ]]) % MOD2 + data[][o << ]) % MOD2;
size1[o] = size1[o << ] + size1[o << | ];
size2[o] = size2[o << ] + size2[o << | ];
return;
} int main()
{
// freopen("data.txt", "r", stdin);
read(n) ,read(m);
bit1[] = ;bit2[] = ;
for(register long long i = ;i <= m;++ i)
bit1[i] = (bit1[i - ] * B)%MOD1, bit2[i] = (bit2[i - ] * B)%MOD2;
for(register long long i = ;i <= n;++ i)
{
long long tmp;
read(tmp);
val1 += tmp * bit1[i - ] % MOD1;
val1 %= MOD1;
val2 += tmp * bit2[i - ] % MOD2;
val2 %= MOD2;
sum1 += bit1[i - ];sum1 %= MOD1;
sum2 += bit2[i - ];sum2 %= MOD2;
}
for(register long long i = ;i <= m;++ i)
{
long long tmp;
read(tmp);
cnt[tmp] = i;
}
for(register long long i = ;i <= m;++ i)
{
modify(cnt[i], i, );
if(i >= n)
{
if(data[][] == (val2 + (sum2 * (i - n))%MOD2)%MOD2 && data[][] == (val1 + (sum1 * (i - n))%MOD1)%MOD1)
++ ans;
modify(cnt[i - n + ], i - n + , -);
}
}
printf("%d", ans);
return ;
}

Codeforces213E

Codefroces 213E. Two Permutations的更多相关文章

  1. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  4. POJ2369 Permutations(置换的周期)

    链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. Permutations

    Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...

  6. 【leetcode】Permutations

    题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...

  7. [leetcode] 47. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  8. Leetcode Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  9. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

随机推荐

  1. 杂项-DTO:DTO(数据传输对象)

    ylbtech-杂项-DTO:DTO(数据传输对象) 数据传输对象(DTO)(Data Transfer Object),是一种设计模式之间传输数据的软件应用系统.数据传输目标往往是数据访问对象从数据 ...

  2. Docker系列(十):Kubernetes集群入门

    kubenetes安装 官网:https://github.com/kubernetes/kubernetes/blob/release-1.0/docs/getting-started- guide ...

  3. jquery 判断当前设备是PC端还是移动端

    $(function(){ var system = { win: false, mac: false, xll: false, ipad:false }; //检测平台 var p = naviga ...

  4. CentOS6.5下源码安装多个MySQL实例及复制搭建

    多实例安装本节是在CentOS6.5下源码安装MySQL5.6.35的基础上,在同一台机器增加一个MySQL实例.参考Centos中安装多个mysql数据的配置实例,安装目录为/usr/local/m ...

  5. Linux 启动dubbo管控台:

  6. 2019-5-21-C#-在-构造函数添加-CallerMemberName-会怎样

    title author date CreateTime categories C# 在 构造函数添加 CallerMemberName 会怎样 lindexi 2019-05-21 11:28:32 ...

  7. LL(1),LR(0),SLR(1),LALR(1),LR(1)对比与分析

    前言:考虑到这几种文法如果把具体内容讲下来肯定篇幅太长,而且繁多的符号对初学者肯定是极不友好的,而且我相信看这篇博客的人已经对这几个文法已经有所了解了,本篇博客的内容只是对 这几个文法做一下对比,加深 ...

  8. 搭建nodejs代理服务器,从而解决跨域问题

    先在同级处新建js文件(app.js) 使用时npm 安装 Node.js 模块语法 也就是对应的文件所在地“npm install”一下 然后安装对应需要的模块: expresspathreques ...

  9. PHP7中标量类型declare的用法详解

    这篇文章主要介绍了PHP7标量类型declare用法,结合实例形式分析了PHP7中标量类型declare的功能.特性与相关使用技巧,需要的朋友可以参考下 本文实例讲述了PHP7标量类型declare用 ...

  10. MyBatis配置文件(二)--settings配置

    settings是MyBatis中最复杂的配置,它能影响MyBatis底层的运行,大部分情况下使用默认值,只需要修改一些常用的规则即可.常用规则有自动映射.驼峰命名映射.级联规则.是否启动缓存.执行器 ...