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:
- XXX consists of n cities, k of whose (just imagine!) are capital cities.
- 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.
- 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.
- 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.
- There is at most one road between any two cities.
- 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?
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.
Print the only integer — summary price of passing each of the roads in XXX.
4 1 2 3 1 2 3
17
5 2 3 5 2 2 4 1 4
71
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);
}
}