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 ...
随机推荐
- 基于tp3.2的腾讯云短信验证码的实现
新手小白在公司要完成短信验证码注册功能,最初由于没有经验,网上的教程又不是很全,便参考着官方API文档,进行开发 直接进入正题:使用composer下载腾讯云短信接口(记得添加依赖).在项目目录下新建 ...
- SAP 修改MIRO变式
转自:http://blog.vsharing.com/SAP100/A799545.html
- alpine 镜像 java 日志中文问号乱码
0x00 前言 吾使用 alpine 作为基础镜像构建了 jdk8 镜像,为线上业务的 java 微服务架构提供支持,但是有容器运行的 java 服务中打印的日志中一旦出现中文,就会出现诸如以下的 ? ...
- Selenium+java - Page Object设计模式
前言 Page Object(页面对象)模式,是Selenium实战中最为流行,并且被自动化测试同学所熟悉和推崇的一种设计模式之一.在设计测试时,把页面元素定位和元素操作方法按照页面抽象出来,分离成一 ...
- js-EventLoop
1.浏览器事件环 eventLoop是由js的宿主环境(浏览器)来实现的 事件循环可以简单的描述为以下四个步骤 1.函数入栈,当Stack中执行到异步任务的时候,就将他丢给WebAPIs,接着执行同步 ...
- 纯数据结构Java实现(5/11)(Set&Map)
纯数据结构Java实现(5/11)(Set&Map) Set 和 Map 都是抽象或者高级数据结构,至于底层是采用树还是散列则根据需要而定. 可以细想一下 TreeMap/HashMap, T ...
- NMS的python实现
https://blog.csdn.net/a1103688841/article/details/89711120
- keras+ ctpn 原理流程图
- HDU 6299
题意略. 思路: 我们先把所有字符串处理成 ")))((((" 这样的形式.然后我们把这些字符串排个序,按照min(l,r)来排,小的在前,在我的代码中,l为 ( 的个数,r 为 ...
- Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower)
Leetcode之二分法专题-374. 猜数字大小(374. Guess Number Higher or Lower) 我们正在玩一个猜数字游戏. 游戏规则如下:我从 1 到 n 选择一个数字. 你 ...