bzoj3295树状数组套主席树

3295: [Cqoi2011]动态逆序对

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 5144   Solved: 1730
[ Submit][ Status][ Discuss]

Description

对于序列A,它的逆序对数定义为满足i<j,且Ai>Aj的数对(i,j)的个数。给1到n的一个排列,按照某种顺序依次删除m个元素,你的任务是在每次删除一个元素之前统计整个序列的逆序对数。

Input

输入第一行包含两个整数nm,即初始元素的个数和删除的元素个数。以下n行每行包含一个1到n之间的正整数,即初始排列。以下m行每行一个正整数,依次为每次删除的元素。
 

Output

 
输出包含m行,依次为删除每个元素之前,逆序对的个数。

Sample Input

5 4
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)。

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 160000
using 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 9
1 4 6 7 8 5 9 10 2 3
7 5 2 3 1 8 9 10 
28 
25
21
16
11
11
11
11
11
4
*/


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值