1046 Shortest Distance(20 分)

The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed to tell the shortest distance between any pair of exits.

Input Specification:

Each input file contains one test case. For each case, the first line contains an integer N (in [3,10​5​​]), followed by N integer distances D​1​​ D​2​​ ⋯ D​N​​, where D​i​​ is the distance between the i-th and the (i+1)-st exits, and D​N​​ is between the N-th and the 1st exits. All the numbers in a line are separated by a space. The second line gives a positive integer M (≤10​4​​), with M lines follow, each contains a pair of exit numbers, provided that the exits are numbered from 1 to N. It is guaranteed that the total round trip distance is no more than 10​7​​.

Output Specification:

For each test case, print your results in M lines, each contains the shortest distance between the corresponding given pair of exits.

Sample Input:

5 1 2 4 14 9
3
1 3
2 5
4 1

Sample Output:

3
10
7
 #include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <map>
#include <stack>
#include <vector>
#include <queue>
#include <set>
#define LL long long
using namespace std;
const int MAX = 4e5 + ; int N, D[MAX], pre[MAX], M, ans, a, b, ans2;
struct node
{
int L, R, val;
}P[MAX]; void build(int dep, int l, int r)
{
P[dep].L = l, P[dep].R = r, P[dep].val = ;
if (l == r)
{
pre[l] = dep;
return;
}
int mid = (l + r) >> ;
build(dep << , l, mid);
build((dep << ) + , mid + , r);
} void update(int r, int b)
{
P[r].val += b;
if (r == ) return ;
update(r >> , b);
} void query(int dep, int l, int r)
{
if (P[dep].L == l && P[dep].R == r)
{
ans += P[dep].val;
return ;
}
int mid = (P[dep].L + P[dep].R) >> ;
if (r <= mid) query(dep << , l, r);
else if (l > mid) query((dep << ) + , l, r);
else
{
query(dep << , l, mid);
query((dep << ) + , mid + , r);
}
} int main()
{
// freopen("Date1.txt", "r", stdin);
scanf("%d", &N);
build(, , N);
for (int i = ; i <= N; ++ i)
{
scanf("%d", &D[i]);
update(pre[i], D[i]);
} scanf("%d", &M);
while (M --)
{
ans = ;
scanf("%d%d", &a, &b);
if (b < a) swap(a, b);
if (a == b - ) ans += D[a];
else query(, a, b - );
ans2 = ans, ans = ; if (a - == ) ans += D[];
else if (a - > ) query(, , a - );
if (b == N) ans += D[N];
else query(, b, N);
printf("%d\n", min(ans, ans2));
}
return ;
}

pat 1046 Shortest Distance(20 分) (线段树)的更多相关文章

  1. PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642

    PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...

  2. PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

  3. 1046 Shortest Distance (20 分)

    1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...

  4. PAT Advanced 1046 Shortest Distance (20 分) (知识点:贪心算法)

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  5. 1046 Shortest Distance (20分)

    The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...

  6. 【PAT甲级】1046 Shortest Distance (20 分)

    题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...

  7. PAT A1046 Shortest Distance (20 分)

    题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...

  8. PAT甲 1046. Shortest Distance (20) 2016-09-09 23:17 22人阅读 评论(0) 收藏

    1046. Shortest Distance (20) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue The ...

  9. PAT 1046 Shortest Distance

    1046 Shortest Distance (20 分)   The task is really simple: given N exits on a highway which forms a ...

随机推荐

  1. 详细解读 Spring AOP 面向切面编程(一)

    又是一个周末, 今天我要和大家分享的是 AOP(Aspect-Oriented Programming)这个东西,名字与 OOP 仅差一个字母,其实它是对 OOP 编程方式的一种补充,并非是取而代之. ...

  2. bcache 状态/配置 文件详细介绍(翻译自官网)

    声明: 文中 斜体带下划线  的段落为翻译不够准确的段落 原文:https://www.kernel.org/doc/Documentation/bcache.txt 官网:https://bcach ...

  3. php服务器有哪些

    服务器按照功能可以分为:文件服务器.数据库服务器.web服务器.邮件服务器.代理服务器..... 而上述所有的服务器,均可以用php做开发,比如说做web服务器,常用的构架是php+Mysql+Apa ...

  4. collectionView reloadData走了不执行cellForItemAtIndexPath

    有可能是sizeForItemAtIndexPath方法中的前几个cell没有设置大小, 这里必须设置, 哪怕是设置一个很小的值

  5. JavaScript Array返回值以及是否改变原数组。

    1.        push:最后一位新增://改变原数组         arr.push("123");         返回值是数组的长度:         var b =  ...

  6. human_pose_estimation_demo的进一步研究

    一.demo能力 OpenVINO提供了范例(human_pose_estimation_demo),能够在CPU上以较快速度识别出多人 -iE:/OpenVINO_modelZoo/head-pos ...

  7. 使用animate.css

    今天有个一前辈来看了一下我的小程序啊,说写的还行就是可以不用只按照ui给的图写界面,自己可以添加一些动态的炫酷效果,不用不知道一用吓一跳啊,用之前觉得好好一个界面为什么要搞那些花里胡哨的东西,单纯一点 ...

  8. 主流 CSS 布局(水平居中、垂直居中、居中 )

    什么是布局 html 页面的整体结构或骨架 布局不是某个技术内容 而是一种设计思想 [ 布局方式 ] 水平居中布局 垂直居中布局 居中布局( 水平 + 垂直 ) 什么是水平居中布局 水平居中布局 元素 ...

  9. NetworkManager网络通讯_NetworkManager(二)

    本文主要来实现一下自定UI(实现HUD的功能),并对Network Manger进行深入的讲解. 1)自定义manager 创建脚本CustomerUnetManger,并继承自NetworkMang ...

  10. WORKGROUP无法访问,您可能没有权限使用网络资源,请与这台服务器的管理员联系以....

    解决方法 1.启用Guest账号 在很多情况下,为了本机系统的安全,Guest账户是被禁用的,这样就无法访问该机器的共享资源,因此必须启用Guest账户. 笔者以Windows XP系统为例进行介绍. ...