题意:给两个排列,2种操作1,查询两个区间a和b一样的值个数,2,交换b的两个值

题解:树套树,先把a变成1到n的排列,对b做相同的变换,然后问题就变成了查询区间lb,rb中la到ra的个数,带修改可以树状数组套主席树,需要内存回收

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 998244353
#define ld long double
//#define C 0.5772156649
//#define ls l,m,rt<<1
//#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std; const ull ba=233;
const db eps=1e-8;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200000+10,maxn=200000+10,inf=0x3f3f3f3f; int n,m;
int q[N*150],top;
struct bit_seg{
int root[N],ls[N*150],rs[N*150],sum[N*150],res;
bit_seg(){res=0;}
void update(int &o,int pos,int v,int l,int r)
{
if(!o)
{
if(top)o=q[top--];
else o=++res;
}
sum[o]+=v;
if(l==r)return ;
int m=(l+r)>>1;
if(pos<=m)update(ls[o],pos,v,l,m);
else update(rs[o],pos,v,m+1,r);
if(!sum[o])q[++top]=o,o=0;
}
int query(int L,int R,int o,int l,int r)
{
if(!o)return 0;
if(L<=l&&r<=R)return sum[o];
int m=(l+r)>>1,ans=0;
if(L<=m)ans+=query(L,R,ls[o],l,m);
if(m<R)ans+=query(L,R,rs[o],m+1,r);
return ans;
}
int bitquery(int la,int ra,int lb,int rb)
{
int ans=0;
for(int i=rb;i;i-=i&(-i))ans+=query(la,ra,root[i],1,n);
for(int i=lb-1;i;i-=i&(-i))ans-=query(la,ra,root[i],1,n);
return ans;
}
void bitupdate(int i,int pos,int v)
{
for(;i<=n;i+=i&(-i))update(root[i],pos,v,1,n);
}
}s;
int a[N],b[N],c[N];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),c[a[i]]=i;
for(int i=1;i<=n;i++)scanf("%d",&b[i]),b[i]=c[b[i]];
for(int i=1;i<=n;i++)s.bitupdate(i,b[i],1);
for(int i=1;i<=m;i++)
{
int op;scanf("%d",&op);
if(op==1)
{
int la,ra,lb,rb;scanf("%d%d%d%d",&la,&ra,&lb,&rb);
printf("%d\n",s.bitquery(la,ra,lb,rb));
}
else
{
int x,y;scanf("%d%d",&x,&y);
s.bitupdate(x,b[x],-1);s.bitupdate(y,b[y],-1);
swap(b[x],b[y]);
s.bitupdate(x,b[x],1),s.bitupdate(y,b[y],1);
}
}
return 0;
}
/******************** ********************/

平板电视的精简版

//#pragma GCC optimize(2)
//#pragma GCC optimize(3)
//#pragma GCC optimize(4)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#include <bits/extc++.h>
#define fi first
#define se second
#define db double
#define mp make_pair
#define pb push_back
#define pi acos(-1.0)
#define ll long long
#define vi vector<int>
#define mod 1000000007
#define ld long double
//#define C 0.5772156649
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#define pll pair<ll,ll>
#define pil pair<int,ll>
#define pli pair<ll,int>
#define pii pair<int,int>
#define ull unsigned long long
//#define base 1000000000000000000
#define fin freopen("a.txt","r",stdin)
#define fout freopen("a.txt","w",stdout)
#define fio ios::sync_with_stdio(false);cin.tie(0)
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
inline void sub(ll &a,ll b){a-=b;if(a<0)a+=mod;}
inline void add(ll &a,ll b){a+=b;if(a>=mod)a-=mod;}
template<typename T>inline T const& MAX(T const &a,T const &b){return a>b?a:b;}
template<typename T>inline T const& MIN(T const &a,T const &b){return a<b?a:b;}
inline ll qp(ll a,ll b){ll ans=1;while(b){if(b&1)ans=ans*a%mod;a=a*a%mod,b>>=1;}return ans;}
inline ll qp(ll a,ll b,ll c){ll ans=1;while(b){if(b&1)ans=ans*a%c;a=a*a%c,b>>=1;}return ans;} using namespace std;
using namespace __gnu_pbds; const ull ba=233;
const db eps=1e-6;
const ll INF=0x3f3f3f3f3f3f3f3f;
const int N=200000+10,maxn=200000+10,inf=0x3f3f3f3f; template <class T>
using Tree = tree<T, null_type, std::less<T>, rb_tree_tag,tree_order_statistics_node_update>;
int a[N],b[N],c[N],n,m;
struct bit{
Tree<int>t[N];
void update(int i,int pos,int v)
{
for(;i<=n;i+=i&(-i))
{
if(v==1)t[i].insert(pos);
else t[i].erase(pos);
}
}
int query(int la,int ra,int lb,int rb)
{
int ans=0;
for(int i=rb;i;i-=i&(-i))ans+=t[i].order_of_key(ra+1)-t[i].order_of_key(la);
for(int i=lb-1;i;i-=i&(-i))ans-=t[i].order_of_key(ra+1)-t[i].order_of_key(la);
return ans;
}
}bi;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%d",&a[i]),c[a[i]]=i;
for(int i=1;i<=n;i++)scanf("%d",&b[i]),b[i]=c[b[i]],bi.update(i,b[i],1);
for(int i=1;i<=m;i++)
{
int op;scanf("%d",&op);
if(op==1)
{
int la,ra,lb,rb;scanf("%d%d%d%d",&la,&ra,&lb,&rb);
printf("%d\n",bi.query(la,ra,lb,rb));
}
else
{
int x,y;scanf("%d%d",&x,&y);
bi.update(x,b[x],-1),bi.update(y,b[y],-1);
swap(b[x],b[y]);
bi.update(x,b[x],1),bi.update(y,b[y],1);
}
}
return 0;
}
/******************** ********************/

