[CF1093E]Intersection of Permutations

题目大意:

给定两个长度为\(n(n\le2\times10^5)\)的排列\(A,B\)。\(m(m\le2\times10^5)\)次操作,操作分为以下两种:

  1. 询问有多少同时在\(A_{[x,y]}\)和\(B_{[l,r]}\)中出现的数。
  2. 交换\(B_x\)与\(B_y\)。

思路:

令\(v[a[i]]=i,b[i]=v[b[i]]\),这样询问就变成\([x,y]\)中有多少数在\(B_{[l,r]}\)中出现。

用树状数组+平衡树维护每个区间出现哪些数,询问时在平衡树中查找排名即可。

源代码:

#include<cstdio>
#include<cctype>
#include<climits>
#include<ext/pb_ds/tree_policy.hpp>
#include<ext/pb_ds/assoc_container.hpp>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
const int N=2e5+1;
int n,v[N],a[N];
using namespace __gnu_pbds;
using RBTree=tree<int,null_type,std::less<int>,rb_tree_tag,tree_order_statistics_node_update>;
class FenwickTree {
private:
RBTree t[N];
int lowbit(const int &x) const {
return x&-x;
}
int query(int p,const int &x,const int &y) const {
int ret=0;
for(;p;p-=lowbit(p)) {
const int L=*t[p].lower_bound(x);
const int R=*std::prev(t[p].upper_bound(y));
if(L<=R) ret+=t[p].order_of_key(R)-t[p].order_of_key(L)+1;
}
return ret;
}
public:
void init() {
for(register int i=1;i<=n;i++) {
t[i].insert(INT_MIN);
t[i].insert(INT_MAX);
}
}
void insert(int p,const int &x) {
for(;p<=n;p+=lowbit(p)) {
t[p].insert(x);
}
}
void erase(int p,const int &x) {
for(;p<=n;p+=lowbit(p)) {
t[p].erase(x);
}
}
int query(const int &l,const int &r,const int &x,const int &y) const {
return query(r,x,y)-query(l-1,x,y);
}
};
FenwickTree t;
int main() {
n=getint();
const int m=getint();
for(register int i=1;i<=n;i++) {
v[getint()]=i;
}
for(register int i=1;i<=n;i++) {
a[i]=v[getint()];
}
t.init();
for(register int i=1;i<=n;i++) {
t.insert(i,a[i]);
}
for(register int i=0;i<m;i++) {
const int opt=getint(),x=getint(),y=getint();
if(opt==1) {
const int l=getint(),r=getint();
printf("%d\n",t.query(l,r,x,y));
}
if(opt==2) {
t.erase(x,a[x]);
t.erase(y,a[y]);
std::swap(a[x],a[y]);
t.insert(x,a[x]);
t.insert(y,a[y]);
}
}
return 0;
}

