pat 1046 Shortest Distance(20 分) (线段树)
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,105]), followed by N integer distances D1 D2 ⋯ DN, where Di is the distance between the i-th and the (i+1)-st exits, and DN 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 (≤104), 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 107.
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 分) (线段树)的更多相关文章
- PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642
PAT (Advanced Level) Practice 1046 Shortest Distance (20 分) 凌宸1642 题目描述: The task is really simple: ...
- PAT 甲级 1046 Shortest Distance (20 分)(前缀和,想了一会儿)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
- 1046 Shortest Distance (20 分)
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a si ...
- 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 ...
- 1046 Shortest Distance (20分)
The task is really simple: given N exits on a highway which forms a simple cycle, you are supposed t ...
- 【PAT甲级】1046 Shortest Distance (20 分)
题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序 ...
- PAT A1046 Shortest Distance (20 分)
题目提交一直出现段错误,经过在网上搜索得知是数组溢出,故将数组设置的大一点 AC代码 #include <cstdio> #include <algorithm> #defin ...
- 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 ...
- PAT 1046 Shortest Distance
1046 Shortest Distance (20 分) The task is really simple: given N exits on a highway which forms a ...
随机推荐
- 21.Linux系统服务之大坑
1.CentOS6启动流程 2.CentOS7启动流程 3.C6和C7的区别 4.运行级别C6&C7 0 关机 1 单用户模式 (超级权限 必须面对实体硬件) 2 暂未使用 3 字符界面(黑框 ...
- 3. SOFAJRaft源码分析— 是如何进行选举的?
开篇 在上一篇文章当中,我们讲解了NodeImpl在init方法里面会初始化话的动作,选举也是在这个方法里面进行的,这篇文章来从这个方法里详细讲一下选举的过程. 由于我这里介绍的是如何实现的,所以请大 ...
- [UWP]使用GetAlphaMask制作阴影
1. 前言 最近常常接触到GetAlphaMask,所以想写这篇文章介绍下GetAlphaMask怎么使用.其实GetAlphaMask的使用场景十分有限,Github上能搜到的内容都是用来配合Dro ...
- nuxt.js部署vue应用到服务端过程
由于seo的需要,最近将项目移植道nuxt.js下采用ssr渲染 移植完成后,一路顺畅,但是到了要部署到服务器端上时候,还是个头疼的问题,但最终还是顺利完成.现在记录一下部署中的过程. 注:部署时候过 ...
- 小白学微信小程序
奔着实用性的目的-测试孩子的认字量,开发了一个微信小程序-测字大王.上下班路上看书看了一个星期,代码前后共写一个星期.现在小程序已经对外开放,share下我的开发过程吧. 一 工具准备 首先先过一篇 ...
- Canonical 开源 MicroK8 | 云原生生态周报 Vol. 25
业界要闻 1.Canonical 开源 MicroK8 面向工作站和边缘/物联网的零运维 Kubernetes!MicroK8 是 Canonical 提供的一款功能强大的企业级 Kubernetes ...
- vue.js 使用 vue-router 修改页面标题
module.exports = { name: 'myComponent', data: {} route{ data: function(){ document.title = "页面标 ...
- Java并发编程之线程池的使用
1. 为什么要使用多线程? 随着科技的进步,现在的电脑及服务器的处理器数量都比较多,以后可能会越来越多,比如我的工作电脑的处理器有8个,怎么查看呢? 计算机右键--属性--设备管理器,打开属性窗口,然 ...
- js响应式布局
<!DOCTYPE html> <html class="mobile"> <head> <meta charset="UTF- ...
- Python中的is和==的区别
Python中的is和==的区别 1. is 是比较内存地址id() a = "YongJie" b = "YongJie" print(id(a)) #233 ...