Taking Bus
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 100 Accepted Submission(s): 55
Problem Description
Bestland has a very long road and there are
n
bus station along the road, which are numbered 1 to
n
from left to right. There arem
persons wanting to take the bus to some other station. You task is to find the time needed for each person. Note: All the other information you need is below. Please read the statment carefully.
bus station along the road, which are numbered 1 to
from left to right. There are
persons wanting to take the bus to some other station. You task is to find the time needed for each person. Note: All the other information you need is below. Please read the statment carefully.
Input
There are multiple test cases. The first line of input contains an integer
T (1≤T≤60) ,
indicating the number of test cases. For each test case: The first line contains two integers
n and m (2≤n,m≤105) ,
indicating the number of bus stations and number of people. In the next line, there are
n−1
integers,d1,d2,…,dn−1
(1≤di≤109 ).
Thei -th
integer means the distance between bus stationi
andi+1
isdi
(1≤i<n ).
In the nextm
lines, each contains two integersxi
andyi
(1≤xi,yi≤n,xi≠yi ),
which meansi -th
person is in bus stationxi
and wants goto bus stationyi .
(1≤i≤m)
indicating the number of test cases. For each test case: The first line contains two integers
indicating the number of bus stations and number of people. In the next line, there are
integers,
(
The
integer means the distance between bus station
and
is
(
In the next
lines, each contains two integers
and
(
which means
person is in bus station
and wants goto bus station
What else you should know is that for the
person, the bus starts at bus station
and drives to right. When the bus arrives at station
it will turn around and drive from right to left. Similarly, When the bus arrives at station
it will turn around and drive from left to right. You can assume that the bus drives one meter per second. And you should only consider the time that the bus drives and ignore the others.
Output
For each person, you should output one integer which is the minimum time needed before arriving bus station
yi .
Sample Input
1 7 3 2 3 4 3 4 5 1 7 4 5 5 4
Sample Output
21 10 28HintFor the first person, the bus starts at bus station 1, and the person takes in bus at time 0. After 21 seconds, the bus arrives at bus station 7. So the time needed is 21 seconds. For the second person, the bus starts at bus station 2. After 7 seconds, the bus arrives at bus station 4 and the person takes in the bus. After 3 seconds, the bus arrives at bus station 5. So the time needed is 10 seconds. For the third person, the bus starts at bus station 3. After 7 seconds, the bus arrives at bus station 5 and the person takes in the bus. After 9 seconds, the bus arrives at bus station 7 and the bus turns around. After 12 seconds, the bus arrives at bus station 4. So the time needed is 28 seconds.
Source
Recommend
hujie | We have carefully selected several similar problems for you: 5165 5164 5161 5160 5157
处理一个前缀和就行
/************************************************************************* > File Name: bc27b.cpp > Author: ALex > Mail: 405045132@qq.com > Created Time: 2015年01月24日 星期六 19时11分51秒 ************************************************************************/ #include <map> #include <set> #include <queue> #include <stack> #include <vector> #include <cmath> #include <cstdio> #include <cstdlib> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const int N = 200010; struct node { int s, e; }peo[N]; LL xis[N]; LL dist[N]; int main () { int t; scanf("%d", &t); while (t--) { int s, e; int n, m; scanf("%d%d", &n, &m); memset (xis, 0, sizeof(xis)); for (int i = 1; i < n; ++i) { scanf("%I64d", &dist[i]); } xis[1] = 0; for (int i = n; i < 2 * n - 2; ++i) { dist[i] = dist[2 * n - i - 2]; } for (int i = 2; i < 2 * n - 1; ++i) { xis[i] = xis[i - 1] + dist[i - 1]; } for (int i = 1; i <= m; ++i) { scanf("%d%d", &s, &e); int start = (i - 1) % n + 1; if (s <= e) { if (start <= s) { printf("%I64d\n", xis[e] - xis[start]); } else { printf("%I64d\n", xis[n] - xis[start] + xis[n] + xis[e]); } } else { printf("%I64d\n", xis[n] - xis[start] + xis[n] - xis[e]); } } } return 0; }