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. PMBOK(第六版) PMP笔记——《十一》第十一章(项目风险管理)

    PMBOK(第六版) PMP笔记——<十一>第十一章(项目风险管理) 第十一章 风险管理: 项目的独特性导致项目充满风险,项目风险是一种不确定的事件或条件,可能发生.将 要发生,也可能不发 ...

  2. [JZOJ5456]【NOIP2017提高A组冲刺11.6】奇怪的队列

    Description nodgd的粉丝太多了,每天都会有很多人排队要签名.今天有

  3. React+Antd+Antd-Img-Crop实现上传固定大小的裁剪头像或者图片(且可控制图片数量)

    见章知著 1024,程序员们节日快乐!本文主要讲述react配合antd以及react-img-crop第三方库实现一个可控的图片上传功能. 运行项目 需要具有node环境 第三方库安装 1.antd ...

  4. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  5. SpringCloud之RefreshScope 源码解读

    SpringCloud之RefreshScope @Scope 源码解读 Scope(org.springframework.beans.factory.config.Scope)是Spring 2. ...

  6. C#事件浅淡(1)

    最近在写C#,感觉事件这个机制很好,可是怎么实现自己定义的事件呢?查了资料有的不全有的不完整,有的太深,自己写一个简单的例子. 原则 1,定义一个事件信息类(标准的都继承EventArgs) 2.定义 ...

  7. 实现基于netty的web框架,了解一下

    上一篇写了,基于netty实现的rpc的微框架,其中详细介绍netty的原理及组件,这篇就不过多介绍 这篇实现基于netty的web框架,你说netty强不强,文中有不对的地方,欢迎大牛指正 先普及几 ...

  8. 轻量级CNN模型之squeezenet

    SqueezeNet 论文地址:https://arxiv.org/abs/1602.07360 和别的轻量级模型一样,模型的设计目标就是在保证精度的情况下尽量减少模型参数.核心是论文提出的一种叫&q ...

  9. C加加学习之路 2——两招让你成为牛X的T型人才

    有个小伙伴在微信上问我: 我刚工作半年,有时候对于Java的发展方向有点迷茫,Java的范围是在是太广了,我有时候会不知道从哪开始入手,我想问一下,您有什么好的建议吗? 我理解这位朋友的问题是:工作中 ...

  10. angularjs通过ng-bind-html指令和$sce服务绑定html

    代码: <!doctype html> <html lang="en"> <head> <meta charset="UTF-8 ...