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,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
 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <map>
 6 #include <stack>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 #define LL long long
11 using namespace std;
12 const int MAX = 4e5 + 10;
13
14 int N, D[MAX], pre[MAX], M, ans, a, b, ans2;
15 struct node
16 {
17     int L, R, val;
18 }P[MAX];
19
20 void build(int dep, int l, int r)
21 {
22     P[dep].L = l, P[dep].R = r, P[dep].val = 0;
23     if (l == r)
24     {
25         pre[l] = dep;
26         return;
27     }
28     int mid = (l + r) >> 1;
29     build(dep << 1, l, mid);
30     build((dep << 1) + 1, mid + 1, r);
31 }
32
33 void update(int r, int b)
34 {
35     P[r].val += b;
36     if (r == 1) return ;
37     update(r >> 1, b);
38 }
39
40 void query(int dep, int l, int r)
41 {
42     if (P[dep].L == l && P[dep].R == r)
43     {
44         ans += P[dep].val;
45         return ;
46     }
47     int mid = (P[dep].L + P[dep].R) >> 1;
48     if (r <= mid) query(dep << 1, l, r);
49     else if (l > mid) query((dep << 1) + 1, l, r);
50     else
51     {
52         query(dep << 1, l, mid);
53         query((dep << 1) + 1, mid + 1, r);
54     }
55 }
56
57 int main()
58 {
59 //    freopen("Date1.txt", "r", stdin);
60     scanf("%d", &N);
61     build(1, 1, N);
62     for (int i = 1; i <= N; ++ i)
63     {
64         scanf("%d", &D[i]);
65         update(pre[i], D[i]);
66     }
67
68     scanf("%d", &M);
69     while (M --)
70     {
71         ans = 0;
72         scanf("%d%d", &a, &b);
73         if (b < a) swap(a, b);
74         if (a == b - 1) ans += D[a];
75         else query(1, a, b - 1);
76         ans2 = ans, ans = 0;
77
78         if (a - 1 == 1) ans += D[1];
79         else if (a - 1 > 1) query(1, 1, a - 1);
80         if (b == N) ans += D[N];
81         else query(1, b, N);
82         printf("%d\n", min(ans, ans2));
83     }
84     return 0;
85 }

原文地址:https://www.cnblogs.com/GetcharZp/p/9589797.html

时间: 2024-10-10 13:06:50

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

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

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 con

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

题意: 输入一个正整数N(<=1e5),代表出口的数量,接下来输入N个正整数表示当前出口到下一个出口的距离.接着输入一个正整数M(<=10000),代表询问的次数,每次询问输入两个出口的序号,输出他们之间的最小距离. 代码: #define HAVE_STRUCT_TIMESPEC#include<bits/stdc++.h>using namespace std;int dis[100007],sum[100007];int main(){ ios::sync_with_stdi

1046 Shortest Distance (20 分)(简单模拟,前缀和)

题目链接:https://pintia.cn/problem-sets/994805342720868352/problems/994805435700199424 注意:1.这是一个环,两点之间的距离有两种,一是顺时针,二是逆时针,求出其中的一个距离,另一个距离用这个圆的总距离减去得到,输出两个距离之间最小的那个 2.注意区分x,y的大小,有可能下标大的在前面. 3.用到了前缀和的思想 1 #include <algorithm> 2 #include <iostream> 3

pat 1077 Kuchiguse(20 分) (字典树)

1077 Kuchiguse(20 分) The Japanese language is notorious for its sentence ending particles. Personal preference of such particles can be considered as a reflection of the speaker's personality. Such a preference is called "Kuchiguse" and is often

HDU-6070-二分+线段树

Dirt Ratio Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others)Total Submission(s): 2522    Accepted Submission(s): 1138Special Judge Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculate

pat 1035 Password(20 分)

1035 Password(20 分) To prepare for PAT, the judge sometimes has to generate random passwords for the users. The problem is that there are always some confusing passwords since it is hard to distinguish 1 (one) from l (L in lowercase), or 0 (zero) fro

hiho一下20周 线段树的区间修改

线段树的区间修改 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 对于小Ho表现出的对线段树的理解,小Hi表示挺满意的,但是满意就够了么?于是小Hi将问题改了改,又出给了小Ho: 假设货架上从左到右摆放了N种商品,并且依次标号为1到N,其中标号为i的商品的价格为Pi.小Hi的每次操作分为两种可能,第一种是修改价格--小Hi给出一段区间[L, R]和一个新的价格NewP,所有标号在这段区间中的商品的价格都变成NewP.第二种操作是询问--小Hi给出一段区间[L, R]

【hihoCoder】第20周 线段树

题目: 输入 每个测试点(输入文件)有且仅有一组测试数据. 每组测试数据的第1行为一个整数N,意义如前文所述. 每组测试数据的第2行为N个整数,分别描述每种商品的重量,其中第i个整数表示标号为i的商品的重量Pi. 每组测试数据的第3行为一个整数Q,表示小Hi进行的操作数. 每组测试数据的第N+4~N+Q+3行,每行分别描述一次操作,每行的开头均为一个属于0或1的数字,分别表示该行描述一个询问和一次商品的价格的更改两种情况.对于第N+i+3行,如果该行描述一个询问,则接下来为两个整数Li, Ri,