【Codeforces Round 365 (Div 2)B】【容斥】Mishka and trip 环加完全点图的边权乘积和
来源:互联网 发布:语音转文本软件 编辑:程序博客网 时间:2024/04/30 10:15
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 between a 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 12 3 1 23
17
5 23 5 2 2 41 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.
#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*/
- 【Codeforces Round 365 (Div 2)B】【容斥】Mishka and trip 环加完全点图的边权乘积和
- Codeforces Round #365 (Div. 2) B. Mishka and trip
- Codeforces Round #365 (Div. 2) -- B. Mishka and trip
- Codeforces Round #365 (Div. 2) [B] Mishka and trip
- Codeforces Round #365 (Div. 2) B. Mishka and trip
- Codeforces Round #365 (Div. 2) B. Mishka and trip
- Codeforces Round #365 (Div. 2) B - Mishka and trip
- Codeforces Round #365 (Div. 2) B. Mishka and trip
- Codeforces Round #365 (Div. 2) Mishka and trip
- Codeforces Round #365 (Div. 2) Mishka and trip
- Codeforces Round #365 (Div. 2) Mishka and trip
- Codeforces Round #365 (Div. 2) -- B. Mishka and trip (找规律枚举)
- Codeforces Round #365 (Div. 2) 703B Mishka and trip 水题
- Codeforces#365 (Div.2) Mishka and trip【思维】
- codeforces B. Mishka and trip
- Mishka and trip(CF #365 Div. 2)
- Codeforces Round #365 (Div. 2)D. Mishka and Interesting sum
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum
- GO优缺点
- Linux中断(interrupt)子系统之一:中断系统基本原理
- 003——Struts2在web.xml中的配置
- 【Codeforces Round 365 (Div 2)A】【水题】Mishka and Game
- POJ-3984-迷宫问题(BFS打印路径)
- 【Codeforces Round 365 (Div 2)B】【容斥】Mishka and trip 环加完全点图的边权乘积和
- sql实例大全
- NYOJ-17单调递增最长子序列
- 【Codeforces Round 365 (Div 2)C】【贪心 极限阻碍思想】 Chris and Road 过马路不被凸包车碰撞的最小时间
- hdu 4289 Control【最大流Dinic----拆点+最大流最小割定理】
- 为什么全力推荐Golang
- 【Codeforces Round 365 (Div 2)D】【离线询问 树状数组 前驱思想】Mishka and Interesting sum 区间内出现次数偶数的数的异或和
- centos etho网卡不可用提示:Device etho does not seem to be present解决办法
- 解释一下关系数据库的第一第二第三范式?(转自知乎