Codeforces 1093E Intersection of Permutations [CDQ分治]
思路
一开始想到莫队+bitset,发现要T。
再想到分块+bitset,脑子一抽竟然直接开始写了,当然也T了。
最后发现这就是个裸的CDQ分治……
发现\(a\)不变,可以处理出每个数在\(a\)中的位置\(pos\)。
然后处理出\(aa_i=pos_{b_i}\),交换时就是\(swap(aa_x,aa_y)\)。
把每个位置看成\((i,aa_i)\)的点,查询\(l1,r1,l2,r2\)时就是查以\((l2,l1)\)为左下角,\((r2,r1)\)为右上角的矩形里的点。
把修改看成加点,直接CDQ分治即可。
代码
#include<bits/stdc++.h>
clock_t t=clock();
namespace my_std{
using namespace std;
#define pii pair<int,int>
#define fir first
#define sec second
#define MP make_pair
#define rep(i,x,y) for (int i=(x);i<=(y);i++)
#define drep(i,x,y) for (int i=(x);i>=(y);i--)
#define go(x) for (int i=head[x];i;i=edge[i].nxt)
#define templ template<typename T>
#define sz 202020
#define S 500
typedef long long ll;
typedef double db;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
templ inline T rnd(T l,T r) {return uniform_int_distribution<T>(l,r)(rng);}
templ inline bool chkmax(T &x,T y){return x<y?x=y,1:0;}
templ inline bool chkmin(T &x,T y){return x>y?x=y,1:0;}
templ inline void read(T& t)
{
t=0;char f=0,ch=getchar();double d=0.1;
while(ch>'9'||ch<'0') f|=(ch=='-'),ch=getchar();
while(ch<='9'&&ch>='0') t=t*10+ch-48,ch=getchar();
if(ch=='.'){ch=getchar();while(ch<='9'&&ch>='0') t+=d*(ch^48),d*=0.1,ch=getchar();}
t=(f?-t:t);
}
template<typename T,typename... Args>inline void read(T& t,Args&... args){read(t); read(args...);}
char sr[1<<21],z[20];int C=-1,Z=0;
inline void Ot(){fwrite(sr,1,C+1,stdout),C=-1;}
inline void print(register int x)
{
if(C>1<<20)Ot();if(x<0)sr[++C]='-',x=-x;
while(z[++Z]=x%10+48,x/=10);
while(sr[++C]=z[Z],--Z);sr[++C]='\n';
}
void file()
{
#ifndef ONLINE_JUDGE
freopen("a.in","r",stdin);
#endif
}
inline void chktime()
{
#ifndef ONLINE_JUDGE
cout<<(clock()-t)/1000.0<<'\n';
#endif
}
#ifdef mod
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x%mod) if (y&1) ret=ret*x%mod;return ret;}
ll inv(ll x){return ksm(x,mod-2);}
#else
ll ksm(ll x,int y){ll ret=1;for (;y;y>>=1,x=x*x) if (y&1) ret=ret*x;return ret;}
#endif
// inline ll mul(ll a,ll b){ll d=(ll)(a*(double)b/mod+0.5);ll ret=a*b-d*mod;if (ret<0) ret+=mod;return ret;}
}
using namespace my_std;
int n,m;
int pos[sz],a[sz];
struct hh
{
int type;
int p,pos,v;
int r,x,y,val,id;
}q[sz<<3];
inline bool cmp(const hh &x,const hh &y)
{
if (x.type!=y.type) return x.type<y.type;
if (x.type==1) return x.p<y.p;
return x.r<y.r;
}
int cnt;
void adda(int x,int pos){if (a[x]) q[++cnt]=(hh){1,x,a[x],-1,0,0,0,0,0};q[++cnt]=(hh){1,x,pos,1,0,0,0,0,0};}
void addq(int l1,int r1,int l2,int r2,int id){q[++cnt]=(hh){0,0,0,0,l2-1,l1,r1,-1,id};q[++cnt]=(hh){0,0,0,0,r2,l1,r1,1,id};}
int Ans[sz];
int tr[sz];
void add(int x,int y){while (x<=n) tr[x]+=y,x+=(x&(-x));}
int query(int x){int ret=0;while (x) ret+=tr[x],x-=(x&(-x));return ret;}
void solve(int l,int r)
{
if (l==r) return;
int mid=(l+r)>>1;
solve(l,mid);solve(mid+1,r);
int p=l-1;
rep(i,mid+1,r) if (q[i].type==0)
{
while (p<mid)
{
if (q[p+1].type==0) ++p;
else if (q[p+1].p<=q[i].r)
{
++p;
add(q[p].pos,q[p].v);
}
else break;
}
Ans[q[i].id]+=q[i].val*(query(q[i].y)-query(q[i].x-1));
}
rep(i,l,p) if (q[i].type) add(q[i].pos,-q[i].v);
sort(q+l,q+r+1,cmp);
}
int main()
{
file();
read(n,m);
int x,y,z,l1,l2,r1,r2;
rep(i,1,n) read(x),pos[x]=i;
rep(i,1,n) read(x),adda(i,pos[x]),a[i]=pos[x];
int c=0;
rep(i,1,m)
{
read(z);
if (z==1) read(l1,r1,l2,r2),addq(l1,r1,l2,r2,++c);
else read(x,y),adda(x,a[y]),adda(y,a[x]),swap(a[x],a[y]);
}
solve(1,cnt);
rep(i,1,c) printf("%d\n",Ans[i]);
return 0;
}
Codeforces 1093E Intersection of Permutations [CDQ分治]的更多相关文章
- Codeforces 1093E Intersection of Permutations (CDQ分治+树状数组)
题意:给你两个数组a和b,a,b都是一个n的全排列:有两种操作:一种是询问区间在数组a的区间[l1,r1]和数组b的区间[l2,r2]出现了多少相同的数字,另一种是交换数组b中x位置和y位置的数字. ...
- CF 1093E Intersection of Permutations——CDQ分治
题目:http://codeforces.com/contest/1093/problem/E 只能想到转化成查询一个区间里值在一个范围里的数的个数…… 没有想到这样适合用主席树套树状数组维护.不过据 ...
- 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了这道题,但是感觉就算比赛再次遇到类似 ...
- Codeforces 848C Goodbye Souvenir(CDQ 分治)
题面传送门 考虑记录每个点的前驱 \(pre_x\),显然答案为 \(\sum\limits_{i=l}^{r} i-pre_i (pre_i \geq l)\) 我们建立一个平面直角坐标系,\(x\ ...
- 【cdq分治】【CF1093E】 Intersection of Permutations
传送门 果然前两天写完咕咕咕那个题的题解以后博客就开始咕咕咕了-- Description 给定整数 \(n\) 和两个 \(1~\sim~n\) 的排列 \(A,B\). \(m\) 个操作,操作有 ...
- Codeforces 1045G AI robots [CDQ分治]
洛谷 Codeforces 简单的CDQ分治题. 由于对话要求互相看见,无法简单地用树套树切掉,考虑CDQ分治. 按视野从大到小排序,这样只要右边能看见左边就可以保证互相看见. 发现\(K\)固定,那 ...
- Codeforces 848C Goodbye Souvenir [CDQ分治,二维数点]
洛谷 Codeforces 这题我写了四种做法-- 思路 不管做法怎样,思路都是一样的. 好吧,其实不一样,有细微的差别. 第一种 考虑位置\(x\)对区间\([l,r]\)有\(\pm x\)的贡献 ...
- Codeforces 526F Pudding Monsters - CDQ分治 - 桶排序
In this problem you will meet the simplified model of game Pudding Monsters. An important process in ...
- Codeforces 848C (cdq分治)
Codeforces 848C Goodbye Souvenir Problem : 给一个长度为n的序列,有q个询问.一种询问是修改某个位置的数,另一种询问是询问一段区间,对于每一种值出现的最右端点 ...
随机推荐
- java使用类数组 报错Exception in thread "main" java.lang.NullPointerException
源代码如下: Point[] points=new Point[n];//Point是一个类 for(int i=0;i<n;i++) { System.out.print("请输入x ...
- luogu P4899 [IOI2018] werewolf 狼火
传送门 首先很显然,从人形起点出发能到的点和狼形能到终点的点都是一个联通块,如果能从起点到终点则说明这两个联通块有交 这个时候可以请出我们的克鲁斯卡尔重构树,即对原图分别建两棵重构树,一棵边权为两端点 ...
- java时间计算
- mvc中异常页面的设置
参考原文: http://blog.csdn.net/zjlovety/article/details/48734791 这种异常处理就是说,在mvc发生未处理异常时,对用户展示的页面. 第一种能被m ...
- Django学习手册 - csrf
CSRF csrf原理 无csrf时存在隐患 Form提交 Ajax提交 默认为全局都csrf Form表单提交方式: <div> <form action="/login ...
- 20165234 预备作业2 学习基础和C语言基础调查
学习基础和C语言基础调查 一.技能学习经验及体会 你有什么技能比大多人(超过90%以上)更好? 看到这个问题,我仔细想了想,好像的确没有什么特别出众的技能,但是我想到了许多我个人的爱好. 我从小喜欢五 ...
- Selenium: Trying to log in with cookies and get the errorMessage - “Can only set cookies for current domain” or "Unable to set Cookie"
from selenium import webdriver driver = webdriver.PhantomJS(executable_path='G:/OpenSources/phantomj ...
- Jquery的一些常见用法
谨以此文怀念我们大学的时光,Jquery3.4.0下载 https://code.jquery.com/jquery-3.4.0.min.js ⒈常用方法 $("#div1").h ...
- 【VMware vSphere】ESXi系统开启SSH协议
写在前面: SSH为建立在应用层基础上的安全协议,是目前较为可靠,专为远程登录会话和其他网络服务提供安全性的协议.感兴趣的话,可以百度了解 在这里,我们需要将S ...
- 第八章 让Bootstrap轮播插件carousel支持左右滑动手势的三种方法
因为最近开发的项目涉及到移动设备上的 HTML5 开发,其中需要实现轮播效果.然后最快捷的方式,你知道的(Bootstrap),然后原生的 Bootstrap 的 carousel.js 插件并没有支 ...