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 ≤ w; w 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();
}