UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2
题目
https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=3141
题意
一个1到n的排列,每次随机删除一个,问删除前的逆序数
思路
综合考虑,对每个数点,令value为值,pos为位置,time为出现时间(总时间-消失时间),明显是统计value1 > value2, pos1 < pos2, time1 < time2的个数
首先对其中一个轴排序,比如value,这样在归并过程中,左子树的value总是小于右子树的,可以分治。
当左右子树包含哪些数点已经确定后,可以用自下而上的归并排序使得子树上的数点按照第二维相对有序,方便用尺取法统计子树之间的逆序数。
第三维通过树状数组进行压缩,加快统计速度。
注意仅仅统计左子树对右子树的影响,就会错过右子树中的数点出现的比较晚的情况。因此需要统计右子树对左子树的影响,此时注意别把同一时间出现的重复计数。
感想
1. 注意long long!!!
2. BIT的上限要>=n!
3. 注意统计影响完成后需要清空树状数组(区间大小已经减少了所以可以浪费地使用),此时不能直接用memset清空整个数组,时间会成为O(n2),超时。
代码
时间: 0.250s
时间复杂度O(cnlogn)
#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <queue>
#include <tuple>
#include <cassert> using namespace std; const int MAXN = int(4e5 + ); #define LEFT_CHILD(x) ((x) << 1)
#define RIGHT_CHILD(x) (((x) << 1) + 1)
#define FATHER(x) ((x) >> 1)
#define IS_LEFT_CHILD(x) (((x) & 1) == 0)
#define IS_RIGHT_CHILD(x) (((x) & 1) == 1)
#define BROTHER(x) ((x) ^ 1)
#define LOWBIT(x) ((x) & (-x)) #define LOCAL_DEBUG struct Node{
int value, pos, time;
}nodes[MAXN], tmpNodes[MAXN]; int timeCnt[MAXN * ];
long long revNum[MAXN];
int clearStack[MAXN];
int clearLen;
int n, m;
int bitLimit; int getHigherBit(int n) {
int x = ;
while (x < n) { x <<= ; }
return x;
} void update(int id) {
while (id <= bitLimit) {
if (timeCnt[id] == ) {
clearStack[clearLen++] = id;
}
timeCnt[id]++;
id += LOWBIT(id);
}
} void clearCnt() {
while (clearLen > ) {
timeCnt[clearStack[--clearLen]] = ;
}
} int countTimesSmaller(int id) {
if (id < )return ;
int sum = ;
int tmp = ;
while (id > ) {
sum += timeCnt[id];
id -= LOWBIT(id);
}
return sum;
} void merge_by_pos(int root_ind, int internal_l, int internal_r) {
int internal_mid = (internal_l + internal_r) >> ;
for (int i = internal_l; i <= internal_r; i++) {
tmpNodes[i] = nodes[i];
}
for (int i = internal_l, j = internal_mid + , ind = internal_l; ind <= internal_r; ) {
if (i > internal_mid) {
nodes[ind++] = tmpNodes[j++];
}
else if (j > internal_r) {
nodes[ind++] = tmpNodes[i++];
}
else if (tmpNodes[i].pos < tmpNodes[j].pos) {
nodes[ind++] = tmpNodes[i++];
}
else {
nodes[ind++] = tmpNodes[j++];
}
}
}
void cal(int root_ind, int internal_l, int internal_r) {
if (internal_l == internal_r)return;
int internal_mid = (internal_l + internal_r) >> ;
if(internal_l != internal_mid)cal(LEFT_CHILD(root_ind), internal_l, internal_mid);
if (internal_mid + != internal_r)cal(RIGHT_CHILD(root_ind), internal_mid + , internal_r);
// printf("L Node: %d[%d, %d] LC: %d[%d, %d], RC: %d[%d, %d]\n", root_ind, internal_l, internal_r, LEFT_CHILD(root_ind), internal_l, internal_mid, RIGHT_CHILD(root_ind), internal_mid + 1, internal_r);
for (int i = internal_l, j = internal_mid + ; i <= internal_mid; i++) {
while (j <= internal_r && nodes[i].pos > nodes[j].pos) {
update(nodes[j].time);
j++;
}
revNum[nodes[i].time] += countTimesSmaller(nodes[i].time);
// printf("L (%d, %d, %d): +%d\n", nodes[i].value, nodes[i].pos, nodes[i].time, countTimesSmaller(nodes[i].time));
}
clearCnt(); for (int i = internal_mid, j = internal_r; j > internal_mid; j--) {
while (i >= internal_l && nodes[i].pos > nodes[j].pos) {
update(nodes[i].time);
i--;
}
revNum[nodes[j].time] += countTimesSmaller(nodes[j].time - );
// printf("R (%d, %d, %d): +%d\n", nodes[j].value, nodes[j].pos, nodes[j].time, countTimesSmaller(nodes[j].time - 1));
}
clearCnt();
merge_by_pos(root_ind, internal_l, internal_r); } int main() {
#ifdef LOCAL_DEBUG
freopen("input.txt", "r", stdin);
freopen("output2.txt", "w", stdout);
#endif // LOCAL_DEBUG
for (int ti = ; scanf("%d%d", &n, &m) == ; ti++) {
bitLimit = getHigherBit(n);
for (int i = ; i <= n; i++) {
int tmp;
scanf("%d", &tmp);
nodes[tmp].value = tmp;
nodes[tmp].pos = i;
nodes[tmp].time = ;
}
for (int i = ; i <= m + ; i++) { revNum[i] = ; }
for (int i = ; i < m; i++) {
int tmp;
scanf("%d", &tmp);
nodes[tmp].time = m - i + ;
}
cal(, , n);
long long ans = ;
for (int i = ; i <= m + ; i++) { ans += revNum[i]; }
for (int i = ; i < m; i++) {
printf("%lld\n", ans);
ans -= revNum[m - i + ];
}
}
return ;
}
UVA 11990 `Dynamic'' Inversion CDQ分治, 归并排序, 树状数组, 尺取法, 三偏序统计 难度: 2的更多相关文章
- [APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树)
[APIO2019] [LOJ 3146] 路灯 (cdq分治或树状数组套线段树) 题面 略 分析 首先把一组询问(x,y)看成二维平面上的一个点,我们想办法用数据结构维护这个二维平面(注意根据题意这 ...
- bzoj 1176 cdq分治套树状数组
题面: 维护一个W*W的矩阵,初始值均为S.每次操作可以增加某格子的权值,或询问某子矩阵的总权值.修改操作数M<=160000,询问数Q<=10000,W<=2000000. Inp ...
- bzoj 4991 [Usaco2017 Feb]Why Did the Cow Cross the Road III(cdq分治,树状数组)
题目描述 Farmer John is continuing to ponder the issue of cows crossing the road through his farm, intro ...
- UVA 11990 ``Dynamic'' Inversion (序列分治)
26天以前做过的一道题,之前的做法是分治预处理,树套树在线修改,复杂度为O(nlogn+m*logn*logn),代码量较大. 本来想学习一下cdq分治的,看到论文上的凸包.斜率就暂时放一边了,只知道 ...
- 【BZOJ4285】使者 cdq分治+扫描线+树状数组
[BZOJ4285]使者 Description 公元 8192 年,人类进入星际大航海时代.在不懈的努力之下,人类占领了宇宙中的 n 个行星,并在这些行星之间修建了 n - 1 条星际航道,使得任意 ...
- HDU 5618 Jam's problem again(三维偏序,CDQ分治,树状数组,线段树)
Jam's problem again Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- BZOJ 2716 [Violet 3]天使玩偶 (CDQ分治、树状数组)
题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=2716 怎么KD树跑得都那么快啊..我写的CDQ分治被暴虐 做四遍CDQ分治,每次求一个 ...
- 【CJOJ2616】 【HZOI 2016】偏序 I(cdq分治,树状数组)
传送门 CJOJ Solution 考虑这是一个四维偏序对吧. 直接cdq套在一起,然后这题有两种实现方法(树状数组的更快!) 代码实现1(cdq+cdq+cdq) /* mail: mleautom ...
- bzoj2253纸箱堆叠(动态规划+cdq分治套树状数组)
Description P 工厂是一个生产纸箱的工厂.纸箱生产线在人工输入三个参数 n p a , 之后,即可自动化生产三边边长为 (a mod P,a^2 mod p,a^3 mod P) (a^4 ...
随机推荐
- git报错fatal: loose object ....(stored in .git/objects/....) is emtpy
主要是非正常关机.把.git给破坏了 参考https://stackoverflow.com/questions/12571557/fixing-a-corrupt-loose-object-as-a ...
- Python Appium 滑动、点击等操作
Python Appium 滑动.点击等操作 1.手机滑动-swipe # FileName : Tmall_App.py # Author : Adil # DateTime : 2018/3/25 ...
- Eclipse中打包插件Fat Jar的安装与使用
转自:https://www.cnblogs.com/wbyp/p/6222182.html Eclipse可以安装一个叫Fat Jar的插件,用这个插件打包非常方便,Fat Jar的功能非常 ...
- ubuntu12.04 安装CAJViewer-ubuntu(待解决)
ubuntu12.04测试通过 1.sudo apt-get install wine 2.unzip CAJViewer-ubuntu12.04版.zip 3.wine CAJVieweru.exe
- HDOJ-1806 ( Frequent values ) 线段树区间合并
http://acm.hdu.edu.cn/showproblem.php?pid=1806 线段树维护区间出现频率最高的出现次数.为了维护上者,需要维护线段前后缀的出现次数,当和其他线段在端点处的字 ...
- 架构探险笔记12-安全控制框架Shiro
什么是Shiro Shiro是Apache组织下的一款轻量级Java安全框架.Spring Security相对来说比较臃肿. 官网 Shiro提供的服务 1.Authentication(认证) 2 ...
- Div不用float布局
CSS代码 .wrapper1_4 { width: 100%; /* 也可以固定宽度 */ height: 26px; } .wrapper1_4 > .left { display: inl ...
- 微信小程序获取腾讯经纬度,得到具体地址
getCityNameOFLocation: function() { var that = this; wx.getLocation({ type: 'wgs84', // 默认为 wgs84 返回 ...
- AT2112 Non-redundant Drive
题目:https://www.luogu.org/problemnew/show/AT2112 对于这种找路径的就直接上点分治就好. 分治时,算出每一个点到分治重心的后能剩多少油,从分治重心走到每个点 ...
- 46. 47. Permutations and Permutations II 都适用(Java,字典序 + 非字典序排列)
解析: 一:非字典序(回溯法) 1)将第一个元素依次与所有元素进行交换: 2)交换后,可看作两部分:第一个元素及其后面的元素: 3)后面的元素又可以看作一个待排列的数组,递归,当剩余的部分只剩一个元素 ...