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. 主机入侵防御系统(HIPS)分析

    主机入侵防御系统(Host Intrusion Prevent System,HIPS)是近几年出现并迅速发展的新兴产物,与传统意义的防火墙和杀毒软件不同,它并不具备特征码扫描和主动杀毒等功能,所以想 ...

  2. Logback 日志组件的使用

    Logback 是由 log4j 创始人设计的又一个开源日志组件. 一. logback 的介绍 ​ logback 当前分成三个模块:logback-core,logback- classic 和 ...

  3. 表单修饰符.lazy.number.trim

    <!DOCTYPE html> <html lang="zh"> <head> <title></title> < ...

  4. python-爬免费ip并验证其可行性

    前言 最近在重新温习python基础-正则,感觉正则很强大,不过有点枯燥,想着,就去应用正则,找点有趣的事玩玩 00xx01---代理IP 有好多免费的ip,不过一个一个保存太难了,也不可能,还是用我 ...

  5. jmeter命令行压测

    简介:使用非GUI模式,即命令行模式运行jmeter测试脚本能够大大缩减系统资源 1.配置jdk及添加环境变量 变量名:JAVA_HOME 变量值: C:\Program Files\Java\jdk ...

  6. Leetcode 125.验证回文字符串(Python3)

    题目: 给定一个字符串,验证它是否是回文串,只考虑字母和数字字符,可以忽略字母的大小写. 说明:本题中,我们将空字符串定义为有效的回文串. 示例 1: 输入: "A man, a plan, ...

  7. Spring的自定义注解简单实现

    1.注解的示例为在方法入参上加后缀 注解代码示例: @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) @Documente ...

  8. Python-爬虫实战 简单爬取豆瓣top250电影保存到本地

    爬虫原理 发送数据 获取数据 解析数据 保存数据 requests请求库 res = requests.get(url="目标网站地址") 获取二进制流方法:res.content ...

  9. python 3.6 关于python的介绍

    python的官方网站 https://www.python.org/ python 3.6 的官方网站的下载地址 https://www.python.org/downloads/release/p ...

  10. leyou_02_nginx使用域名访问本地项目

    1.nginx的搭建依赖环境 1.1 准备jdk环境 当前最新版本下载地址:http://www.oracle.com/technetwork/java/javase/downloads/index. ...