George and Cards - CodeForces 387E 树状数组

E. George and Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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


思路:应该是从最小的数开始删起,那么l[i],r[i]数组记录某个数删的时候,可以涉及到左边右边的边界,然后树状数组再得到其中已经删去的点。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<vector>
#define M 1000010
using namespace std;
int l[M],r[M],l2[M],r2[M],num1[M],num2[M],pos[M],vis[M],tree[M],n,m;
long long ans=0;
int lowbit(int x)
{ return x&(-x);}
void update(int x)
{ for(;x<=n;x+=lowbit(x))
   tree[x]++;
}
int sum(int x)
{ int ret=0;
  for(;x>0;x-=lowbit(x))
   ret+=tree[x];
  return ret;
}
int main()
{ int i,j,k,len;
  scanf("%d%d",&n,&m);
  for(i=1;i<=n;i++)
  { scanf("%d",&num1[i]);
    l[i]=i;r[i]=i;l2[i]=i;r2[i]=i;
    pos[num1[i]]=i;
  }
  for(i=1;i<=m;i++)
  { scanf("%d",&num2[i]);
    vis[num2[i]]=1;
  }
  vis[0]=2;vis[n+1]=2;
  for(i=1;i<=n;i++)
   if(vis[num1[i]]==0)
    l2[i]=l2[i-1];
  r2[1+n]=1+n;
  for(i=n;i>=1;i--)
   if(vis[num1[i]]==0)
    r2[i]=r2[i+1];
  for(i=1;i<=n;i++)
   while(true)
   { if(num1[l[i]-1]>=num1[i])
      l[i]=l[l[i]-1];
     else if(vis[num1[l[i]-1]]==0)
      l[i]=l2[l[i]-1]+1;
     else
      break;
   }
  for(i=n;i>=1;i--)
   while(true)
   { if(num1[r[i]+1]>=num1[i])
      r[i]=r[r[i]+1];
     else if(vis[num1[r[i]+1]]==0)
      r[i]=r2[r[i]+1]-1;
     else
      break;
   }
  for(i=1;i<=n;i++)
   if(vis[i]==0)
   { k=pos[i];
     ans+=r[k]-l[k]+1-sum(r[k])+sum(l[k]-1);
     update(k);
   }
  printf("%I64d\n",ans);
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值