[CF1093E]Intersection of Permutations
[CF1093E]Intersection of Permutations
题目大意:
给定两个长度为\(n(n\le2\times10^5)\)的排列\(A,B\)。\(m(m\le2\times10^5)\)次操作,操作分为以下两种:
- 询问有多少同时在\(A_{[x,y]}\)和\(B_{[l,r]}\)中出现的数。
- 交换\(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的更多相关文章
- CF1093E Intersection of Permutations 树状数组套权值线段树
\(\color{#0066ff}{ 题目描述 }\) 给定整数 \(n\) 和两个 \(1,\dots,n\) 的排列 \(a,b\). \(m\) 个操作,操作有两种: \(1\ l_a\ r_a ...
- [CF1093E]Intersection of Permutations:树套树+pbds
分析 裸的二维数点,博主用树状数组套平衡树写的,顺便pbds真好用. Update on 2018/12/20:再解释一下为什么是二维数点,第一维是\(la \leq i \leq ra\),第二维是 ...
- CF1093E Intersection of Permutations [分块 +bitset]
大家好, 我非常喜欢暴力数据结构, 于是就用分块A了此题 分块题,考虑前缀和 \(b_i\) 表示 bitset 即 \(0\) ~ $i $ 出现过的数字,然后考虑直接暴力复制块然后前缀和,修改也很 ...
- CF 1093 E. Intersection of Permutations
E. Intersection of Permutations 链接 题意: 给定两个序列,询问第一个排列的[l1,r1]和第二个排列[l2,r2]中有多少个共同的数,支持在第二个排列中交换两个数. ...
- 【cdq分治】【CF1093E】 Intersection of Permutations
传送门 果然前两天写完咕咕咕那个题的题解以后博客就开始咕咕咕了-- Description 给定整数 \(n\) 和两个 \(1~\sim~n\) 的排列 \(A,B\). \(m\) 个操作,操作有 ...
- CF 1093E Intersection of Permutations——CDQ分治
题目:http://codeforces.com/contest/1093/problem/E 只能想到转化成查询一个区间里值在一个范围里的数的个数…… 没有想到这样适合用主席树套树状数组维护.不过据 ...
- Codeforces 1093E Intersection of Permutations [CDQ分治]
洛谷 Codeforces 思路 一开始想到莫队+bitset,发现要T. 再想到分块+bitset,脑子一抽竟然直接开始写了,当然也T了. 最后发现这就是个裸的CDQ分治-- 发现\(a\)不变,可 ...
- E. Intersection of Permutations
题意:给两个排列,2种操作1,查询两个区间a和b一样的值个数,2,交换b的两个值 题解:树套树,先把a变成1到n的排列,对b做相同的变换,然后问题就变成了查询区间lb,rb中la到ra的个数,带修改可 ...
- CF1093:E. Intersection of Permutations(树状数组套主席树)
题意:给定长度为N的a数组,和b数组,a和b都是1到N的排列: 有两种操作,一种是询问[L1,R1],[L2,R2]:即问a数组的[L1,R1]区间和b数组的[L2,R2]区间出现了多少个相同的数字. ...
随机推荐
- SQL server存储过程,触发器,游标相关实例
use MySchool go alter proc P_stu as select AVG(StudentResult)as 平均分 from Result select * from Result ...
- Kafka(二)设计原理
1.持久性 kafka使用文件存储消息,这就直接决定kafka在性能上严重依赖文件系统的本身特性.且无论任何OS下,对文件系统本身的优化几乎没有可能.因为kafka是对日志进行append操作,因此磁 ...
- Exp3 免杀原理与实践 20164314 郭浏聿
一.实践内容 1.正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,加壳工具,使用shellcode编程 2.通过组合应用各种技术实现恶意代码免杀(0.5分) ...
- 函数语法:原生js判断某个元素是否有指定的class名的几种方法
var aLi = document.querySelectorAll('#tabs li'); for(var i = 0;i <p.length;i++){ //第一种方法,用classLi ...
- 命名空间的using声明
using声明具有如下的形式: using namespace::name; 一旦声明了上述语句,就可以直接访问命名空间中的名字: #include<iostream> //using声明 ...
- return *this和return this有什么区别?
return *this返回的是当前对象的克隆或者本身(若返回类型为A, 则是克隆, 若返回类型为A&, 则是本身 ). return this返回当前对象的地址(指向当前对象的指针). 转: ...
- aptitude与apt-get
aptitude 与 apt-get 一样,是 Debian 及其衍生系统中功能极其强大的包管理工具.与 apt-get 不同的是,aptitude 在处理依赖问题上更佳一些.举例来说,aptitud ...
- get 和free
1.ngx_pool_t ** ngx_get_pool()//use:getngx_pool_t **pool_address;ngx_pool_t *pool;pool_address = ngx ...
- php输出语句 echo print printf print_r var_dump sprintf
php的几种输出方式: echo 常用的输出语句,例如:echo 'helloworld!'; print() 输出语句,有返回值.例如:print('helloworld!'); 输出成功返回1,失 ...
- 网络流24题——骑士共存问题 luogu 3355
题目描述:这里 从这里开始,我们涉及到了一个新的问题:最小割问题 首先给出一些定义(本人根据定义自己口胡的): 一个流网络中的一个割是一个边集,使得割掉这些边集后源点与汇点不连通 而最小割问题就是一个 ...