首页 技术 正文
技术 2022年11月19日
0 收藏 581 点赞 2,337 浏览 1695 个字

题意和分析在之前的链接中有:https://www.cnblogs.com/pkgunboat/p/10160741.html

之前补题用三维偏序的cdq的分治A了这道题,但是感觉就算比赛再次遇到类似的题可能写不出cdq的代码。。。这次算是自己独立A的了。。。

如果这题不卡常的话,什么树套什么树都可以,为了节省空间以及提高效率,我在外层写一个树状数组,树状数组的每一个节点用pb_ds的红黑树维护(红黑大法好),这样查询效率比较高。

为了方便红黑树的区间操作,每颗树插入1个负无穷,1个正无穷。

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#define INF 0x3f3f3f3f
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/tree_policy.hpp>
#define lowbit(x) (x&(-x))
using namespace std;
using namespace __gnu_pbds;
const int maxn=200010;
int a[maxn],b[maxn],pos[maxn],mp[maxn];
__gnu_pbds::tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update> t[maxn];
__gnu_pbds::tree<int,null_type,less<int>,rb_tree_tag,tree_order_statistics_node_update>::iterator itl,itr;
int n,m,ql,qr;
int query(int x){
int ans;
itl=t[x].lower_bound(ql);
itr=t[x].upper_bound(qr);
return t[x].order_of_key(*itr)-t[x].order_of_key(*itl);}
int ask(int x){
int ans=0;
for(;x;x-=lowbit(x))
ans+=query(x);
return ans;
}
void build(int n){
for(int i=1;i<=n;i++){
for(int j=i-lowbit(i)+1;j<=i;j++)
t[i].insert(mp[j]);
t[i].insert(-INF);
t[i].insert(INF);
}}
void maintain(int x,int y){
if(y<0){
if(t[x].find(-y)!=t[x].end())
t[x].erase(-y);
}
else{
t[x].insert(y);
}
}
void add(int x,int y){
for(;x<=n;x+=lowbit(x))
maintain(x,y);
}
int main(){
int l,r,x,op;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%d",&x);
pos[x]=i;
}
for(int i=1;i<=n;i++){
scanf("%d",&x);
mp[i]=pos[x];
}
build(n);
while(m--){
scanf("%d",&op);
if(op==1){
scanf("%d%d%d%d",&ql,&qr,&l,&r);
printf("%d\n",ask(r)-ask(l-1));
}
else{
scanf("%d%d",&l,&r);
add(l,-mp[l]);
add(r,-mp[r]);
swap(mp[l],mp[r]);
add(l,mp[l]);
add(r,mp[r]);
}
}
}

代码比较:

cdq分治:                                        

Educational Codeforces Round 56 (Rated for Div. 2) E(1093E) Intersection of Permutations (树套树,pb_ds)      

树套树:

 Educational Codeforces Round 56 (Rated for Div. 2) E(1093E) Intersection of Permutations (树套树,pb_ds)

明显cdq分治更快,而树套树的内存占用更少(不太科学)。

以本辣鸡的码力,如果要用线段树这些常数比较大的树应该要超时。。。

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,985
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,501
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,345
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,128
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,763
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,839