分析

裸的二维数点,博主用树状数组套平衡树写的,顺便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. C语言第十一周作业

        这个作业属于哪个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/computer-scienceclass3-2018/ ...

  2. Win10使用自带功能创建系统映像备份时D盘被包含进去问题的解决

    在使用Windows10系统时,使用Windows自带功能创建系统映像备份文件时碰到了一些问题,所以在此记录一下. 创建系统映像文件的步骤,如下: 1.打开 控制面板 -> 选择 系统和安全 - ...

  3. Windows系统里Oracle 11g R2 Client(64bit)的下载与安装

    环境: windows10系统(64位) 最好先安装jre或jdk(此软件用来打开oracle自带的可视化操作界面,不装也没关系:可以安装plsql,或者直接用命令行操作) Oracle 11g 是仅 ...

  4. [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理)

    [Codeforces 1228E]Another Filling the Grid (排列组合+容斥原理) 题面 一个\(n \times n\)的格子,每个格子里可以填\([1,k]\)内的整数. ...

  5. [ERROR] Plugin org.apache.maven.plugins:maven-clean-plugin:2.5 or one of its dependencies could not be resolved: Cannot access nexus-aliyun (http://maven.aliyun.com/nexus/content/groups/public) in off

    这个错误是将work offline打勾引起的. 这个是离线工作模式,相当于断网,远程的jar会拉不下来.

  6. python 元组及操作

    # 元组是'不可变'的 list 使用小括号 创建后不允许修改 # 创建# t = ('a','b','c',1,2,3)# print(t)# print(type(t))# # 3# print( ...

  7. Python 多列数据存储

    zip()函数 zip函数可以把多个列表相加成一个tuple(元组) a = [1,2,3,4] b = [11,22,33,44] c = [111,222,333,444] A = list(zi ...

  8. viewset的使用的方法

    viewset的使用方法中是怎么区分的update和crate modelViewSet中设置了这个类之后,设置了类成员变量:queryset 和 seriazlier class,所以POST操作直 ...

  9. Linux部署项目 shell脚本启动 及 Centos7开放指定端口

    我们首先要在linux上安装好jdk   tomcat   mysql  这些基本环境,这些可以在楼主的  Linux入门   里面找到. linux部署spring项目 1. 右击项目,maven ...

  10. css厂商前缀

    在vue中写css,不要加厂商前缀,vue-cli会在打包时自动生成