题意

给定两个排列a和b,两种操作,交换b_i和b_j,询问a[l_a...r_a]和b[l_b...r_b]有多少个数相同。

分析

  • 由于给的是排列,保证b的每个数都有a的对应,构造数组c,c[i]表示b[i]在a数组中的位置。
  • 所以询问就变成询问c[l_b...r_b]中有多少个值域为[l_a...r_b]的数,树套树...
  • 内层的权值线段树动态开点一不小心就RE在第22个点,写了可回收的方式才过。
  • 代码10分钟,debug两节课...

代码

#include <bits/stdc++.h>
using namespace std;
const int N=2e5+50;
int n,m,tr[N],a[N],p[N],b[N],c[N],o,la,ra,lb,rb,xi,yi,x[N],y[N],c1,c2,sta[N],top;
struct VST{
#define mid (l+r)/2
int tot,sum[N*180],ls[N*180],rs[N*180];
//回收节点
int newnode(){
int p=top?sta[--top]:++tot;
ls[p]=rs[p]=sum[p]=0;
return p;
}
void update(int &x,int l,int r,int v,int add){
if(!x){
x=newnode();
}
sum[x]+=add;
if(l<r){
if(v<=mid){
update(ls[x],l,mid,v,add);
}else{
update(rs[x],mid+1,r,v,add);
}
}
//回收节点
if(!sum[x]){
sta[top++]=x;
x=0;
}
}
int query(int l,int r,int k){
if(k==0){
return 0;
}
if(r<=k){
int ans=0;
for(int i=1;i<=c1;i++){
ans-=sum[x[i]];
}
for(int i=1;i<=c2;i++){
ans+=sum[y[i]];
}
return ans;
}
if(k<=mid){
for(int i=1;i<=c1;i++){
x[i]=ls[x[i]];
}
for(int i=1;i<=c2;i++){
y[i]=ls[y[i]];
}
return query(l,mid,k);
}else{
int ans=0;
for(int i=1;i<=c1;i++){
ans-=sum[ls[x[i]]];
x[i]=rs[x[i]];
}
for(int i=1;i<=c2;i++){
ans+=sum[ls[y[i]]];
y[i]=rs[y[i]];
}
return ans+query(mid+1,r,k);
}
}
}ac;
struct BIT{
int lowbit(int x){
return x&(-x);
}
void update(int i,int x){
int k=c[i];
while(i<=n){
ac.update(tr[i],1,n,k,x);
i+=lowbit(i);
}
}
int query(int l,int r,int xi,int yi){
c1=c2=0;
for(int i=l-1;i;i-=lowbit(i)){
x[++c1]=tr[i];
}
for(int i=r;i;i-=lowbit(i)){
y[++c2]=tr[i];
}
int R=ac.query(1,n,yi);
c1=c2=0;
for(int i=l-1;i;i-=lowbit(i)){
x[++c1]=tr[i];
}
for(int i=r;i;i-=lowbit(i)){
y[++c2]=tr[i];
}
int L=ac.query(1,n,xi-1);
return R-L;
}
}bit;
int main(){
// freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&a[i]);
p[a[i]]=i;
}
for(int i=1;i<=n;i++){
scanf("%d",&b[i]);
c[i]=p[b[i]];
bit.update(i,1);
}
for(int i=1;i<=m;i++){
scanf("%d",&o);
if(o==1){
scanf("%d%d%d%d",&la,&ra,&lb,&rb);
int ans=bit.query(lb,rb,la,ra);
printf("%d\n",ans);
}else if(o==2){
scanf("%d%d",&xi,&yi);
bit.update(xi,-1);
bit.update(yi,-1);
swap(c[xi],c[yi]);
bit.update(xi,1);
bit.update(yi,1);
}
}
return 0;
}

Codeforces1093E_Intersection of Permutations的更多相关文章

  1. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  3. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  4. POJ2369 Permutations(置换的周期)

    链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS   Memory Limit: 65536K Total Submi ...

  5. Permutations

    Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...

  6. 【leetcode】Permutations

    题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...

  7. [leetcode] 47. Permutations II

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  8. Leetcode Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  9. one recursive approach for 3, hdu 1016 (with an improved version) , permutations, N-Queens puzzle 分类: hdoj 2015-07-19 16:49 86人阅读 评论(0) 收藏

    one recursive approach to solve hdu 1016, list all permutations, solve N-Queens puzzle. reference: t ...

随机推荐

  1. React 基于antd+video.js实现m3u8格式视频播放及实时切换

    文档连接地址(官网看起麻烦,看中文别人整理好的)https://blog.csdn.net/a0405221/article/details/80923090 React项目使用  安装依赖 npm ...

  2. Netty基础系列(4) --堆外内存与零拷贝详解

    前言 到目前为止,我们知道Nio当中有三个最最核心的组件,分别是:Selelctor,Channel,Buffer.在Netty基础系列(3) --彻底理解NIO 这一篇文章中只是进行了大致的介绍. ...

  3. c#小灶——常量、变量和赋值

    常量 常量很好理解,和变量相对,就是不会变的量.比如,1就是常量,3.6也是常量,‘a’也是常量,“aaaaa”也是常量,只是不同类型.这些都是表面上一眼就看出来的常量,还有一种表面上看不出来的常量, ...

  4. 使用bibtex为latex论文添加参考文献

    此文以引用Shannon的Prediction  and  Entropy  of Printed  English为例 1. bib文件 1.1 准备工作 进入Google Scholar 点击设置 ...

  5. 源码解读 Spring Boot Profiles

    前言 上文<一文掌握 Spring Boot Profiles> 是对 Spring Boot Profiles 的介绍和使用,因此本文将从源码角度探究 Spring Boot Profi ...

  6. Spring Boot之Profile--快速搞定多环境使用与切换

    Spring Profile是Spring3引入的概念,主要用在项目多环境运行的情况下,通过激活方式实现多环境切换,省去多环境切换时配置参数和文件的修改,并且Spring profile提供了多种激活 ...

  7. CSV Data Set Config 详细使用说明

    JMeter 5.1.1 CSV Data Set Config 场景一:线程组中设置:单线程执行1次 如上图所示:变量名称为空时JMeter默认把new 1.txt的文件首行作为变量名 再如:此时A ...

  8. 章节十六、3-TestNG方法和类注解

    一.Test Suite(测试套件) 我们通常认为一个testcase就是一个测试方法,但是会有很多的testcase,所以我们不可能把所有的testcase放到同一个测试类中,假如需要测试的页面有1 ...

  9. Go-项目结构和代码组织

    简介 做大量的输入,通过对比.借鉴,加上自己的经验,产出一个尽可能优的方案. 开源界优秀项目的结构示例 因为最新的 Go 版本已经使用 module 作为版本依赖,所以,所有项目的 vendor 我都 ...

  10. SpringBoot电商项目实战 — Redis实现分布式锁

    最近有小伙伴发消息说,在Springboot系列文第二篇,zookeeper是不是漏掉了?关于这个问题,其实我在写第二篇的时候已经考虑过,但基于本次系列文章是实战练习,在项目里你能看到Zookeepe ...