Two Permutations

关键是没想到按大小顺序把第二个排列一个一个加入线段树, 然后线段树维护整体的hash值,

得到的hs值减去一个sub 之后与, 第一个排列的hash值比较。

#include<bits/stdc++.h>
#define LL long long
#define LD long double
#define ull unsigned long long
#define fi first
#define se second
#define mk make_pair
#define PLL pair<LL, LL>
#define PLI pair<LL, int>
#define PII pair<int, int>
#define SZ(x) ((int)x.size())
#define ALL(x) (x).begin(), (x).end()
#define fio ios::sync_with_stdio(false); cin.tie(0);using namespace std;const int N = 2e5 + 7;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 998244353;
const double eps = 1e-8;
const double PI = acos(-1);template<class T, class S> inline void add(T& a, S b) {a += b; if(a >= mod) a -= mod;}
template<class T, class S> inline void sub(T& a, S b) {a -= b; if(a < 0) a += mod;}
template<class T, class S> inline bool chkmax(T& a, S b) {return a < b ? a = b, true : false;}
template<class T, class S> inline bool chkmin(T& a, S b) {return a > b ? a = b, true : false;}int n, m;
int a[N], b[N];
int pos[N];
ull Pow[N];#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1struct info {int cnt;ull hs;
};
info operator + (const info& a, const info& b) {info c;c.cnt = a.cnt + b.cnt;c.hs = b.hs + a.hs * Pow[b.cnt];return c;
}
struct segmentTree {info a[N << 2];void update(int p, int v, int l, int r, int rt) {if(l == r) {if(!v) a[rt].cnt = a[rt].hs = 0;else a[rt].cnt = 1, a[rt].hs = v;return;}int mid = l + r >> 1;if(p <= mid) update(p, v, lson);else update(p, v, rson);a[rt] = a[rt << 1] + a[rt << 1 | 1];}
} Tree;int main() {for(int i = Pow[0] = 1; i < N; i++) Pow[i] = Pow[i - 1] * 233333;scanf("%d%d", &n, &m);for(int i = 1; i <= n; i++) scanf("%d", &a[i]);for(int i = 1; i <= m; i++) {scanf("%d", &b[i]);pos[b[i]] = i;}int ans = 0;ull hsA = 0;ull sub = 0;for(int i = 1; i <= n; i++) hsA *= 233333, hsA += a[i];for(int i = 1; i <= n; i++) sub *= 233333, sub += 1;for(int i = 1; i <= n; i++) Tree.update(pos[i], i, 1, m, 1);ans += hsA == Tree.a[1].hs;for(int i = n + 1; i <= m; i++) {Tree.update(pos[i - n], 0, 1, m, 1);Tree.update(pos[i], i, 1, m, 1);ull hsB = Tree.a[1].hs;hsB -= sub * (i - n);ans += hsA == hsB;}printf("%d\n", ans);return 0;}/*
*/

转载于:https://www.cnblogs.com/CJLHY/p/10893872.html

