TTTTTTTTTTTTT CF#365 div2 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 toci.
- 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 every 1 ≤ 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 1
2 3 1 2
5 2
3 5 2 2 4
1 4
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个点,n个点按标号一次首位相接,其中有m个特殊的点,这m个点跟其他的所有点都相连接,点之间连接的线
的权值为点的权值之积,任意两个点之间至多一条边,最后问你整个图边的权值之和
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
#define inf 0x7f7f7f7f
#define FOR(i,n) for(int i=1;i<=n;i++)
#define CT continue;
#define PF printf
#define SC scanf
const int mod=1000000007;
const int N=1e5+100; int ncap[N],flag[N];
ll a[N];
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
ll all=0;
for(int i=1;i<=n;i++)
{
scanf("%lld",&a[i]);
all+=a[i];
}
a[n+1]=a[1];
a[0]=a[n];
for(int i=1;i<=m;i++) scanf("%d",&ncap[i]);
ll ans=0,tmp=0;
for(int i=1;i<=n;i++) ans+=a[i+1]*a[i];
MM(flag,0);
for(int i=1;i<=m;i++)
{
int cur=ncap[i],l=cur-1,r=cur+1;
flag[cur]=1;
if(l==0) l=n;
if(r==n+1) r=1;
ans+=a[cur]*(all-a[cur]-tmp);
if(!flag[l]) ans-=a[cur]*a[l];
if(!flag[r]) ans-=a[cur]*a[r];
tmp+=a[cur];
}
printf("%lld\n",ans);
}
return 0;
}
分析:有点锻炼思维,首先ans初始值为n个点围成的一个圈的边权值之和,然后对于每个特殊的点,它所能增加的边权值之和
为 该点权值( 所有点的权值之和-该点权值-先前遍历过的特殊的点的权值-与其在环上直接相连的两点权值之和)
不过有时候因为先前遍历过的点可能跟与在环上直接相连的点有重合,所以要设置一个flag,避免再被减一
次。
TTTTTTTTTTTTT CF#365 div2 B 统计点的更多相关文章
- CF 365 div2 D
http://codeforces.com/contest/703/problem/D 题目大意:给你一个长度为n数组,有q个询问,每次询问一个区间[l,r],这个区间的val就是所有数值为偶数个的数 ...
- CF #365 DIV2 D Mishka and Interesting sum 区间异或+线段树
D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组
题目链接:CF #365 (Div. 2) D - Mishka and Interesting sum 题意:给出n个数和m个询问,(1 ≤ n, m ≤ 1 000 000) ,问在每个区间里所有 ...
- CF #365 (Div. 2) D - Mishka and Interesting sum 离线树状数组(转)
转载自:http://www.cnblogs.com/icode-girl/p/5744409.html 题目链接:CF #365 (Div. 2) D - Mishka and Interestin ...
- cf 442 div2 F. Ann and Books(莫队算法)
cf 442 div2 F. Ann and Books(莫队算法) 题意: \(给出n和k,和a_i,sum_i表示前i个数的和,有q个查询[l,r]\) 每次查询区间\([l,r]内有多少对(i, ...
- cf #365b 巧妙的统计
Mishka and trip time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- CF 672 div2 D
http://codeforces.com/contest/672/problem/D 题目大意: 有n个人,每个人有pi的钱,然后可以由如下操作,每次都可以挑选一个最富有的人,把它的钱给最穷的人.但 ...
- CF #442 div2
A 判断下5个名字出现了几次.pre数据巨弱,就这么一水题在std测刷掉了非常多的人.. /** @Date : 2017-10-24 16:04:41 * @FileName: A.cpp * @P ...
- CF#603 Div2
差不多半年没打cf,还是一样的菜:不过也没什么,当时是激情,现在已是兴趣了,开心就好. A Sweet Problem 思维,公式推一下过了 B PIN Codes 队友字符串取余过了,结果今天早上一 ...
随机推荐
- C++ 包含目录、库目录、附加包含目录、附加库目录、附加依赖项之详解(转)
最近因为接触机器学习,所有涉猎到C++方面的开发.在c++中有几个概念很迷糊. VS项目中的包含目录.库目录.附加包含目录.附加库目录.附加依赖项均在"项目->属性->配置属性& ...
- Python自学笔记之字符串的操作
1.将字符串全部变为小写:lower() casefold() 范围更广 2.将字符串全部变为大写:upper() 3.判断是否大小写:isupper() islower() 4.居中:center( ...
- python3.6以后的新写法
声明redis_store为StrictRedis 类型,值为None,用处:在别处调用时,如果redis_store仍为None,不会有提示(自动补全的提示),如果想要自动补全的提示则写成这样,函数 ...
- react中数据持久化缓存redux-persist
一.安装redux-persist: npm install redux-persist --save 二..babelrc中增加redux-persist配置: "plugins" ...
- 初识python之了解程序设计基本方法
对于用计算机解决一些问题,这里有一个程序设计的基本方法,主要分为六个步骤,其分析和实现过程如下: (1)分析问题:利用计算机解决问题需要结合计算机技术的发展水平和人类对问题的思考程度,在特定技术和社会 ...
- 一种移动端position:absolute布局:
一种移动端position:absolute布局: 1.他父级不需要加上 position:relative; 如果父级不是不是body,则加position:absolute; 2.红色加量部分 ...
- OSCP-FristiLeaks
环境搭建 靶机下载: https://www.vulnhub.com/entry/fristileaks-13,133/ 安装:直接用virtualbox打开 网络桥接 找到靶机IP 虚拟机启动就显示 ...
- 开源you-get项目爬虫,以及基于python+selenium的自动测试利器
写在前面 爬虫和自动测试,对于python来说是最合适不过也是最擅长的. 开源的项目也很多,例如you-get项目https://github.com/soimort/you-get.盗链和爬虫神器. ...
- vue项目中关于微信分享的坑,以及安卓和ios获取location.href不同的处理
最近做vue项目的微信公众号项目,涉及到微信分享,记录一下心得,以备后用,vue路由用的是hash模式: 该项目只是公众号里面的h5链接,不需要获取code获取access_token的票据,因此前端 ...
- deep_learning_neural network梯度下降
神经网络优化算法:梯度下降法.Momentum.RMSprop和Adam 最近回顾神经网络的知识,简单做一些整理,归档一下神经网络优化算法的知识.关于神经网络的优化,吴恩达的深度学习课程讲解得非常通俗 ...