分析

裸的二维数点,博主用树状数组套平衡树写的,顺便pbds真好用。

Update on 2018/12/20:再解释一下为什么是二维数点,第一维是\(la \leq i \leq ra\),第二维是\(lb \leq c_i \leq rb\),其中\(c_i\)表示\(a\)中第\(i\)个数在\(b\)中出现的位置。

代码

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <algorithm>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define lowbit(x) ((x)&(-(x)))
#define rin(i,a,b) for(int i=(a);i<=(b);i++)
#define rec(i,a,b) for(int i=(a);i>=(b);i--)
#define trav(i,a) for(int i=head[(a)];i;i=e[i].nxt)
typedef long long LL;
using namespace std;
using namespace __gnu_pbds; inline int read(){
int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
return x*f;
} const int MAXN=200005;
int n,m,a[MAXN],b[MAXN],c[MAXN],ic[MAXN],pos[MAXN];
tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> rbt[MAXN]; inline void Ins(int x,int kk){
for(int i=x;i<=n;i+=lowbit(i)) rbt[i].insert(kk);
} inline void Del(int x,int kk){
for(int i=x;i<=n;i+=lowbit(i)) rbt[i].erase(kk);
} inline int Ask(int x,int lb,int rb){
int ret=0;
for(int i=x;i;i-=lowbit(i)){
auto it=rbt[i].lower_bound(lb);
if(it==rbt[i].end()) continue;
int bg=*it;
it=rbt[i].upper_bound(rb);
if(it==rbt[i].begin()) continue;
int ed=*(--it);
ret+=rbt[i].order_of_key(ed)-rbt[i].order_of_key(bg)+1;
}
return ret;
} inline int Ask(int la,int ra,int lb,int rb){
return Ask(ra,lb,rb)-Ask(la-1,lb,rb);
} int main(){
n=read(),m=read();
rin(i,1,n) a[i]=read();
rin(i,1,n) b[i]=read(),pos[b[i]]=i;
rin(i,1,n) c[i]=pos[a[i]],ic[pos[a[i]]]=i;
rin(i,1,n) Ins(i,c[i]);
while(m--){
int opt=read();
if(opt==1){
int la=read(),ra=read(),lb=read(),rb=read();
printf("%d\n",Ask(la,ra,lb,rb));
}
else{
int x=read(),y=read();
Del(ic[x],x);Del(ic[y],y);
Ins(ic[x],y);Ins(ic[y],x);
swap(ic[x],ic[y]);
}
}
return 0;
}

[CF1093E]Intersection of Permutations:树套树+pbds的更多相关文章

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

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

  2. Educational Codeforces Round 56 (Rated for Div. 2) E(1093E) Intersection of Permutations (树套树,pb_ds)

    题意和分析在之前的链接中有:https://www.cnblogs.com/pkgunboat/p/10160741.html 之前补题用三维偏序的cdq的分治A了这道题,但是感觉就算比赛再次遇到类似 ...

  3. Codeforces 1093E Intersection of Permutations (CDQ分治+树状数组)

    题意:给你两个数组a和b,a,b都是一个n的全排列:有两种操作:一种是询问区间在数组a的区间[l1,r1]和数组b的区间[l2,r2]出现了多少相同的数字,另一种是交换数组b中x位置和y位置的数字. ...

  4. [CF1093E]Intersection of Permutations

    [CF1093E]Intersection of Permutations 题目大意: 给定两个长度为\(n(n\le2\times10^5)\)的排列\(A,B\).\(m(m\le2\times1 ...

  5. BZOJ 3110: [Zjoi2013]K大数查询 [树套树]

    3110: [Zjoi2013]K大数查询 Time Limit: 20 Sec  Memory Limit: 512 MBSubmit: 6050  Solved: 2007[Submit][Sta ...

  6. BZOJ4170 极光(CDQ分治 或 树套树)

    传送门 BZOJ上的题目没有题面-- [样例输入] 3 5 2 4 3 Query 2 2 Modify 1 3 Query 2 2 Modify 1 2 Query 1 1 [样例输出] 2 3 3 ...

  7. bzoj3262: 陌上花开(树套树)

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  8. bzoj3295: [Cqoi2011]动态逆序对(树套树)

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  9. BZOJ 3110 k大数查询 & 树套树

    题意: 有n个位置,每个位置可以看做一个集合,现在要求你实现一个数据结构支持以下功能: 1:在a-b的集合中插入一个数 2:询问a-b集合中所有元素的第k大. SOL: 调得火大! 李建说数据结构题能 ...

  10. BZOJ 3110 树套树 && 永久化标记

    感觉树套树是个非常高深的数据结构.从来没写过 #include <iostream> #include <cstdio> #include <algorithm> ...

随机推荐

  1. 【Qt开发】 V4L2_CAP_VIDEO_OVERLAY与V4L2_CAP_VIDEO_CAPTURE的区别

    原文地址http://www.cnblogs.com/tlm1992/archive/2012/06/12/2545772.html 这部分spec的内容没有全看懂,但是根据FSL的代码能知道这其中的 ...

  2. mysqlreport 安装&使用

    安装包:mysqlreport-3.5.tgz 下载地址:http://hackmysql.com/scripts/mysqlreport-3.5.tgz 安装办法:[root@nagios ~]# ...

  3. shell学习笔记2---执行Shell脚本(多种方法)

    在新进程中运行 Shell 脚本 1) 将 Shell 脚本作为程序运行 切换到脚本所在位置(还要给脚本加上可执行权限) [mozhiyan@localhost demo]$ ./test.sh #执 ...

  4. 小白学Python——Matplotlib 学习(3) 函数图形

    import matplotlib.pyplot as plt import numpy as np x = np.linspace(-1,1,50) y = 2*x + 1 plt.figure() ...

  5. TApplication,TForm,TControl,TComponent,TWinControl研究(博客索引)good

    TApplication,TForm,TControl,TComponent,TWinControl研究 http://blog.csdn.net/suiyunonghen/article/detai ...

  6. 剑指offer-二叉树中和为某一值的路径-python

    题目描述 输入一颗二叉树的跟节点和一个整数,打印出二叉树中结点值的和为输入整数的所有路径.路径定义为从树的根结点开始往下一直到叶结点所经过的结点形成一条路径.(注意: 在返回值的list中,数组长度大 ...

  7. getchar、putchar、puts、gets

    getchar(字符)  输入获取一个字符 putchar(字符)  输出控制台一个字符 scanf()格式化输入 printf() 格式化输出 gets(arr) 输入一个字符串给已经声明的数组ar ...

  8. ubuntu install xsltproc docbook-xsl docbook-xml

    问题一: $ makexsltproc --output phtml/ param.xsl ./pxml/mainbook.xmlmake: xsltproc: Command not foundma ...

  9. SPA(单页面应用)和MPA(多页面应用)

    话不多说,直接看图,一目了然

  10. Docker 安装、卸载、启动、停止

    1.1 查看当前系统的内核版本 查看当前系统的内核版本是否高于 3.10 英文文档:https://docs.docker.com/ 中文文档:https://docs.docker-cn.com/  ...