【Codeforces Round 365 (Div 2)B】【容斥】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 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 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.


#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|1
typedef 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,求sum
2,对于每个首都城市x,对答案的贡献为c[x] * others

【时间复杂度&&优化】
O(n)

【数据】
4 4
1 1 1 1
1 2 3 4
ans=6

6 6
1 1 1 1 1 1
1 2 3 4 5 6

*/


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值