题意:给两个排列,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. js中获取当前浏览器类型

    本文为博主原创,转载请注明出处: 在应用POI进行导出时,先应用POI进行数据封装,将数据封装到Excel中,然后在进行download下载操作,从而完成 POI导出操作.由于在download操作时 ...

  2. 运行python脚本后台执行

    最近搞到了一台服务器,挂一个脚本刷刷河畔在线时间.脚本随便写了两下,能跑到什么时候就随缘了 https://blog.csdn.net/philosophyatmath/article/details ...

  3. hdu 3832 Earth Hour bfs

    Earth Hour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others) Prob ...

  4. Java Object类及其equals方法

    基本概念: Object类位于java.lang包中,java.lang包包含着Java最基础和核心的类,在编译时会自动导入: Object类是所有Java类的祖先.每个类都使用 Object 作为超 ...

  5. JDK10 新特性

    关于至此,我从大一下学习,以及大二上的巩固,这应该是SE部分的最后一章节内容,介绍一下jdk10的新特性 jdk在更新10之后,出现很多新特性,根据我所观看的视频,主要提及以下几点新特性 1.新增va ...

  6. [原][osg][osgEarth]EarthManipulator关于oe漫游器的handle部分解读以及修改(仿照谷歌,修改oe漫游器中focal(视角切换)功能 续 二)

    bool EarthManipulator::handle(const osgGA::GUIEventAdapter& ea, osgGA::GUIActionAdapter& aa) ...

  7. PostegreSQL模板数据库

    模板数据库 模板数据库就是创建新database时,PostgreSQL会基于模板数据库制作一份副本,其中会包含所有的数据库设置和数据文件. CREATE DATABASE 实际上是通过拷贝一个现有的 ...

  8. 你真的了解restful api吗?

    前言 在以前,一个网站的完成总是“all in one”,页面,数据,渲染全部在服务端完成,这样做的最大的弊端是后期维护,扩展极其痛苦,开发人员必须同时具备前后端知识.于是慢慢的后来兴起了前后端分离的 ...

  9. 00-python-内置函数笔记

    01.enumerate()函数用于将一个可遍历的数据对象(如 列表.元组或字符串)组合为一个索引序列,同时列出数据和数据包下标,一般用在for循环中 for i, element in enumer ...

  10. Python pymysql 增删改查封装

    关于pymysql 的增删改查,简单做个封装,方便后面使用直接拿来调用即可. 其中 增删改 的处理其实是一致的,本可以使用统一的方法,但是为了明显区分,这里分开来写了. 直接看代码就即可,如下: # ...