Codeforces1093E_Intersection of Permutations
题意
给定两个排列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的更多相关文章
- Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- POJ2369 Permutations(置换的周期)
链接:http://poj.org/problem?id=2369 Permutations Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- Permutations
Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...
- 【leetcode】Permutations
题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...
- [leetcode] 47. Permutations II
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- Leetcode Permutations
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 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 ...
随机推荐
- 数据结构之稀疏矩阵C++版
//只是简单的演示一下,这个实际运用视乎不怎么多,所以java版不再实现 /* 希疏矩阵应用于对数据的压缩,仅仅保留不为0的数据 稀疏矩阵的转置,可以由多种方式,下面演示的稍显简单,时间复杂度略高O( ...
- Spring Boot + Security + JWT 实现Token验证+多Provider——登录系统
首先呢就是需求: 1.账号.密码进行第一次登录,获得token,之后的每次请求都在请求头里加上这个token就不用带账号.密码或是session了. 2.用户有两种类型,具体表现在数据库中存用户信息时 ...
- 实现API优先设计的重要性和实现方式
应用API优先的方法意味着设计API时,使其具有一致性和适应性,无论应用于哪些开发项目.对API使用API描述语言(如OpenAPI)是关键,因为它有助于建立API与其他程序通信的枢纽,即使这些系 ...
- python练习题-1
1.输出正方形 x=input("请输入:") x=int(x) for i in range(0,x): if (i==0) or (i==x-1): print("* ...
- https理论及实践
什么是https协议? http协议以明文的方式在网络中传输,安全性难以保证,https在http协议的基础上加入SSL/TLS层.TLS是SSL协议的最新版本,SSL使用SSL数字证书在通信两端建立 ...
- AutoCAD二次开发(.Net)之创建图层Layer
//https://blog.csdn.net/qq_21489689?t=1[CommandMethod("CREATELY")] public void CreateLayer ...
- Goland_IDE的护眼、主题、字体等设置
Goland_IDE的护眼.主题.字体等设置 1.代码格式化 File->Settings->Tools->File Watchers->+->go fmt->将N ...
- 大白话5分钟带你走进人工智能-第35节神经网络之sklearn中的MLP实战(3)
本节的话我们开始讲解sklearn里面的实战: 先看下代码: from sklearn.neural_network import MLPClassifier X = [[0, 0], [1, 1]] ...
- 上个月,我赚了2W外快。。。
前段时间和室友一起给某个公司做了一个管理系统,每个人分2W多.这里和大家分享一下做完项目后一点点感受,想到啥就说点啥. 核心竞争力 两个月就挣了2W块,挣了我爸妈两个人一年的收入,每天还贼辛苦,披星戴 ...
- 搭建本地的idea激活服务器
前言 博主用的是idea这个IDE,因为最近idea官方打击第三方激活服务有些严重,所以我的idea经常处于今天可以用,到了明天就不能用的状态,所以,从idea激活的网站找到了本地的id ...