codeforces387E、George and Cards(二分+树状数组)

                                                        George and Cards

George is a cat, so he loves playing very much.

Vitaly put n cards in a row in front of George. Each card has one integer written on it. All cards had distinct numbers written on them. Let's number the cards from the left to the right with integers from 1 to n. Then the i-th card from the left contains number pi (1 ≤ pi ≤ n).

Vitaly wants the row to have exactly k cards left. He also wants the i-th card from left to have number bi written on it. Vitaly gave a task to George, to get the required sequence of cards using the remove operation n - k times.

In one remove operation George can choose w (1 ≤ ww is not greater than the current number of cards in the row) contiguous cards (contiguous subsegment of cards). Let's denote the numbers written on these card as x1, x2, ..., xw (from the left to the right). After that, George can remove the card xi, such that xi ≤ xj for each j (1 ≤ j ≤ w). After the described operation George gets w pieces of sausage.

George wondered: what maximum number of pieces of sausage will he get in total if he reaches his goal and acts optimally well? Help George, find an answer to his question!

Input

The first line contains integers n and k (1 ≤ k ≤ n ≤ 106) — the initial and the final number of cards.

The second line contains n distinct space-separated integers p1, p2, ..., pn (1 ≤ pi ≤ n) — the initial row of cards.

The third line contains k space-separated integers b1, b2, ..., bk — the row of cards that you need to get. It is guaranteed that it's possible to obtain the given row by using the remove operation for n - k times.

Output

Print a single integer — the maximum number of pieces of sausage that George can get if he acts optimally well.

Examples

Input

3 2
2 1 3
1 3

Output

1

Input

10 5
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10

Output

30

 

一、原题地址

点我传送

 

二、大致题意

给出n个数的序列a[ ],现在有一个长度为m的序列b[ ],现在想要把序列a[ ]经过x次操作后转化为序列b[ ].

在每一次操作中,可以先选取一个区间[l, r],在这个区间内找到一个最小值然后删除。在每一次操作中,能获得的能量值为选取的区间长度,现在询问最后能获得的最大的能量值和是多少。

 

三、思路

每一次的删除操作必定是从最小的开始删除的。注意到题目中序列里所有的数字都是唯一的,所以枚举从1到n的数字,若当前的数字 i 是存在于序列b[]中的,那么就是需要保留的。否则若不在b[ ]中则说明这是一个需要删除的数字,我们在寻找这个数字周边的最小值时,可以发现因为我们是从小到大枚举的所以那些应该删除的比较小的数字已经删除掉了,而真正需要考虑的是小于当前这个数 j 的那些需要被保存的数字。

为了记录那些需要被保存的数字,用一个set来记录他们所在的位置,而只有枚举到了这个数我们才将他的位置加入set,表示在后面的查询中它们是需要被考虑到的。

而在寻找到区间的长度之后,显然是需要减去那些已经被删掉的数字的,这一点用树状数组来区间查询一下删除数字的个数再减掉就可以了。

 

四、代码

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<set>
using namespace std;
typedef long long LL;

int n, m;
int a[1000005], vis[1000005], pos[1000005];
const int maxn = 1500000;
int c[maxn];		//用于处理树状数组
int lowbit(int x)
{
	return (x)&(-x);
}
void update_onepos(int pos, int x)		//单点增加x
{
	while (pos <= n)
	{
		c[pos] += x;
		pos += lowbit(pos);
	}
}
int getsum_onepos(int pos)		//区间求和 [1,x]
{
	int sum = 0;
	while (pos > 0)
	{
		sum += c[pos];
		pos -= lowbit(pos);
	}
	return sum;
}
int getsum_range(int x1, int x2)	//任意区间[x1,x2]求和
{
	return getsum_onepos(x2) - getsum_onepos(x1);
}

int main()
{
	scanf("%d %d", &n, &m);
	for (int i = 1; i <= n; i++)
	{
		scanf("%d", &a[i]);
		pos[a[i]] = i;
	}
	for (int i = 1; i <= m; i++)
	{
		int x;
		scanf("%d", &x);
		vis[x] = 1;
	}
	LL ans = 0;
	set<int>ss;
	for (int i = 1; i <= n; i++)
	{
		if (vis[i])
		{
			ss.insert(pos[i]);
		}
		else
		{
			LL l, r;
			set<int>::iterator it = ss.lower_bound(pos[i]);
			if (it == ss.begin())
			{
				if (ss.empty())
				{
					l = 1, r = n;
				}
				else
				{
					l = 1, r = (*it)-1;
				}
			}
			else if (it == ss.end())
			{
				it--;
				l = (*it)+1, r = n;
			}
			else
			{
				r = (*it) - 1;
				it--;
				l = (*it) + 1;
			}
			ans =ans+ (LL)(r - l + 1);
			ans =ans- (LL)getsum_range(l - 1, r);
			update_onepos(pos[i], 1);
		}
	}
	printf("%lld\n", ans);
	getchar();
	getchar();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
树状数组(Fenwick Tree)是一种用于高效处理区间和查询的数据结构,常用于解一维数组的前缀和、区间更新和查询等问题。 在 Codeforces 上,树状数组常被用来解决一些与区间和查询有关的问题。它可以在 O(logn) 的时间内完成单点更新和查询,以及区间求和等操作。 下面是一个简单的示例代码,展示了如何实现一个基本的树状数组: ```cpp #include <iostream> #include <vector> using namespace std; // 获取最低位的 1 int getLowbit(int x) { return x & -x; } // 树状数组的单点更新操作 void update(vector<int>& fenwick, int index, int delta) { while (index < fenwick.size()) { fenwick[index] += delta; index += getLowbit(index); } } // 树状数组的前缀和查询操作 int query(vector<int>& fenwick, int index) { int sum = 0; while (index > 0) { sum += fenwick[index]; index -= getLowbit(index); } return sum; } int main() { int n; cin >> n; vector<int> fenwick(n + 1, 0); // 初始化树状数组 for (int i = 1; i <= n; i++) { int val; cin >> val; update(fenwick, i, val); } // 进行查询操作 int q; cin >> q; while (q--) { int type; cin >> type; if (type == 1) { int index, delta; cin >> index >> delta; update(fenwick, index, delta); } else if (type == 2) { int l, r; cin >> l >> r; int sum = query(fenwick, r) - query(fenwick, l - 1); cout << sum << endl; } } return 0; } ``` 在这个示例中,我们使用了一个长度为 n 的数组 `fenwick` 来表示树状数组。`update` 函数用于更新树状数组中的某个元素,`query` 函数用于查询树状数组中某个区间的和。 你可以根据具体问题的要求进行相应的修改和扩展。希望对你有所帮助!如果有任何疑问,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值