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 ...
随机推荐
- Windows 10 Certified with Oracle E-Business Suite
Microsoft Windows 10 (32-bit and 64-bit) is certified as a desktop client operating system for end-u ...
- SQL group by底层原理——本质是排序,可以利用索引事先排好序
转自:http://blog.csdn.net/caomiao2006/article/details/52140993 由于GROUP BY 实际上也同样会进行排序操作,而且与ORDER BY 相比 ...
- 利用wtl的CDialogResize自动调整atl ActiveX控件布局
前言 利用atl 开发activex控件时,如果使用atl复合控件时,acitvex控件上的界面元素不会自动改变大小,如果自己在OnSize中处理每个子控件的布局是一件非常麻烦的事,我们可以借助wtl ...
- 【.Net 】Json和Xml解析
引言 Json和Xml是现在跨平台传输数据的主流格式,关于它们的解析,网上资料很多,我稍作整理,写成一个小demo,方便日后使用. JSON解析 能进行json解析的类库有很多,例如Ja ...
- MonoBehavior lifecycle
awake 只调用一次, awake在所有obj都初始化之后被调用. 用途: 初始化游戏状态 设置脚本间的引用 ### ExecuteInEditMode 编辑模式下 ``` 这个模式下,脚本编译,会 ...
- LeetCode Reverse Words in a String III
原题链接在这里:https://leetcode.com/problems/reverse-words-in-a-string-iii/#/description 题目: Given a string ...
- mongodb所在目录空间不足解决方法
1.原理是将目录/home/aa软连接到/usr/lib/下,以后从/usr/lib下读取的内容其实都是放在/home/aa下. 建议不要大范围动/usr下的内容,咋着也是属于系统目录,可能会对已装软 ...
- ECMAScript 2016(ES7) 知多少
ECMAScript 2016(ES7) 知多少 1. 数组方法 Array.prototype.includes(value : any) : boolean 2. 幂运算符 x ** y 扩展阅读 ...
- PostgreSQL 监控数据库活动
监控数据库活动 1. 标准Unix 工具 [root@mysqlhq ~]# ps auxww | grep ^postgrespostgres 12106 0.0 0.0 340060 15064 ...
- java中如何将OutputStream转换为InputStream
在不需要文件生成的情况下,直接将输出流转换成输入流.可使用下面的三种方法: 如果你曾经使用java IO编程,你会很快碰到这种情况,某个类在OutputStream上创建数据而你需要将它发送给某个需要 ...