题意和分析在之前的链接中有: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. Redis 存储机制

    Redis存储机制分成两种Snapshot和AOF.无论是那种机制,Redis都是将数据存储在内存中. Snapshot工作原理: 是将数据先存储在内存,然后当数据累计达到某些设定的伐值的时候,就会触 ...

  2. 使用JMeter建立接口测试

    [需求]某组机器是Android和iOS输入法接口服务器,有很多重要的接口,例如:升级,网络开关,热词等.现在有3台机器过保要下线,新申请了3台机器,需要验证一下这3台机器接口的正确性. [测试步骤] ...

  3. LeetCode OJ:Binary Tree Right Side View(右侧视角下的二叉树)

    Given a binary tree, imagine yourself standing on the right side of it, return the values of the nod ...

  4. 1110. Complete Binary Tree (25)

    Given a tree, you are supposed to tell if it is a complete binary tree. Input Specification: Each in ...

  5. 洛谷 P3904 三只小猪

    题目背景 你听说过三只小猪的故事吗?这是一个经典的故事.很久很久以前,有三只小猪.第一只小猪用稻草建的房子,第二个小猪用木棍建的房子,第三个小猪则使用砖做为材料.一只大灰狼想吃掉它们并吹倒了稻草和木棍 ...

  6. 使用dumpbin命令查看dll导出函数及重定向输出到文件【轉】

    查看dll导出函数,一般使用Viewdll等第三方工具. VS开发环境中,可以查看32位和64位的dll.具体使用方法如下: 1. 进入VS开发环境,然后Tools -> Visual stud ...

  7. GMchess Linux下的中国象棋游戏

    gmchess,一款Linux下的中国象棋程序

  8. 用Azure上Cognitive Service的Face API识别人脸

    Azure在China已经发布了Cognitive Service,包括人脸识别.计算机视觉识别和情绪识别等服务. 本文将介绍如何用Face API识别本地或URL的人脸. 一 创建Cognitive ...

  9. 一根Express Route同时支持ARM和ASM的VNET

    ARM模式的Azure管理模式在China Azure上已经正式落地了.今后在China Azure上应该主要以ARM的模式创建VM了. 并且目前Express Route也已经可以在ARM模式下创建 ...

  10. laravel 接收json串

    在做项目的时候发现 用平时的$request->all() 无法获取到请求值 然后这样解决了 但是还是不知道原因 学习源头: http://www.cnblogs.com/anjuncc/p/5 ...