题干:

Once at a team training Vasya, Petya and Sasha got a problem on implementing linear search in an array.

According to the boys, linear search works as follows. The array elements in a pre-selected order are in turn compared with the number that you need to find. Once you find the array element that is equal to the required one, the search ends. The efficiency of the algorithm is the number of performed comparisons. The fewer comparisons the linear search has made, the more effective it is.

Vasya believes that a linear search would work better if it sequentially iterates through the elements, starting with the 1-st one (in this problem we consider the elements of the array indexed from 1 to n) and ending with the n-th one. And Petya says that Vasya is wrong: the search will need less comparisons if it sequentially iterates the elements starting from the n-th and ending with the 1-st one. Sasha argues that the two approaches are equivalent.

To finally begin the task, the teammates decided to settle the debate and compare the two approaches on an example. For this, they took an array that is a permutation of integers from 1 to n, and generated m queries of the form: find element with value bi in the array. They want to calculate for both approaches how many comparisons in total the linear search will need to respond to all queries. If the first search needs fewer comparisons, then the winner of the dispute is Vasya. If the second one does, then the winner is Petya. If both approaches make the same number of comparisons, then Sasha's got the upper hand.

But the problem is, linear search is too slow. That's why the boys aren't going to find out who is right before the end of the training, unless you come in here. Help them to determine who will win the dispute.

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements in the array. The second line contains n distinct space-separated integers a1, a2, ..., an (1 ≤ ai ≤ n) — the elements of array.

The third line contains integer m (1 ≤ m ≤ 105) — the number of queries. The last line contains m space-separated integers b1, b2, ..., bm (1 ≤ bi ≤ n) — the search queries. Note that the queries can repeat.

Output

Print two integers, showing how many comparisons Vasya's approach needs and how many comparisons Petya's approach needs. Separate the numbers by spaces.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cin, cout streams or the %I64d specifier.

Examples

Input

2
1 2
1
1

Output

1 2

Input

2
2 1
1
1

Output

2 1

Input

3
3 1 2
3
1 2 3

Output

6 6

Note

