bzoj3295树状数组套主席树
来源:互联网 发布:电脑静默安装软件 编辑:程序博客网 时间:2024/06/07 01:04
3295: [Cqoi2011]动态逆序对
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5144 Solved: 1730
[Submit][Status][Discuss]
Description
对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数。给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序对数。
Input
输入第一行包含两个整数n和m,即初始元素的个数和删除的元素个数。以下n行每行包含一个1到n之间的正整数,即初始排列。以下m行每行一个正整数,依次为每次删除的元素。
Output
输出包含m行,依次为删除每个元素之前,逆序对的个数。
Sample Input
5 4
1
5
3
4
2
5
1
4
2
1
5
3
4
2
5
1
4
2
Sample Output
5
2
2
1
样例解释
(1,5,3,4,2)(1,3,4,2)(3,4,2)(3,2)(3)。
2
2
1
样例解释
(1,5,3,4,2)(1,3,4,2)(3,4,2)(3,2)(3)。
HINT
N<=100000 M<=50000
被精神污染搞得半死的我看到了这道题。突然感觉好开心啊,自己想出来了。
首先,静态逆序对求一遍,删除了一个数a,考虑这个数a对整体的影响。对于位置在a之前的所有数b,若b>a,答案就要减一,对于位置在a之后的所有数c,若c<a答案也要减一。换句话说,就是问区间1,i-1中有多少数比a[i]大,区间i+1,n中有多少数比a[i]小,直接上主席树就玩了。还有,修改的时候记得只减不加。
T-T没有过洛谷的数据(TLE一个点)但是bzoj过了,嚯嚯!
贴个代码。
#include<iostream>#include<cstdlib>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<cmath>#define maxn 160000using namespace std;int read(){char ch = getchar(); int x = 0, f = 1;while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}return x * f;}int lowbit(int x) {return x & -x;}int n, m, top, s[maxn], root[maxn], rank[maxn];long long ans;struct tree {int l, r, sum;tree() : l(0), r(0), sum(0) {} }t[maxn * 60];int getans(int x){int sum = 0;for(int i = x; i; i -= lowbit(i)) sum += s[i];return sum;}int update(int x) {for(int i = x;i <= n; i += lowbit(i)) ++s[i];}void add_tree(int &cur_p, int pos, int add, int L, int R){if(!cur_p) cur_p = ++top;t[cur_p].sum += add;if(L == R) return;int mid = L + R >> 1;if(pos <= mid) add_tree(t[cur_p].l, pos, add, L, mid);else add_tree(t[cur_p].r, pos, add, mid + 1, R);}void creat_tree(int p, int pos, int add) {for(int i = p;i <= n; i += lowbit(i)) add_tree(root[i], pos, add, 1, n);}void init(){n = read(); m = read();for(int i = 1;i <= n; ++i){int a = read();rank[a] = i;ans += getans(n) - getans(a);update(a);creat_tree(i, a, 1);}}int get_sum(int p, int pos, int L, int R){if(L == R) return t[p].sum;int mid = L + R >> 1;if(pos <= mid) return get_sum(t[p].l, pos, L, mid);else return t[t[p].l].sum + get_sum(t[p].r, pos, mid + 1, R);}int query(int p, int pos) {int sum = 0;for(int i = p; i; i -= lowbit(i)) sum += get_sum(root[i], pos, 1, n);return sum;}int ask_prefix(int x, int pos) {return query(pos, n) - query(pos, x);}int ask_suffix(int x, int pos) {return query(n, x) - query(pos, x);}void solve(){for(int i = 1;i <= m; ++i){printf("%lld\n", ans);int x = read(), pos = rank[x];ans -= ask_prefix(x, pos) + ask_suffix(x, pos);creat_tree(pos, x, -1);}}int main(){init();solve();return 0;}/*10 91 4 6 7 8 5 9 10 2 37 5 2 3 1 8 9 10 28 25211611111111114*/
阅读全文
0 0
- bzoj3295树状数组套主席树
- 【bzoj3295】[Cqoi2011]动态逆序对 树状数组套主席树
- 主席树&树状数组套主席树
- 【bzoj3295】 CQOI2011动态逆序对 树状数组+主席树
- bzoj3295: [Cqoi2011]动态逆序对 树状数组套线段树
- 【bzoj3295】【Cqoi2011】【动态逆序对】【树状数组套平衡树】
- ZOJ 2112 [树状数组套主席树]
- bzoj 1901(主席树套树状数组)
- bzoj1901 [ Zju2112 ] --树状数组套主席树
- bzoj2120树状数组套主席树解法
- [BZOJ3295][Cqoi2011]动态逆序对(树状数组套线段树||cdq分治)
- zoj 2112 (主席树,树状数组套线段树)
- zoj 2112 (主席树,树状数组套线段树)
- 树状数组套主席树(good)uva12345
- ZOJ 2112 Dynamic Rankings(树状数组套主席树)
- 1901: Zju2112 Dynamic Rankings 树状数组套主席树
- BZOJ 2141: 排队|树状数组套主席树
- 3295: [Cqoi2011]动态逆序对 树状数组套主席树
- 浅析HTML5的10大优势
- C库头文件(C99)
- js导出excel
- 图像各种变换 Miscellaneous Image Transformations
- HTML5中meta属性的应用
- bzoj3295树状数组套主席树
- Unity3D默认的快捷键
- 分布式配置管理平台disconf-初体验
- 基于Python检索系统(1)总体介绍
- Play framework自定义标签
- HDU6047Maximum Sequence
- easyui 实现动态表头
- java 多线程简单介绍
- -----dfs+思维 hdu 6060-RXD and dividing