【Codeforces Round 365 (Div 2)B】【容斥】Mishka and trip 环加完全点图的边权乘积和

来源:互联网 发布:语音转文本软件 编辑:程序博客网 时间:2024/04/30 10:15

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 between a 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 12 3 1 23
output
17
input
5 23 5 2 2 41 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.


#include<stdio.h>#include<iostream>#include<string.h>#include<string>#include<ctype.h>#include<math.h>#include<set>#include<map>#include<vector>#include<queue>#include<bitset>#include<algorithm>#include<time.h>using namespace std;void fre() { freopen("c://test//input.in", "r", stdin); freopen("c://test//output.out", "w", stdout); }#define MS(x,y) memset(x,y,sizeof(x))#define MC(x,y) memcpy(x,y,sizeof(x))#define MP(x,y) make_pair(x,y)#define ls o<<1#define rs o<<1|1typedef long long LL;typedef unsigned long long UL;typedef unsigned int UI;template <class T1, class T2>inline void gmax(T1 &a, T2 b) { if (b>a)a = b; }template <class T1, class T2>inline void gmin(T1 &a, T2 b) { if (b<a)a = b; }const int N = 1e5 + 10, M = 0, Z = 1e9 + 7, ms63 = 0x3f3f3f3f;int n, m;LL c[N];bool e[N];void zjs(){LL sum = 0;for (int i = 1; i <= n; ++i){scanf("%lld", &c[i]);sum += c[i];}c[0] = c[n]; c[n + 1] = c[1];LL ans = 0;MS(e, 1);for (int i = 1; i <= m; ++i){int x; scanf("%d", &x);sum -= c[x];ans += c[x] * sum;e[x] = 0;}e[n + 1] = e[1];for (int i = 1; i <= n; ++i)if (e[i] && e[i + 1]){ans += c[i] * c[i + 1];}printf("%lld\n", ans);}int main(){while (~scanf("%d%d", &n, &m)){zjs(); continue;LL sum = 0;for (int i = 1; i <= n; ++i){scanf("%lld", &c[i]);sum += c[i];}c[0] = c[n]; c[n + 1] = c[1];//先算上环形的LL ans = 0;for (int i = 1; i <= n; ++i)ans += c[i] * c[i + 1];LL cap = 0;LL slf = 0;MS(e, 0);for (int i = 1; i <= m; ++i){int x; scanf("%d", &x);cap += c[x];slf += c[x] * c[x];int y = x - 1; if (y == 0)y = n;if (!e[x])ans -= c[x] * c[x + 1], e[x] = 1;//如果环形的已经算上了,这里减掉if (!e[y])ans -= c[y] * c[y + 1], e[y] = 1;//如果环形的已经算上了,这里减掉}ans += cap*sum - slf;//每个首都,连其他所有城市,不包括自己ans -= (cap*cap - slf) / 2;//首都与首都相连的,被算了2倍printf("%lld\n", ans);}return 0;}/*【题意】有n(1e5)个点,每个点有一个权值c[](10000)这些点按照1<->2<->3<->...n<->1的方式连成了一个环。除此之外,这个图上,n个点中有m个是首都城市。每个首都城市和其他所有点都有一条边。我们想求出,对于所有边(x,y),求出∑(c[x] * c[y])。【类型】容斥【分析】这道题我的做法是这样的——1,算上环上所有边的贡献。2,对于所有首都城市,在不考虑环边的条件下,一共有多少条边呢?第一种计算方式cap表示首都城市权值和sum表示所有城市权值和slf表示"首都城市权值平方"的和(cap * sum - slf) - (cap * cap - slf)/2就是了。因为我们要考虑环边,所以一旦首都边和环边冲突了,我们减掉环边(用bool数组记重)第二种计算方式,zjs的写法——1,求sum2,对于每个首都城市x,对答案的贡献为c[x] * others【时间复杂度&&优化】O(n)【数据】4 41 1 1 11 2 3 4ans=66 61 1 1 1 1 11 2 3 4 5 6*/


0 0
原创粉丝点击