In the first sample Vasya's approach will make one comparison (it starts with the 1-st element and immediately finds the required number), and Petya's approach makes two comparisons (first he compares with the 2-nd array element, doesn't find the search item and compares with the 1-st element).

In the second sample, on the contrary, Vasya's approach will need two comparisons (first with 1-st element, and then with the 2-nd), and Petya's approach will find the required value in one comparison (the first comparison with the 2-nd element).

题目大意:

就是告诉你一组数,,m个询问值为x的数字的位置。第一个人从左边数,第二个人从右边数,数到这个数就停止。问你每个人在m个询问中共需要数多少个数、

解题报告:

两种解法,,第一种权值数组复杂度o(n+m)、、第二种STL复杂度o((n+m)logn)、、看代码吧不难。。

AC代码1:(124ms)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
ll id[MAX];
int n,m;
int main()
{cin>>n;ll tmp;for(int i=0; i<n; i++) {scanf("%lld",&tmp);id[tmp]=i;}ll ans1=0,ans2=0;cin>>m;while(m--) {scanf("%d",&tmp);tmp=id[tmp];ans1+=tmp;ans2+=n-tmp;}printf("%lld %lld\n",ans1,ans2);return 0 ;
}

AC代码2:(374ms)

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<string>
#include<cmath>
#include<cstring>
#define ll long long
#define pb push_back
#define pm make_pair
#define fi first
#define se second
using namespace std;
const int MAX = 2e5 + 5;
multiset<pair<ll,int> > s1,s2;
multiset<pair<ll,int> > :: iterator it;
ll a[MAX],b[MAX];
int main()
{ll n,m,tmp;ll ans1=0,ans2=0;cin>>n;for(int i = 1; i<=n; i++) {scanf("%lld",a+i);s1.insert(pm(a[i],i));}reverse(a+1,a+n+1);for(int i = 1; i<=n; i++) {s2.insert(pm(a[i],i));}cin>>m;while(m--) {scanf("%lld",&tmp);it = s1.lower_bound(pm(tmp,0));
//      printf("%lld %d\n",(*it).fi,(*it).second);ans1 += s1.lower_bound(pm(tmp,0))->second;ans2 += s2.lower_bound(pm(tmp,0))->second;}printf("%lld %lld\n",ans1,ans2);return 0 ;}

【CodeForces - 227B 】Effective Approach (STL,思维)相关推荐

  1. Codeforces 1077B Disturbed People(思维题)

    Codeforces 1077B Disturbed People(思维题) There is a house with nn flats situated on the main street of ...

  2. [翻译论文]An effective approach for land-cover classification from airborne lidar fused with co-register

    [翻译论文]An effective approach for land-cover classification from airborne lidar fused with co-register ...

  3. 【ESMM论文精读】Entire Space Multi-Task Model: An Effective Approach for Estimating Post-Click Conversion

    文章目录 原始论文 摘要 (ABSTRACT) 关键词 (KEYWORDS) 1. 介绍 (INTRODUCTION) 2. 提出的方法 THE PROPOSED APPROACH 2.1 符号 No ...

  4. codeforces 1641C Anonymity Is Important (思维好题,STL)

    题意: 有 n 个人和 q 个信息, 0 l r x,如果 x = 0,表示编号 l 到 r 的人没有病:否则,表示编号 l 到 r 的人至少有一个得病. 1 x,询问编号为 x 的人健康情况. 思路 ...

  5. CodeForces - 160D Edges in MST(思维+tarjan/树链剖分+线段树)

    题目链接:点击查看 题目大意:给出一张 n 个点 m 条边组成的带权无向图,现在对于每条边来说,确定一下其分类: 一定是最小生成树上的边 可能是最小生成树上的边 一定不是最小生成树的边 题目分析:两种 ...

  6. Codeforces 846 A Curriculum Vitae 思维 暴力

    题目链接: http://codeforces.com/contest/846/problem/A 题目描述: 给你一个串, 你可以做删除操作, 要求结果串0不能在1的右边, 问最多可以剩几个数字 解 ...

  7. Codeforces 861 B Which floor? 思维

    题目链接: http://codeforces.com/contest/861/problem/B 题目描述: 每个楼梯上有相同数量的数, 都是从1开始, 给你m个不矛盾的信息, 问你能不能判断n在第 ...

  8. CodeForces - 1494E A-Z Graph(构造+思维)

    题目链接:https://vjudge.net/problem/CodeForces-1494E 题目大意:给出一个初始时只有 nnn 个点的有向带权图,需要执行 mmm 次操作,每次操作分为下列三种 ...

  9. CodeForces - 1504C Balance the Bits(思维+构造)

    题目链接:https://vjudge.net/problem/CodeForces-1504C 题目大意:给出一个长度为 nnn 的 010101 串,现在要求构造出两个长度为 nnn 的合法括号序 ...

最新文章

  1. 2022-2028年中国测绘设备行业研究及前瞻分析报告
  2. 将mnist数据集存储到本地文件
  3. MySQL-索引优化篇(4)_索引的维护
  4. 【PHP】Sublime下PHP网站开发指南
  5. 三步教你解决Invalid bound statement (not found): com.xxx.dao.xxxDao.selectByxx错误!!!!很简单
  6. 什么时候使用resulttype_ResultMap和ResultType在使用中的区别
  7. Nginx的accept_mutex配置
  8. 最全的数据结构和算法,不信过来看看有没有漏掉的
  9. UL电子线标准规格说明书
  10. 工业相机选型和镜头焦距计算
  11. Eclipse常用快捷键
  12. C我语言编程老鼠寻路,数据结构课设-走迷宫游戏.doc
  13. [转载]如何用JDO开发数据库应用(3)
  14. flutter 使用MD5加密
  15. [史上最全]笔记本更换内存条和固态硬盘重装系统教程
  16. 数据挖掘之缺失数据缺失的各种插补算法比较
  17. 【原创】机器学习一般流程总结
  18. 网页屏幕上有只小虫在到处爬(代码)
  19. 激光雷达和相机联合标定
  20. 火车到站时间接口 站到站列车信息检索

热门文章

  1. [Leetcode][第1143题][JAVA][最长公共子序列][LCS][动态规划]
  2. [Leetcode][第题][JAVA][两个数组的交集 II1][双指针][HashMap]
  3. f2 柱状图滚动 钉钉小程序_详解钉钉小程序组件之自定义模态框(弹窗封装实现)...
  4. 重装Nodejs后,webstorm代码报错问题
  5. java io 文件路径_如何从Java项目中的相对路径读取文件? java.io.File找不到指定的路径...
  6. datagridview 当前上下文中不存在bind_全面解析JavaScript中this指向问题
  7. inside uboot (六) DRAM芯片的控制线及时序
  8. mediastreamer2 的简介
  9. 设计模式C++实现 ——观察者模式
  10. java native 接口_Java本地接口--Java Native Interface (JNI)