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. 安装zabbix需要php的两个模块php-bcmath和php-mbstring(转)

    安装zabbix需要php的两个模块php-bcmath和php-mbstring 原创 Linux操作系统 作者:甲骨文技术支持 时间:2018-02-24 18:35:24  1472  0 1. ...

  2. 洛谷P2890 [USACO07OPEN]便宜的回文Cheapest Palindrome

    题目链接: 点我 题目分析: 玄学\(dp\) 设\(val[s[i] - 'a' + 1]\)表示字母\(s[i]\)的花费 首先发现对于一个已经回文了的串\(s[i, j]\),在\(s[i - ...

  3. Quartz:Quartz

    ylbtech-Quartz:Quartz Quartz是OpenSymphony开源组织在Job scheduling领域又一个开源项目,它可以与J2EE与J2SE应用程序相结合也可以单独使用.Qu ...

  4. UNION All中ORDER By的使用

    一个sql中,union了几个子查询.单独执行每个子查询都没问题,但union后执行,报ORA-00904: "xxx": invalid identifier关于union的使用 ...

  5. java线程队列

    工作原理 1.线程池刚创建时,里面没有一个线程.任务队列是作为参数传进来的.不过,就算队列里面有任务,线程池也不会马上执行它们. 2.当调用 execute() 方法添加一个任务时,线程池会做如下判断 ...

  6. 2019-6-27-WPF-如何给定两个点画出一条波浪线

    title author date CreateTime categories WPF 如何给定两个点画出一条波浪线 lindexi 2019-6-27 10:17:6 +0800 2019-6-26 ...

  7. MapReduce 图解流程

    Anatomy of a MapReduce Job In MapReduce, a YARN application is called a Job. The implementation of t ...

  8. [欧拉路]CF1152E Neko and Flashback

    1152E - Neko and Flashback 题意:对于长为n的序列c和长为n - 1的排列p,我们可以按照如下方法得到长为n - 1的序列a,b,a',b'. ai = min(ci, ci ...

  9. Django项目:CMDB(服务器硬件资产自动采集系统)--02--02CMDB将服务器基本信息提交到API接口

    AutoCmdb # urls.py """AutoCmdb URL Configuration The `urlpatterns` list routes URLs t ...

  10. xcode下的DerivedData

    在模拟器运行的情况下经常会出现以下的错误: error: remove /Users/mac/Library/Developer/Xcode/DerivedData/YuQing-amkrrucjrn ...