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. springcluoud入门

    概念: Spring Cloud是一个分布式的整体解决方案. Spring Cloud 为开发者提供了在分布式系统(配置管理,服务发现,熔断,路由,微代理,控制总线,一次性token,全局琐,lead ...

  2. CSS工程化

    CSS工程化 引言: 你在编写CSS代码时,是否遇到过这样的问题: 代码没有层次结构,难以看出嵌套关系 .site-footer .footer-container .footer-menu { di ...

  3. CAS去掉HTTPS认证

    如何去掉HTTPS认证? 说明:默认情况下HTTP也是可以访问CAS SERVER的,但认证,登陆,退出等操作均没有任何的效果.所以必须作出下面的修改 1.进入WEB-INF\spring-confi ...

  4. Ubuntu 16.04 安装STS

    先将STS下载下来,网址是 https://spring.io/tools/sts/all ,然后将STS压缩包移动或者copy到想要放置的位置,比如, sudo cp spring-tool-sui ...

  5. SpringBoot Controller接收参数的几种方式盘点

    本文不再更新,可能存在内容过时的情况,实时更新请移步我的新博客:SpringBoot Controller接收参数的几种方式盘点: SpringBoot Controller接收参数的几种常用方式盘点 ...

  6. [Swoole系列入门教程 4] 定时器与心跳demo

  7. mac上安装软件后,桌面上软件的图标如何去掉?

    桌面上的图标是软件的镜像包,默认安装以镜像形式,你选中它,按command+e 就可以推掉它

  8. Odoo的权限

    Odoo的权限的核心是权限组(res_groups).对每个权限组,可以设置权限组的菜单表示,对象表示,记录规则表示,字段表示. 1.菜单/对象级别 设置哪些人可以访问哪些菜单/对象,对象的访问权限包 ...

  9. Django项目:CRM(客户关系管理系统)--82--72PerfectCRM实现CRM动态菜单和角色

    #models.py # ————————01PerfectCRM基本配置ADMIN———————— from django.db import models # Create your models ...

  10. 2019牛客暑期多校赛(第一场) A Equivalent Prefixes(单调栈)

    传送门:https://ac.nowcoder.com/acm/contest/881/A 题意:给定两个数组a和b,求最大的p,满足在区间 [1,p] 中任何区间的两个数组的最小值的下标都相等. 思 ...