题意和分析在之前的链接中有: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)的更多相关文章

  1. Multidimensional Queries(二进制枚举+线段树+Educational Codeforces Round 56 (Rated for Div. 2))

    题目链接: https://codeforces.com/contest/1093/problem/G 题目: 题意: 在k维空间中有n个点,每次给你两种操作,一种是将某一个点的坐标改为另一个坐标,一 ...

  2. 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 ...

  3. Educational Codeforces Round 56 (Rated for Div. 2) ABCD

    题目链接:https://codeforces.com/contest/1093 A. Dice Rolling 题意: 有一个号数为2-7的骰子,现在有一个人他想扔到几就能扔到几,现在问需要扔多少次 ...

  4. Educational Codeforces Round 56 (Rated for Div. 2) D

    给你一个无向图 以及点的个数和边  每个节点只能用1 2 3 三个数字 求相邻 两个节点和为奇数   能否构成以及有多少种构成方法 #include<bits/stdc++.h> usin ...

  5. Educational Codeforces Round 56 (Rated for Div. 2)

    涨rating啦.. 不过话说为什么有这么多数据结构题啊,难道是中国人出的? A - Dice Rolling 傻逼题,可以用一个三加一堆二或者用一堆二,那就直接.. #include<cstd ...

  6. 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 ], 这样 ...

  7. Educational Codeforces Round 56 (Rated for Div. 2) F. Vasya and Array

    题意:长度为n的数组,数组中的每个元素的取值在1-k的范围内或者是-1,-1代表这个元素要自己选择一个1-k的数字去填写,然后要求填完的数组中不能出现连续长度大于len的情况,询问填空的方案数. 题解 ...

  8. Educational Codeforces Round 56 (Rated for Div. 2) D. Beautiful Graph (二分图染色)

    题意:有\(n\)个点,\(m\)条边的无向图,可以给每个点赋点权\({1,2,3}\),使得每个点连的奇偶不同,问有多少种方案,答案对\(998244353\)取模. 题解:要使得每个点所连的奇偶不 ...

  9. 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 ...

随机推荐

  1. js中scrollIntoView()的用法

    一. 什么是scrollIntoView scrollIntoView是一个与页面(容器)滚动相关的API 二. 如何调用 element.scrollIntoView() 参数默认为true 参数为 ...

  2. poj 2513 欧拉图/trie

    http://poj.org/problem?id=2513 Colored Sticks Time Limit: 5000MS   Memory Limit: 128000K Total Submi ...

  3. c++primer 第l六章编程练习答案

    6.11.1 #include<iostream> #include<cctype> int main() { using namespace std; char ch; ci ...

  4. uva11134 - Fabled Rooks(问题分解,贪心法)

    这道题非常好,不仅用到了把复杂问题分解为若干个熟悉的简单问题的方法,更是考察了对贪心法的理解和运用是否到位. 首先,如果直接在二维的棋盘上考虑怎么放不好弄,那么注意到x和y无关(因为两个车完全可以在同 ...

  5. 浅学soap--------3

    //person.wsdl 标签 <?xml version="1.0" ?> <definitions name="person" targ ...

  6. 通过ifreme实现文件上传

    模板页面添加ifreme <div style=' display: none;' >      <iframe name ="uploadResponse_attachm ...

  7. 如果两个人,两台电脑同时登录同一个帐号,同时对同一个账单提交,账单同时被服务器处理,那服务器应该先处理谁的,或者怎么规避这个问题。 非单点登录,重定向,stoken拦截器的问题

    方法一:给用户设置个状态 服务器端坐标记,比如数据库中增加一列,标识是否登陆,登录时先判断这个就行了,不过要考虑非正常退出的情况 http 方法二:在用户表里面 多加一个状态字段,登录成功 改变状态  ...

  8. 2017/2/22怎么判断mongodb服务已经启动了?

    打开任务管理器,看看服务下面是否有个MongoDB,有就表示成功

  9. AngularJs出现错误Error: [ng:areq]

    1.没有对应的控制器 2.有控制器但是路径没有配对

  10. JavaWeb框架_Struts2_(一)----->Struts2 框架入门

    1.  框架入门 2.1  Struts2简介 (1). Struts2是一种基于MVC模式的的轻量级Web开发框架. MVC模式:MVC全名是Model View Controller,是模型(mo ...