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 ...
随机推荐
- PMP 德尔菲技术
1.德尔菲技术,必须遵守以下几个规则: 每个专家只与主持人单线联系. 专家之间完全背靠背,更不能进行讨论.为保证专家提出独立见解,甚至需要把专家分散在不同的物理地点. 专家以匿名的书面形式提出意见. ...
- [Luogu4447] [AHOI2018初中组]分组
题目描述 小可可的学校信息组总共有n 个队员,每个人都有一个实力值a[i]a[i]a[i].现在,一年一度的编程大赛就要到了,小可可的学校获得了若干个参赛名额,教练决定把学校信息组的nnn 个队员分成 ...
- C# 控件 RichTextBox 显示行号,并且与Panel相互联动
我们在使用到WINFORM窗体工作中,要求RichTextBox 加入行号: 之前有看到大牛们写的,但是太复杂繁多,而且有用双TextBox进行联动,非常不错,今天我们尝试RichTextBox +P ...
- 利用phar实行php反序列化命令执行漏洞复现
利用phar实行php反序列化命令执行(测试环境复现) 前言 一般说到反序列化漏洞,第一反应都是unserialize()函数.然而安全研究员Sam Thomas分享了议题”It’s a PHP un ...
- Java代码优化建议
总结日常Java开发常见优化策略,持续更新. 尽可能使用局部变量 调用方法时传递的参数以及在调用中创建的临时变量都保存在栈中,速度较快,其他变量,如静态变量.实例变量等,都在堆中创建,速度较慢.另外, ...
- python问题:IndentationError:expected an indented block
Python语言是一款对缩进非常敏感的语言,给很多初学者带来了困惑,即便是很有经验的Python程序员,也可能陷入陷阱当中.最常见的情况是tab和空格的混用会导致错误,或者缩进不对,而这是用肉眼无法分 ...
- 在已有 Windows10 系统的基础上,安装 Ubuntu17.10 系统(新版 BIOS)
1.第一步,下载Ubuntu系统镜像 2.第二步,制作启动U盘,使用UltralSO,步骤:打开文件——选择iso文件——启动——写入硬盘映像——选择U盘——写入 3.第三步,分区,在Windows徽 ...
- POJO和JavaBean
1.POJO POJO(Plain Ordinary Java Object):POJO就是一个简单的普通的Java对象,它不包含业务逻辑或持久逻辑等,但不是JavaBean.EntityBean等, ...
- Airflow速用
Airflow是Apache用python编写的,用到了 flask框架及相关插件,rabbitmq,celery等(windows不兼容):. 主要实现的功能 编写 定时任务,及任务间的编排: 提供 ...
- django-模板之now标签(七)
1.在settings.py中设置成中国时区 2.index.html 3.显示