Codeforces#365 (Div.2) Mishka and trip【思维】

B. Mishka and trip
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Little Mishka is a great traveller and she visited many countries. After thinking about where to travel this time, she chose XXX — beautiful, but little-known northern country.

Here are some interesting facts about XXX:

  1. XXX consists of n cities, k of whose (just imagine!) are capital cities.
  2. All of cities in the country are beautiful, but each is beautiful in its own way. Beauty value of i-th city equals to ci.
  3. All the cities are consecutively connected by the roads, including 1-st and n-th city, forming a cyclic route 1 — 2 — ... — n — 1. Formally, for every 1 ≤ i < n there is a road between i-th and i + 1-th city, and another one between 1-st and n-th city.
  4. Each capital city is connected with each other city directly by the roads. Formally, if city x is a capital city, then for every1 ≤ i ≤ n,  i ≠ x, there is a road between cities x and i.
  5. There is at most one road between any two cities.
  6. Price of passing a road directly depends on beauty values of cities it connects. Thus if there is a road between cities i and j, price of passing it equals ci·cj.

Mishka started to gather her things for a trip, but didn't still decide which route to follow and thus she asked you to help her determine summary price of passing each of the roads in XXX. Formally, for every pair of cities a and b (a < b), such that there is a road betweena and b you are to find sum of products ca·cb. Will you help her?

Input

The first line of the input contains two integers n and k (3 ≤ n ≤ 100 000, 1 ≤ k ≤ n) — the number of cities in XXX and the number of capital cities among them.

The second line of the input contains n integers c1, c2, ..., cn (1 ≤ ci ≤ 10 000) — beauty values of the cities.

The third line of the input contains k distinct integers id1, id2, ..., idk (1 ≤ idi ≤ n) — indices of capital cities. Indices are given in ascending order.

Output

Print the only integer — summary price of passing each of the roads in XXX.

Examples
input
4 1
2 3 1 2
3
output
17
input
5 2
3 5 2 2 4
1 4
output
71
Note

This image describes first sample case:

It is easy to see that summary price is equal to 17.

This image describes second sample case:

It is easy to see that summary price is equal to 71.


题目大意:一共有n个点,有k个关键点,每个点的点权我们已经知道了,而且其关键点的编号也知道了,初始的时候需要连接1-2,2-3,3-4............n-1的边,然后关键点还需要连接其他各点,其边权值为两定点的权值乘积。


思路:


1、记录点权和sum,那么a【di】*(sum-a【di】)的值就是其点di到其他个点的距离值的和。


2、那么我们初始的时候先处理初始必须加进去的边,之后再处理关键点,每个关键点,output+=a【di】*(sum-a【di】)-a【di】*a【di+1】-a【di】*【di-1】,然而如果关键点的个数大于1的时候,我们就有加了多次的边,而这些多次相加的边一定是关键点之间的边,那么我们用summ记录当前点之前所有已经处理完了的点的点权和。那么我们每一次处理完一个关键点之后,output-=summ*a【di】。summ+=a【di】。


3、而这个时候,我们发现有可能多减掉了一些边权,比如这样样例中:

4 2

2 3 1 2

1 2

我们还需要加回来。


那么我们再增加一个vis【】标记数组,标记一个点是否是关键点,如果其当前关键点的邻居(di-1,di+1两个点)也是关键点,那么还要加回来其两点边权,因为这条边是在初始的时候我们就加进去的了,然后我们在刚刚output-summ*a【di】的时候,将这条边去掉了,所以我萌还要加回来。


4、将思路确定好之后,剩下的就是实现代码的过程了,注意数据比较大,用LL...

.

Ac代码:


#include<stdio.h>
#include<string.h>
using namespace std;
#define ll __int64
ll a[100005];
ll sum[100005];
int dian[100005];
int vis[100005];
int main()
{
    int n,k;
    while(~scanf("%d%d",&n,&k))
    {
        memset(a,0,sizeof(a));
        memset(vis,0,sizeof(vis));
        memset(sum,0,sizeof(sum));
        ll output=0;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            sum[i]=sum[i-1]+a[i];
        }
        a[0]=a[n];
        a[n+1]=a[1];
        for(int i=1;i<=n;i++)
        {
            output+=a[i]*a[i+1];
        }
        ll summ=0;
        while(k--)
        {
            int d;
            scanf("%d",&d);
            vis[d]=1;
            ll tmp=(sum[n]-a[d])*a[d];
            tmp-=a[d]*a[d+1];
            tmp-=a[d]*a[d-1];
            //printf("%I64d\n",tmp);
            output+=tmp;
            output-=summ*a[d];
            if(d==1)
            {
                if(vis[d+1]==1)output+=a[d+1]*a[d];
                if(vis[n]==1)output+=a[n]*a[d];
            }
            else if(d==n)
            {
                if(vis[d-1]==1)output+=a[d-1]*a[d];
                if(vis[1]==1)output+=a[1]*a[d];
            }
            else
            {
                if(vis[d-1]==1)output+=a[d-1]*a[d];
                if(vis[d+1]==1)output+=a[d+1]*a[d];
            }
            summ+=a[d];
        }
        printf("%I64d\n",output);
    }
}






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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值