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了这道题,但是感觉就算比赛再次遇到类似的题可能写不出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分治:
树套树:

明显cdq分治更快,而树套树的内存占用更少(不太科学)。
以本辣鸡的码力,如果要用线段树这些常数比较大的树应该要超时。。。
Educational Codeforces Round 56 (Rated for Div. 2) E(1093E) Intersection of Permutations (树套树,pb_ds)的更多相关文章
- Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))
题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...
- Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph 【规律 && DFS】
传送门:http://codeforces.com/contest/1093/problem/D D. Beautiful Graph time limit per test 2 seconds me ...
- Educational Codeforces Round 56 (Rated for Div. 2) ABCD
题目链接:https://codeforces.com/contest/1093 A. Dice Rolling 题意: 有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次 ...
- Educational Codeforces Round 56 (Rated for Div. 2) D
给你一个无向图 以及点的个数和边 每个节点只能用1 2 3 三个数字 求相邻 两个节点和为奇数 能否构成以及有多少种构成方法 #include<bits/stdc++.h> usin ...
- Educational Codeforces Round 56 (Rated for Div. 2)
涨rating啦.. 不过话说为什么有这么多数据结构题啊,难道是中国人出的? A - Dice Rolling 傻逼题,可以用一个三加一堆二或者用一堆二,那就直接.. #include<cstd ...
- Educational Codeforces Round 56 (Rated for Div. 2) F - Vasya and Array dp好题
F - Vasya and Array dp[ i ][ j ] 表示用了前 i 个数字并且最后一个数字是 j 的方案数. dp[ i ][ j ] = sumdp [i - 1 ][ j ], 这样 ...
- Educational Codeforces Round 56 (Rated for Div. 2) F. Vasya and Array
题意:长度为n的数组,数组中的每个元素的取值在1-k的范围内或者是-1,-1代表这个元素要自己选择一个1-k的数字去填写,然后要求填完的数组中不能出现连续长度大于len的情况,询问填空的方案数. 题解 ...
- Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph (二分图染色)
题意:有\(n\)个点,\(m\)条边的无向图,可以给每个点赋点权\({1,2,3}\),使得每个点连的奇偶不同,问有多少种方案,答案对\(998244353\)取模. 题解:要使得每个点所连的奇偶不 ...
- Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship
Problem Educational Codeforces Round 60 (Rated for Div. 2) - C. Magic Ship Time Limit: 2000 mSec P ...
随机推荐
- 删除rime输入法
mac: 首先将输入法从偏好设置-键盘-输入源中去除,添加系统的输入法. 然后执行命令: $ killall Squirrel $ sudo rm -Rf "/Library/Input M ...
- SecureCrt 连接Redhat linux
1.Vmware虚机设置网络模式为桥接Bridge.保证linux中能ping通windows,windows中也能ping通linux. 2.修改sshd_config文件,命令为:vi /etc/ ...
- Python - 批量改变文件名
import osimport sysimport datetime path = "E:\python_test"datename = '2016-02-11'a = datet ...
- 【Oracle】容易犯的错误和技巧集合
引言 此文记录日常开发中容易遇到的oracle编程误区和一些使用技巧,不定期更新. 1.sum(),max(),min(),avg()等函数会得到null值 declare n_num ): ...
- 教你们在cmd里运行打开游戏,效率很快的。喜欢吧?
第一步安装好的游戏 环境变量 变量值:把刚才复制好粘贴在里面,前面不用删除. 喜欢吧?这招非常好用.
- New Concept English three (55)
28w/m 45errors Recent developments in astronomy have made it possible to detect planets in our won M ...
- msyql acid特性
以下内容出自<高性能MySQL>第三版,了解事务的ACID及四种隔离级有助于我们更好的理解事务运作. 下面举一个银行应用是解释事务必要性的一个经典例子.假如一个银行的数据库有两张表:支票表 ...
- BZOJ5336: [TJOI2018]party
BZOJ5336: [TJOI2018]party https://lydsy.com/JudgeOnline/problem.php?id=5336 分析: 好题. 正常的思路是设\(f[i][j] ...
- UGUI transform
在编辑器中将UGUI对象挂在另一个物体上,UGUI预制体根对象位置调成零,调好位置后保存 由gameframework初始化,再挂到对应父对象上时,位置会有偏差,会更改锚点旋转等信息,需在OnWind ...
- why latches are considered bad?
A "latch" is different from a "Flip-Flop" in that a FF only changes its output i ...