[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. 解决reverse改变原数组

    let arr =[1,2,3,4] console.log(arr) //[1,2,3,4] let arr2 = arr; console.log(arr2) //[4,3,2,1] consol ...

  2. python setuptools

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

  3. SAVEPOINT 标记

    create table duo(               --创建表格                v_xuhao number(3),                v_name varch ...

  4. c语言编译四大步

    -o: 指定生成后的文件名,后面跟指定的名称 四步:-E 预处理 > -S 编译 > -c 汇编 > 链接 -E: 表示预处理,生成文件为.i,会做宏(define)定义的展开.头文 ...

  5. JavaScript实现轮播图效果

    我又来了,同志们.老想你们了 捕获小可爱一枚. 下面进入正题:用JavaScript原生代码写轮播图效果. 具体效果就不多说了,网站上面的轮播效果我们都知晓.下面是展示代码 html代码: <d ...

  6. day 19 - 1 模块

    collections 模块 在内置数据类型(dict.list.set.tuple)的基础上,collections 模块还提供了几个额外的数据类型:Counter.deque.defaultdic ...

  7. 手写代码 - java.util.List 相关

    1-ArrayList 访问元素,不能使用ArrayList[0]形式!!!! 必须使用ArrayList.get(0);

  8. MailKit系列之附件分离

    本文主要谈谈实现思路,不提供完整代码 一.分离基础 1.MIME邮件的multipart类型 引用文章:https://blog.csdn.net/wangyu13476969128/article/ ...

  9. springboo+nginx测试负载均衡

    1:之前只是用nginx调用了boot_8044这一个服务,这次新建一个boot_8055服务,并在linux上启动: 两个boot我都是放在 /myprojects 目录下的(自定义,能启动就行) ...

  10. ext window嵌jsp页面自适应

    //定义window调用方法传入jsp所需参数function getWindow(obj,obj1,obj2,obj3,obj4,obj5,obj6,obj7,obj8,obj9){ Ext.def ...