/*
线段树 + 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. 开源框架Quartz动态加入、改动和删除定时任务 (二)

    貌似每次回过头去看之前写的一些东西,总感觉不是非常完美~~虽说不做完美人.但也要做完美事!这次主要是针对Quartz的动态维护和Spring集成.简单粗暴一点,直接上代码,有什么不了解留言交流 先来一 ...

  2. 补番计划 (长沙理工大学第十一届程序设计竞赛)(双端队列+set容器+string)

    补番计划 Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other) Total Submissi ...

  3. Stack Exchange 的架构

    近日,Stack Exchange系统管理员blog上发布了一篇关于Stack Exchange的架构一瞥,其包括了Stack Overflow, Server Fault 和 Super User的 ...

  4. C#异常处理机制初步

    原地址:http://www.sudu.cn/info/html/edu/net/20071227/50446.html 一.c#的异常处理所用到关键字 try 用于检查发生的异常,并帮助发送任何可能 ...

  5. Django——如何处理请求(URL配置和视图)

    URLconfig—— 为了绑定视图函数和URL,我们使用URLconf. URLconf 就像是 Django 所支撑网站的目录. 它的本质是 URL 模式以及要为该 URL 模式调用的视图函数之间 ...

  6. 常用sql语句记录

    1.表 --建表 if OBJECT_ID('Student') is not null create table Student( ID ,) not null, Name ), Code ), f ...

  7. &quot;《 Serial Drivers 》by Alessandro Rubini&quot; 学习笔记

    Introduction to "serial device driver"     (My study note) 膜拜大神的作品. Standing on the should ...

  8. Atitit  OOCSS vs bem

    Atitit  OOCSS vs bem     1. 今天最流行的CSS开发框架技术当属OOCSS,尽管还有其他类似的技术存在,如BEM.1 2. CSS设计模式:OOCSS 和 SMACSS1 2 ...

  9. spark-streaming的checkpoint机制源码分析

    转发请注明原创地址 http://www.cnblogs.com/dongxiao-yang/p/7994357.html spark-streaming定时对 DStreamGraph 和 JobS ...

  10. nginx反向代理批量实现https协议访问

    我们进入大多数HTTPS网站ie浏览器都会给出相关提醒了,但我配置了一台HTTPS代理机器发现css与js都加载不了,这个有朋友说是https页面,如果加载http协议的内容,会被认为页面不安全,所以 ...