/*
线段树 + hash:
首先我们可以知道A序列是1~n的排列,那么我们可以先在B序列中把1~n的排列找出来,看其相对位置是否与A相同(hash可做),相同即表明存在一个d满足条件。
以此类推,我们接下来可以把B中 2~ n + 1的排列找出来,如果其每位-1后相对顺序还是与A序列一致,即存在d-1也满足。。。
线段树中保存一个长度为n的序列的hash。具体看代码
*/
#include <bits/stdc++.h> using namespace std; #define lson l, m ,rt<<1
#define rson m + 1, r, rt <<1|1
const int maxn = 2e5 + ;
const int M = ;//hash底数
typedef long long ll;
int pw[maxn], s, h;
int n, m; int hash[maxn << ],sz[maxn << ];
void pushUp(int rt){
hash[rt] = hash[rt<<] * pw[sz[rt<<|]] + hash[rt<<|];
sz[rt] = sz[rt<<] + sz[rt<<|];
}
void update(int p, int val, int tag, int l, int r, int rt){
if (l == r){
hash[rt] += tag * val;
sz[rt] += tag;
return ;
}
int m = (l + r) >> ;
if (p <= m) update(p, val, tag, lson);
else update(p, val, tag, rson);
pushUp(rt);
}
int pos[maxn];
int main(){
int x;
while (~scanf("%d%d", &n, &m)){
pw[] = ;
h = s = ;
for (int i = ; i <= n; ++i){
scanf("%d", &x);
h = h * M + x;
pw[i] = pw[i - ] * M;//M的i次方
s += pw[i - ];//1 ~n的排列的hash转化成 2 ~ n + 1的hash需要+s.......自己算算
}
for (int i = ; i <= m; ++i){
scanf("%d", &x);
pos[x] = i;
}
memset(hash, , sizeof(hash));
memset(sz, , sizeof(sz));
int ans = ;
for (int i = ; i <= m; ++i){
update(pos[i], i, , , m, );
if (i >= n){
if ((hash[] - (s * (i - n)) == h))
ans++;
update(pos[i - n + ], i - n + , -, , m, );
}
}
printf("%d\n", ans);
}
return ;
}

CodeForces 213 E的更多相关文章

  1. Codeforces Round #213 (Div. 2) A. Good Number

    #include <iostream> #include <vector> using namespace std; int main(){ ; cin >> n ...

  2. Codeforces Round #213 (Div. 2) B. The Fibonacci Segment

    #include <iostream> #include <algorithm> #include <vector> using namespace std; in ...

  3. codeforces.com/problemset/problem/213/C

    虽然一开始就觉得从右下角左上角直接dp2次是不行的,后面还是这么写了WA了 两次最大的并不一定是最大的,这个虽然一眼就能看出,第一次可能会影响第二次让第二次太小. 这是原因. 5 4 32 1 18 ...

  4. Codeforces Round #213 (Div. 1) B - Free Market 思维+背包 好题

    B - Free Market 思路:这个题怎么说呢,迷惑性很大,题目里说了交换了两个集合的时候如果有相同元素不能交换,感觉如果没 这句话能很快写出来, 其实当交换的两个集合有重复元素的时候只要交换那 ...

  5. CodeForces比赛总结表

    Codeforces A                     B                        C                             D            ...

  6. CodeForces 445B DZY Loves Chemistry

    DZY Loves Chemistry Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64 ...

  7. Codeforces Round #131 (Div. 1) B. Numbers dp

    题目链接: http://codeforces.com/problemset/problem/213/B B. Numbers time limit per test 2 secondsmemory ...

  8. Codeforces 364A - Matrix

    原题地址:http://codeforces.com/problemset/problem/364/A 题目大意: 给定一个数字a(0 ≤ a ≤ 109)和一个数列s(每个数都是一位,长度不超过40 ...

  9. Codeforces Round #322 (Div. 2) D. Three Logos 暴力

    D. Three Logos Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/581/problem ...

随机推荐

  1. chrome护眼模式

    chrome护眼模式 使用stylish插件: 学习:https://jingyan.baidu.com/article/b907e627f74df146e6891c67.html 插件下载:http ...

  2. 倍福TwinCAT(贝福Beckhoff)常见问题(FAQ)-如何查看错误代码

    一般模块出错会在Error中显示为TRUE,同时ErrorID中会有一个错误代码,下图所示就是出错之后的效果   在变量表中也可以看到,右击转成16进制查看   由于是NC模块出错,所以可以再NC的E ...

  3. B9:备忘录模式 Memento

    在不破坏封装性的前提下,捕获一个对象的内部状态,并在该对象之外保存这个状态.这样以后就可以将该对象恢复到原先保存的状态 UML: 示例代码: class Role { private $hp; pri ...

  4. GDB基本命令(整合)(转)

    directory:添加源文件目录 l src.cpp:line_num可进入文件 如:l src.cpp:10 回车自动重复上一命令 一.gdb调试基本知识a.调试器指示的是将要执行的代码行b.只有 ...

  5. 自己动手开发更好用的markdown编辑器-05(粘贴上传图片)

    这里文章都是从个人的github博客直接复制过来的,排版可能有点乱. 原始地址 http://benq.im/2015/04/28/hexomd-05/   文章目录 1. 七牛云存储 1.1. 系统 ...

  6. docker run

    1.登录Docker Hub 账户 2.输入命令:构建一个web应用. docker run -d -P training/webapp python app.py 参数说明: -d:让容器在后台运行 ...

  7. jQuery中事件绑定

    一.前言 最近在做项目中要用到jQuery来绑定事件,首先想到的是$(selector).事件名();这样绑定事件的方式,这种方式对事件进行绑定其实也就是bind()方法,但当选择器匹配的元素过多,$ ...

  8. mount rootfs

    主要用到的命令为pivot_root,可man 8 pivot_root了解用法. 1. pivot_root - change the root filesystem pivot_root new_ ...

  9. abp使用风格定义

    一个开发人员的开发模式下面.用这个有太多代码要写了.所以需要限制与规范自己: 基于abp新系统的开发过程1>*.Core 增加文件夹eg:Questions1.1>增加实体文件eg:QAQ ...

  10. JS方法代理

    作者:Jiang, Jilin JS作为一门脚本语言.十分easy上手.外加其灵活性,能够轻而易举地扩展功能.今天,我们就聊聊JS的方法代理. 方法代理是脚本语言中常见的方法扩展形式.这样的灵活的形式 ...