E. Intersection of Permutations的更多相关文章

  1. [CF1093E]Intersection of Permutations

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

  2. CF 1093 E. Intersection of Permutations

    E. Intersection of Permutations 链接 题意: 给定两个序列,询问第一个排列的[l1,r1]和第二个排列[l2,r2]中有多少个共同的数,支持在第二个排列中交换两个数. ...

  3. Codeforces 1093E Intersection of Permutations [CDQ分治]

    洛谷 Codeforces 思路 一开始想到莫队+bitset,发现要T. 再想到分块+bitset,脑子一抽竟然直接开始写了,当然也T了. 最后发现这就是个裸的CDQ分治-- 发现\(a\)不变,可 ...

  4. CF1093:E. Intersection of Permutations(树状数组套主席树)

    题意:给定长度为N的a数组,和b数组,a和b都是1到N的排列: 有两种操作,一种是询问[L1,R1],[L2,R2]:即问a数组的[L1,R1]区间和b数组的[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. 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了这道题,但是感觉就算比赛再次遇到类似 ...

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

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

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

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

随机推荐

  1. 集合00_Java集合框架

    集合类概述 1.继承树 2.集合和数组 区别如下: 数组可以存储基本数据类型,也可以存储引用类型:而集合只能存储引用类型(比如存储int,它会自动装箱成Integer) 数组长度固定,集合长度可变 3 ...

  2. 题解——loj6281 数列分块入门5 (分块)

    分块 若块内最大值为0或1,则不用再开方 然后暴力修改 可以证明,如果开方后向下取整,则最多开方4次一个数就会变成0或1 #include <cstdio> #include <cm ...

  3. Redis 应用:缓存

    使用Redis做预定库存缓存功能 缓存是在业务层做的,准确讲应该是在MVC模型中Model的ORM里面 PHP项目的缓存从以前的APC缓存逐渐切换到Redis中,并且根据Redis所支持的数据结构做了 ...

  4. CSS-形变 动画 表格

    一.形变 /*1.形变参考点: 三轴交界点*/ transform-origin: x轴坐标 y轴坐标; ​ /*2.旋转 rotate deg*/ transform: rotate(720deg) ...

  5. 常用模块(json/pickle/shelve/XML)

    一.json模块(重点) 一种跨平台的数据格式 也属于序列化的一种方式 介绍模块之前,三个问题: 序列化是什么? 我们把对象(变量)从内存中变成可存储或传输的过程称之为序列化. 反序列化又是什么? 将 ...

  6. geoserver源码学习与扩展——自动发布shapefile图层

    geoserver通过工作空间Workspace-数据源DataStore-图层Layer管理地理数据,这些信息都通过Catalog进行组织和管理,要完成自动发布只需要在Catalog中增加相应的信息 ...

  7. 【BZOJ】1831: [AHOI2008]逆序对

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1831 考虑$-1$的位置上填写的数字一定是不降的. 令${f[i][j]}$表示$DP$到 ...

  8. mysql基本知识总结

    第一天 create database act_web character set utf8; : 创建数据库并设立编码(命令中是不允许使用“-”的) '; :创建用户并设立密码 grant all ...

  9. java正则常用记录

    1.   java中 字符串的某个字母是否有某个指定字符N  : for(int i=0;i<temp.length();i++){ if( temp.get(i).substring(0,1) ...

  10. Eclipse中使用MySql遇到:Loading class `com.mysql.jdbc.Driver'. This is deprecated. The new driver class is `com.mysql.cj.jdbc.Driver'. The driver is automatically registered via the SPI and manual loading o

    在Eclipse中使用MySQL遇到了点小问题 如果对Eclipse中配置MySql还有疑问的可以参考一下这篇博客:https://blog.csdn.net/qq_38247544/article/ ...