[CF1093E]Intersection of Permutations的更多相关文章

  1. CF1093E Intersection of Permutations 树状数组套权值线段树

    \(\color{#0066ff}{ 题目描述 }\) 给定整数 \(n\) 和两个 \(1,\dots,n\) 的排列 \(a,b\). \(m\) 个操作,操作有两种: \(1\ l_a\ r_a ...

  2. [CF1093E]Intersection of Permutations:树套树+pbds

    分析 裸的二维数点,博主用树状数组套平衡树写的,顺便pbds真好用. Update on 2018/12/20:再解释一下为什么是二维数点,第一维是\(la \leq i \leq ra\),第二维是 ...

  3. CF1093E Intersection of Permutations [分块 +bitset]

    大家好, 我非常喜欢暴力数据结构, 于是就用分块A了此题 分块题,考虑前缀和 \(b_i\) 表示 bitset 即 \(0\) ~ $i $ 出现过的数字,然后考虑直接暴力复制块然后前缀和,修改也很 ...

  4. CF 1093 E. Intersection of Permutations

    E. Intersection of Permutations 链接 题意: 给定两个序列,询问第一个排列的[l1,r1]和第二个排列[l2,r2]中有多少个共同的数,支持在第二个排列中交换两个数. ...

  5. 【cdq分治】【CF1093E】 Intersection of Permutations

    传送门 果然前两天写完咕咕咕那个题的题解以后博客就开始咕咕咕了-- Description 给定整数 \(n\) 和两个 \(1~\sim~n\) 的排列 \(A,B\). \(m\) 个操作,操作有 ...

  6. CF 1093E Intersection of Permutations——CDQ分治

    题目:http://codeforces.com/contest/1093/problem/E 只能想到转化成查询一个区间里值在一个范围里的数的个数…… 没有想到这样适合用主席树套树状数组维护.不过据 ...

  7. Codeforces 1093E Intersection of Permutations [CDQ分治]

    洛谷 Codeforces 思路 一开始想到莫队+bitset,发现要T. 再想到分块+bitset,脑子一抽竟然直接开始写了,当然也T了. 最后发现这就是个裸的CDQ分治-- 发现\(a\)不变,可 ...

  8. E. Intersection of Permutations

    题意:给两个排列,2种操作1,查询两个区间a和b一样的值个数,2,交换b的两个值 题解:树套树,先把a变成1到n的排列,对b做相同的变换,然后问题就变成了查询区间lb,rb中la到ra的个数,带修改可 ...

  9. CF1093:E. Intersection of Permutations(树状数组套主席树)

    题意:给定长度为N的a数组,和b数组,a和b都是1到N的排列: 有两种操作,一种是询问[L1,R1],[L2,R2]:即问a数组的[L1,R1]区间和b数组的[L2,R2]区间出现了多少个相同的数字. ...

随机推荐

  1. Eclipse——在eclipse上安装Pydev插件实现python编程

    介绍:2003年7月16日,以 Fabio Zadrozny 为首的三人开发小组在全球最大的开放源代码软件开发平台和仓库 SourceForge 上注册了一款新的项目,该项目实现了一个功能强大的 Ec ...

  2. python setuptools

    在安装python依赖库时,我们使用pip install 或者python setup.py install. pip 会自己搜索适合的版本,python setup.py 需要下载源码本地安装.但 ...

  3. git应用

    安装 Git for windows git config --global user.name "zhangyue" git config --global user.mail ...

  4. 关于HashSet集合add元素

    HashSet集合add元素底层实现使用的是HashMap. 简单记忆:无论HashMap put元素还是HashSet add元素,都先调用hashCode()方法,若hashCode方法返回值不同 ...

  5. Python3:输出当前目录所有文件的第二种方式-walk()函数

    上一篇,我们讲了用递归的方式输入所有文件路径,其实os还提供了一个好用的方法-walk() 简单看一下: 中文大意就是: 返回的是一个三元tupple(dirpath, dirnames, filen ...

  6. 开源mall学习

    https://github.com/macrozheng/mall 学习知识点 1.Spring Security 2.@Aspect 3.logstash 4. es crud templete ...

  7. 2018-2019-2 20165325 《网络对抗技术》 Exp6 信息搜集与漏洞扫描

    2018-2019-2 20165325 <网络对抗技术> Exp6 信息搜集与漏洞扫描 实验内容(概要) 1 各种搜索技巧的应用: 2 DNS IP注册信息的查询: 3 基本的扫描技术 ...

  8. iis7.5做反向代理配置方法实例图文教程

    网络上好多开场的文章就说了好多的原理之类的这里我们直接开始配置.不过也要简单说下win下配置反向代理只有IIS7以上的版本才可以实现这个功能,在这里我们使用WINDOWS2008 R2来做为测试 20 ...

  9. selenium中webdriver识别class属性多个值中有空格的解决方案

    初学自动化测试,貌似大家十有八九都是用百度网站进行练手的,特此感谢百度. http://www.baidu.com 页面中主要就是搜索框和提交按钮: 输入框各元素属性:<input id=&qu ...

  10. windows 2012 如何设置取消禁拼ping

    直接上操作方法: 开始——搜索——高级安全windows防火墙——入站规则找到“文件和打印机共享(回显请求-ICMPv4-in)”右击……选择“启用规则”就OK了! 如果用到IPV6.后面也有 “文件 ...