Codeforces Round #365 (Div. 2) B 前缀和
1 second
256 megabytes
standard input
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:
- 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 route1 — 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 andj, 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 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个中心城市 每个城市都有一个权值c k个中心城市编号按照升序给出
首先以1 — 2 — ... — n — 1路径串成一个环,其次中心城市到任意城市都有一条路.任意两个城市间最多有一条路
每条边的权值为相邻两个城市的权值的乘积。求所有边的权值的和。
题解:
1.处理成串的边的权值和
2.处理与中心城市相连的城市的边的权值和
3.去重(某条边的两段都为中心城市)
/******************************
code by drizzle
blog: www.cnblogs.com/hsd-/
^ ^ ^ ^
O O
******************************/
//#include<bits/stdc++.h>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<map>
#include<algorithm>
#include<queue>
#include<cmath>
#define ll __int64
#define PI acos(-1.0)
#define mod 1000000007
using namespace std;
int n,k;
ll a[];
int b[];
int used[];
ll zha[];
int main()
{
scanf("%d %d",&n,&k);
ll sum=;
ll ans=;
memset(used,,sizeof(used));
for(int i=;i<=n;i++)
{
scanf("%I64d",&a[i]);
sum+=a[i];
}
zha[]=;
for(int i=;i<=k;i++)
{
scanf("%d",&b[i]);
zha[i]=zha[i-]+a[b[i]];
used[b[i]]=;
}
for(int i=;i<n;i++)
ans=ans+a[i]*a[i+];
ans=ans+a[n]*a[];
for(int i=;i<=k;i++)
{
if(b[i]==)
{
ans=ans+a[]*(sum-a[]-a[]-a[n]);
continue;
}
if(b[i]==n)
{
ans=ans+a[n]*(sum-a[]-a[n]-a[n-]);
continue;
}
ans=ans+a[b[i]]*(sum-a[b[i]]-a[b[i]-]-a[b[i]+]);
}
for(int i=;i<=k;i++)
{
ll exm=zha[k]-zha[i];
if(b[i]==n)
break;
if(b[i]==)
{
if(used[])
exm=exm-a[];
if(used[n])
exm=exm-a[n];
ans=ans-exm*a[b[i]];
}
else
{
if(used[b[i]+])
exm=exm-a[b[i]+];
ans=ans-exm*a[b[i]];
}
}
printf("%I64d\n",ans);
}
Codeforces Round #365 (Div. 2) B 前缀和的更多相关文章
- Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点
// Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)
题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...
- Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)
题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...
- Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum 离线+线段树
题目链接: http://codeforces.com/contest/703/problem/D D. Mishka and Interesting sum time limit per test ...
- Codeforces Round #365 (Div. 2) E - Mishka and Divisors(转化成01-背包)
http://codeforces.com/contest/703/problem/E 题意: 给出n个数和一个k,计算出至少要多少个数相乘才是k的倍数. 思路:这道题目参考了杭电大神的代码http: ...
- Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)
http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...
- Mishka and Divisors[CodeForces Round #365 Div.2]
http://codeforces.com/contest/703/problem/E 题意:给定一个最多个数的序列,从中选出最少个数的数字,使得他们的乘积是k的倍数,若有多种选择方式,输出选出数字和 ...
- Codeforces Round #365 (Div. 2) D 树状数组+离线处理
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
- Codeforces Round #365 (Div. 2) A 水
A. Mishka and Game time limit per test 1 second memory limit per test 256 megabytes input standard i ...
随机推荐
- ZOJ 3652 Maze 模拟,bfs,读题 难度:2
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4842 要注意题目中两点: 1.在踏入妖怪控制的区域那一刹那,先减行动力,然后才 ...
- 一模 (6) day1
第一题: 题目大意: 一个n的全排列A[i]是单峰的,当且仅当存在某个x使得A[1]<A[2]<...<A[x]>A[x+1]>...>A[n]. 试求 n 的单峰 ...
- C/C++类型转换总结
---恢复内容开始--- 最近做笔试题经常会碰到有关类型转换的题型,所以结合例子做下总结,也是希望自己能更时刻的理解类型转换. C++的类型转换包括内置类型和类类型对象的转换. (1) 1.1隐式类型 ...
- iOS开发中XML的DOM和SAX解析方法
一.介绍 dom是w3c指定的一套规范标准,核心是按树形结构处理数据,dom解析器读入xml文件并在内存中建立一个结构一模一样的“树”,这树各节点和xml各标记对应,通过操纵此“树”来处理xml中的文 ...
- poj2891 拓展欧几里得
//Accepted 164 KB 16 ms //拓展欧几里得 //m=a1*x+b1 --(1) //m=a2*(-y)+b2 --(2) //->a1*x+a2*y=b2-b1 //由欧几 ...
- C++使用POST方法向网页提交数据-----C++发送HTTP数据获取Google天气预报
例1:C++使用POST方法向网页提交数据 转自:http://www.it165.net/pro/html/201208/3534.html 在C++中可以使用POST方法向网页中提交数据,这 ...
- SVG 2D入门9 - 蒙板
SVG支持的蒙板 SVG支持多种蒙板特效,使用这些特性,我们可以做出很多很炫的效果.至于中文中把mask叫做"蒙板"还是"遮罩"就不去区分了,这里都叫做蒙板吧. ...
- LeetCode 笔记系列 19 Scramble String [合理使用递归]
题目: Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty subs ...
- javascript笔记5-BOM
Javascript应用的平台很多,不仅仅针对Web.在Web中使用Javascript,BOM(browser object model,浏览器对象模型)是核心. BOM提供了很多对象,用于访问浏览 ...
- Code is not literature
http://www.gigamonkeys.com/code-reading/ I have started code reading groups at the last two companie ...