http://codeforces.com/contest/703/problem/B

题意:

每个点都有一个值,每条边的权值为这两个点相乘。1~n成环。现在有k个城市,城市与其他所有点都相连,计算出所有边权值和。

思路:

每个城市要与其它所有城市都相连,所以我们可以先去计算城市的,先预处理一下,计算出所有点的值的和,那么每个城市乘以所有点的值的和减去它自身的值。

但是这样的话每两个城市之间就计算了两遍,要减去,具体看代码。

最后再考虑环上的点,如果还有边未计算,就加上去。

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL; const int maxn=1e5+; int n,k;
int a[maxn];
int b[maxn];
int vis[maxn];
LL sum;
LL summ; int main()
{
//freopen("D:\\input.txt","r",stdin);
while(~scanf("%d%d",&n,&k))
{
sum=; summ=;
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i]; //所有点的和
vis[i]=;
}
for(int i=;i<=k;i++)
{
scanf("%d",&b[i]);
summ+=a[b[i]]; //城市的和
vis[b[i]]=;
} LL ans=;
for(int i=;i<=k;i++)
{
ans+=a[b[i]]*(sum-a[b[i]]);
} //去重
LL temp=;
for(int i=;i<=k;i++)
{
temp+=a[b[i]]*(summ-a[b[i]]);
}
temp/=;
ans-=temp; //计算环上还未计算的
for(int i=;i<n;i++)
{
if(vis[i]== && vis[i+]==)
ans+=a[i]*a[i+];
}
if(vis[]== && vis[n]==)
ans+=a[]*a[n];
printf("%lld\n",ans);
}
return ;
}

Codeforces Round #365 (Div. 2) B - Mishka and trip的更多相关文章

  1. 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 ...

  2. Codeforces Round #365 (Div. 2)-D Mishka and Interesting sum(树状数组)

    题目链接:http://codeforces.com/contest/703/problem/D 思路:看了神犇的代码写的... 偶数个相同的数异或结果为0,所以区间ans[l , r]=区间[l , ...

  3. Codeforces Round #365 (Div. 2) D. Mishka and Interesting sum (离线树状数组+前缀xor)

    题目链接:http://codeforces.com/contest/703/problem/D 给你n个数,m次查询,每次查询问你l到r之间出现偶数次的数字xor和是多少. 我们可以先预处理前缀和X ...

  4. Codeforces Round #365 (Div. 2) E - Mishka and Divisors(转化成01-背包)

    http://codeforces.com/contest/703/problem/E 题意: 给出n个数和一个k,计算出至少要多少个数相乘才是k的倍数. 思路:这道题目参考了杭电大神的代码http: ...

  5. Codeforces Round #365 (Div. 2) D - Mishka and Interesting sum(离线树状数组)

    http://codeforces.com/contest/703/problem/D 题意: 给出一行数,有m次查询,每次查询输出区间内出现次数为偶数次的数字的异或和. 思路: 这儿利用一下异或和的 ...

  6. Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum 树状数组+离线

    D. Mishka and Interesting sum time limit per test 3.5 seconds memory limit per test 256 megabytes in ...

  7. Codeforces Round #365 (Div. 2) D.Mishka and Interesting sum

    题目链接:传送门 题目大意:给n个数,m次询问,每次询问区间 l,r 内出现偶数次数的异或和 题目思路:前缀和+离线处理+树状数组 首先可以知道, l,r 内出现奇数次的数的和,就是把 l,r内所有数 ...

  8. Codeforces Round #365 (Div. 2) C - Chris and Road 二分找切点

    // Codeforces Round #365 (Div. 2) // C - Chris and Road 二分找切点 // 题意:给你一个凸边行,凸边行有个初始的速度往左走,人有最大速度,可以停 ...

  9. Codeforces Round #365 (Div. 2) Mishka and trip

    Mishka and trip 题意: 有n个城市,第i个城市与第i+1个城市相连,他们边的权值等于i的美丽度*i+1的美丽度,有k个首都城市,一个首都城市与每个城市都相连,求所有边的权值. 题解: ...

随机推荐

  1. 前端开发 - JQuery&Bootstrap - 总结

    一.JavaScript和Jquery的区别 1.javascript的缺点: 1.书写繁琐,代码量大 2.代码复杂 3.动画效果,很难实现.使用定时器 各种操作和处理 2.定义: 1.Javascr ...

  2. chrome 因js死循环卡住

    chrome因js死循环卡住的解决方法: top命令查看chrome的pid kill -9 pid 杀死整个chrome进程(所有网页都关闭)

  3. Clairewd’s message--hdu4300(Next数组的运用)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4300 题意就是给你26的字母的加密方式,然后又给了一个串s1是包含加密后的和没有加密的但是没有加密的可 ...

  4. 一条SQL引起的雪崩

    1.问题描述 MySQL服务器卡死,CPU飚到300%多,命令执行缓慢. 2.问题定位 踩了狗屎运,直接找到了问题缘由 发现了一条SQL写的模糊匹配,将%写在了关键字的前面,这样会造成查询不使用索引, ...

  5. PHP连接MYSQL操作数据库

    PHP连接MYSQL操作数据库 <?php $con = mysql_connect("localhost","root",""); ...

  6. PKU 1208 The Blocks Problem(模拟+list应用)

    题目大意:原题链接 关键是正确理解题目意思 首先:介绍一下list容器的一些操作:参考链接 list<int> c1; c1.unique();              去重. c1.r ...

  7. 31. Next Permutation (下一个全排列)

    Implement next permutation, which rearranges numbers into the lexicographically next greater permuta ...

  8. DevStore分享:月薪3万的程序员都避开了哪些坑

    程序员薪水有高有低,有的人一个月可能拿30K.50K,有的人可能只有2K.3K.同样有五年工作经验的程序员,可能一个人每月拿20K,一个拿5K.是什么因素导致了这种差异?我特意总结了容易导致薪水低的九 ...

  9. WEB项目异常处理

    package cn.rest.advice; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger;impor ...

  10. 如何建立DB2分区数据库?(转)

    欢迎和大家交流技术相关问题:邮箱: jiangxinnju@163.com博客园地址: http://www.cnblogs.com/jiangxinnjuGitHub地址: https://gith ...