Codeforces 213E Two Permutations 线段树 (看题解)相关推荐

  1. Codeforces 671C Ultimate Weirdness of an Array 线段树 (看题解)

    Ultimate Weirdness of an Array 写不出来, 日常好菜啊.. 考虑枚举GCD, 算出一共有多少个对 f(l, r) <= GCD, 我们用fuc[ i ] 表示的是在 ...

  2. Codeforces 311D Interval Cubing 数学 + 线段树 (看题解)

    Interval Cubing 这种数学题谁顶得住啊. 因为 (3 ^ 48) % (mod - 1)为 1 , 所以48个一个循环节, 用线段树直接维护. #include<bits/stdc ...

  3. 洛谷 P3373 【模板】线段树 2 题解

    洛谷 P3373 [模板]线段树 2 题解 题面 题目链接:[戳这里](https://www.luogu.org/problemnew/show/P3373) 题目描述 输入输出格式 输入输出样例 ...

  4. Codeforces 997E Good Subsegments (线段树)

    题目链接 https://codeforces.com/contest/997/problem/E 题解 经典题,鸽了 159 天终于看明白题解了.. 考虑一个区间是连续的等价于这个区间内的 \((\ ...

  5. 蒟蒻君的刷题日记Day12(线段树专题T4):P8082 [COCI2011-2012#4] KEKS 线段树版题解

    解题思路 看题解区的大佬们用的都是单调栈,本蒟蒻献上一篇线段树题解. 整个数最大,首先位数是确定的,则肯定优先考虑高位大小. 大体思路就是从前向后依次求出每一位的值(好像是废话). 对于第 iii 位 ...

  6. CodeForces - 1539F Strange Array(线段树区间合并)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的序列,规定位置 iii 的贡献是:设 x=a[i]x=a[i]x=a[i],选择一个包含 iii 的区间 [l,r][l,r][l,r],将其中 ...

  7. CodeForces - 1430E String Reversal(线段树+模拟)

    题目链接:点击查看 题目大意:给出一个字符串 sss ,令其反转的串为 ttt ,每次操作可以将 ttt 中的两个相邻位置的字符交换,问最少需要进行多少次操作才能使得 ttt 变成 sss 题目分析: ...

  8. CodeForces - 1295E Permutation Separation(线段树+二维偏序,好题)

    题目链接:点击查看 题目大意:给出一个1~n的排列,现在要求选择一个合适的分割点,将整个排列分为两个部分,要求通过适当的移动使得前半部分的所有数值都小于后半部分的数值,现在给出移动每个数字所花费的代价 ...

  9. 【codeforces 12D】【线段树】【降维】【离散化】【三元组比较大小】

    [题意] 给出n个女士的三位属性xi,yi,zi(注意此处是一行x给完再给y再给z).若存在xi>xj && yi > yj  && zi>zj ,那 ...

最新文章

  1. 专访郭霖:成长无止境
  2. 浅谈RPA 在银行领域的十个场景应用
  3. java 向量上的坐标点_新高三知识点-点的平移公式
  4. kf真空接头标准尺寸_酒泉VER8314KN39H出口过滤器滤芯 高线真空滤油机介
  5. 项目管理实战之团队管理 (转)
  6. 项目Alpha冲刺--1/10
  7. 工作169:删除操作
  8. python入门三剑客_python数据分析三剑客基础之matpoltlib初解
  9. 穿越沙漠问题c语言算法,穿越沙漠问题---递推法
  10. 东野圭吾《沉默的巡游》简单书评(含剧透)
  11. 服务器ajax无响应时间,ajax 服务器响应时间
  12. alt+w热键组合无法使用
  13. mysql ibd 恢复_MySQL:如何从ibd文件中恢复数据
  14. .NETZ 原理分析 .NETZ - .NET Executables Compressor .NETZ compresses the Microsoft .NET Framework execu
  15. UE4材质 制作UV贴图
  16. java同步锁售票_线程同步锁之火车站售票案例
  17. 补充“为什么Scrum不行” (转自陈勇)
  18. 加餐2 | 银行与券商
  19. 计算机应用基础课程作业2016,2016年电大 2016秋电大计算机应用基础作业.doc
  20. 将普通数值转化为科学计数法且保留5位有效数字

热门文章

  1. PHP file_get_contents() 函数
  2. bzoj1500: [NOI2005]维修数列
  3. 通过实例看懂diff命令输出
  4. 关于排版与交互的问题
  5. 获取到某一方法的调用者的类名、方法名、命名空间
  6. 10月17日Servlet培训日记
  7. 移动和云环境下的报文传输流程--理解DNS解析、CDN资源下发、公网传输流程、数据中心网络
  8. Spark Streaming之checkpoint机制
  9. 下划线_Python中下划线的5种含义
  10. 嵌入式串口打印信息重定向到当前终端界面