George is a cat, so he loves playing very much.
Vitaly put n cards in a row in front of George. Each card has an integer written on it. All cards had distinct numbers written on them. Let's number the cards from the left-to-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 has exactly K cards left. He also wants the i-th card from left to has 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 isn't greater than the current number of cards in the row) Contiguo US 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 this 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 would he get in total if he reaches his goal and acts optimally W Ell? Help George, find a 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 so you need to get. It's 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.
Sample Test (s)
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
Requires a n-k operation, each operation selects a continuous sequence, you can delete the minimum value of the sequence, the value is the length of the sequence
Then obviously, every time should be deleted from the beginning of the small, and the longer the better, I started with rmq+ two points to determine the interval, the results of various T
It is then changed to set, the side of the operation side to set lost numbers (the position of these numbers), and then can be over
Note that after the interval has been determined, add the answer by removing the number of deleted numbers in the interval.
Use a tree-like array.
/************************************************************************* > File name:cf-227-e.cpp > Aut Hor:alex > Mail: [email protected] > Created time:2015 April 28 Tuesday 10:54 20 seconds ******************************* *****************************************/#include <functional>#include <algorithm>#include <iostream>#include <fstream>#include <cstring>#include <cstdio>#include <cmath>#include <cstdlib>#include <queue>#include <stack>#include <map>#include <bitset>#include <set>#include <vector>using namespace STD;Const DoublePI =ACOs(-1.0);Const intINF =0x3f3f3f3f;Const DoubleEPS =1e-15;typedef Long LongLL;typedefPair <int,int> PLL;Static Const intN =1001010;intArr[n];intTar[n];intTmp[n]; PLL Qu[n];intTree[n]; Set <int>Qty Set <int>:: Iterator it;intLowbit (intx) {returnX & (-X);}voidAddintNintXintVal) { for(inti = x; I <= N; i + = Lowbit (i)) {Tree[i] + = val; }}intGetsum (intx) {intAns =0; for(inti = x; I I-= Lowbit (i)) {ans + = tree[i]; }returnAns;}intMain () {intN, K, u; while(~scanf("%d%d", &n, &k)) { for(inti =1; I <= N; ++i) {scanf("%d", &u); Tmp[u] =1; Arr[u] = i; Add (n, I,1); } for(inti =1; I <= K; ++i) {scanf("%d", &u); Tmp[u] =0; } st.clear (); St.insert (0); St.insert (n +1); LL ans =0; for(inti =1; I <= N; ++i) {if(!tmp[i]) {St.insert (arr[i]);Continue; } it = St.upper_bound (Arr[i]);intr = *it-1;intL = * (--it); Ans + = Getsum (r)-Getsum (L); Add (n, Arr[i],-1); }printf("%lld\n", ans); }return 0;}
Codeforces Round #227 (Div. 2)---E. George and Cards (greedy, tree-like array +set maintenance, good question!)