/*
线段树 + 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. 虚拟机oracle virtualbox 上安装centos6.5 网络设置

    上篇文章写到,在虚拟机上安装centos6.5,结果依照文章非常顺利的安装了,可是用yum安装软件的时候.报错,源有问题,不能下载,然后ping一下摆渡.非常悲催 dns解析不了,cat /etc/r ...

  2. 经典算法——Jump Game(II)

    Given an array of non-negative integers, you are initially positioned at the first index of the arra ...

  3. EXTJS4自学手册——组合图像

    Ext.create('Ext.panel.Panel', { title: '组合图像', renderTo: 'ComplexDiagram', items: [{ xtype: 'button' ...

  4. 【Python3 爬虫】08_正则表达式(元字符与语法)

    元字符表 符号 说明 示例 . 表示任意字符 'abc'  >>>'a.c'   >>>结果为:'abc' ^ 表示字符开头 'abc'  >>> ...

  5. Android开发SDK接入机智云,智能家居实现APP远程控制多设备

    代码地址如下:http://www.demodashi.com/demo/12847.html 一.前言. 此框架只用了一周星期做了出来,因为对机智云的框架比较熟悉了 !期间SDK初始化出了问题,去咨 ...

  6. NodeJS on Nginx: 使用nginx反向代理处理静态页面

    最近OurJS后台已经从纯node.js迁移到了Nginx+NodeJS上来了,感觉性能提升了不少,特与大家分享. Nginx ("engine x") 是一个高性能的 HTTP ...

  7. MATLAB 的字符串分析

    MATLAB的字符串分析. 字符串实际上是指1Xn 的字符数组. MATLAB软件具有强大的字符串处理功能,提供了很多的字符或字符串处理函数,包括字符串的创建.字符串的属性.比较.查找以及字符串的转换 ...

  8. 【Linux指标】内存篇

    1:内存使用率 指标名称 指标含义 单位 采集方式(Linux) 采集方式(Windows) AGT.可用内存 GB 通过/proc/meminfo得到MemAvailable;若/proc/memi ...

  9. sublime test3 安装及配置

    操作系统: Ubuntu16.04 注: 除下载及安装外,其他配置不限操作系统 1. 下载及安装 官网:https://www.sublimetext.com/ 进入官网 点击 INSTALL FOR ...

  10. Angular2升级到Angular4

    angular4终于在两天前发布了正式版本,那么怎么升级呢?其实angular2和angular4之间属于平滑过渡,并不像1和2之间颠覆性的重写代码. npm uninstall -g @angula ...