Taking Bus

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1275    Accepted Submission(s): 420

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 are m
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). The i-th integer means the distance between bus station i and i+1 is di (1≤i<n). In the next m lines, each contains two integers xi and yi (1≤xi,yi≤n,xi≠yi), which means i-th person is in bus station xi and wants goto bus station yi. (1≤i≤m)

What else you should know is that for the i-th person, the bus starts at bus station ((i−1) mod n)+1 and drives to right. When the bus arrives at station n, it will turn around and drive from right to left. Similarly, When the bus arrives at station 1,
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
28

Hint

For 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
 
题意:一辆公交车最开始从左到右行驶,路上有m个乘客,第m个乘客的起始位置是 s,目标位置是 t ,公交车对于第i个乘客的起始位置是 (i-1)%n+1,并且是向右行驶,只有到达第n个点才会返回,从左向右行驶,现在给出第 i个站到第 i+1个站的时间,问每个乘客从起始点到目标点所需时间.
题解:画个图,分6种情况考虑(s0代表公交车的起始位置,s,t代表乘客的起始位置和目标位置)
一:s0<=s&&s0<=t
1.s<t
2.s>t
二:s0>=s&&s0>=t
1.s<t
2.s>t
三:s0位于 s,t之间
1.s<t
2.s>t
处理一下前缀和,就能够在O(1)时间处理所有询问了.
#include<stdio.h>
#include<iostream>
#include<string.h>
#include <stdlib.h>
#include<math.h>
#include<algorithm>
#include <queue>
using namespace std;
typedef long long LL;
const int N = ;
int d[*N];
LL sum[*N];
int main()
{
int tcase;
scanf("%d",&tcase);
int t =;
while(tcase--){
int n,m;
scanf("%d%d",&n,&m);
for(int i=;i<n;i++){
scanf("%d",&d[i]);
}
memset(sum,,sizeof(sum));
for(int i=;i<n;i++){
sum[i] = sum[i-]+d[i];
}
for(int i=;i<=m;i++){
LL time = ;
int s0 = (i-)%n+;
int s,t;
scanf("%d%d",&s,&t);
if(s0<=s&&s0<=t){
if(s<t){
time = sum[t-]-sum[s0-];
}else{
time = sum[n-]-sum[s0-]+sum[n-]-sum[t-];
}
}else if(s0>=s&&s0>=t){
if(s<t){
time = sum[n-]-sum[s0-]+sum[n-]+sum[t-];
}else{
time = sum[n-]-sum[s0-]+sum[n-]-sum[t-];
}
}else{
if(s<t){
time = sum[n-]-sum[s0-]+sum[n-]+sum[t-];
}else{
time = sum[n-]-sum[s0-]+sum[n-]-sum[t-];
}
}
printf("%lld\n",time);
}
}
return ;
}

hdu 5163(前缀和+分类讨论)的更多相关文章

  1. hdu 5511 Minimum Cut-Cut——分类讨论思想+线段树合并

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=5511 题意:割一些边使得无向图变成不连通的,并且恰好割了两条给定生成树上的边.满足非树边两段一定在给定生成 ...

  2. HDU 4609 FFT+各种分类讨论

    思路: http://www.cnblogs.com/kuangbin/archive/2013/07/24/3210565.html 其实我是懒得写了.... 一定要define int long ...

  3. HDU 5203 Rikka with wood sticks 分类讨论

    题目链接: hdu:http://acm.hdu.edu.cn/showproblem.php?pid=5203 bc(chinese):http://bestcoder.hdu.edu.cn/con ...

  4. HDU 6627 equation (分类讨论)

    2019 杭电多校 5 1004 题目链接:HDU 6627 比赛链接:2019 Multi-University Training Contest 5 Problem Description You ...

  5. HDU 6665 Calabash and Landlord (分类讨论)

    2019 杭电多校 8 1009 题目链接:HDU 6665 比赛链接:2019 Multi-University Training Contest 8 Problem Description Cal ...

  6. HDU5957 Query on a graph(拓扑找环,BFS序,线段树更新,分类讨论)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=5957 题意:D(u,v)是节点u和节点v之间的距离,S(u,v)是一系列满足D(u,x)<=k的点 ...

  7. bzoj 3779 重组病毒 好题 LCT+dfn序+线段树分类讨论

    题目大意 1.将x到当前根路径上的所有点染成一种新的颜色: 2.将x到当前根路径上的所有点染成一种新的颜色,并且把这个点设为新的根: 3.查询以x为根的子树中所有点权值的平均值. 分析 原题codec ...

  8. 【2019.8.8 慈溪模拟赛 T2】query(query)(分治+分类讨论)

    分治 首先,我们考虑分治处理此问题. 每次处理区间\([l,r]\)时,我们先处理完\([l,mid]\)和\([mid+1,r]\)两个区间的答案,然后我们再考虑计算左区间与右区间之间的答案. 处理 ...

  9. 【2019.7.25 NOIP模拟赛 T1】变换(change)(思维+大分类讨论)

    几个性质 我们通过推式子可以发现: \[B⇒AC⇒AAB⇒AAAC⇒C\] \[C⇒AB⇒AAC⇒AAAB⇒B\] 也就是说: 性质一: \(B,C\)可以相互转换. 则我们再次推式子可以发现: \[ ...

随机推荐

  1. CodeForces 805F Expected diameter of a tree 期望

    题意: 给出一个森林,有若干询问\(u, v\): 从\(u, v\)中所在子树中随机各选一个点连起来,构成一棵新树,求新树直径的期望. 分析: 回顾一下和树的直径有关的东西: 求树的直径 从树的任意 ...

  2. PC端网站转换为webApp工具

    百度开发云site App:http://siteapp.baidu.com/

  3. window.parent 、window.top及window.self 详解

    在应用有frameset或者iframe的页面时,parent是父窗口,top是最顶级父窗口(有的窗口中套了好几层frameset或者iframe),self是当前窗口. 1. window.self ...

  4. Python 模块:random 随机数生成

    Python中的random模块用于生成随机数. 使用该模块之前需要 import random 几个常用的函数用法: 1.random.random 函数原型: random.random() 用于 ...

  5. [译]8-spring bean的作用域

    在spring中使用<bean/>标签定义bean的时候,可以使用scope属性来定义bean的作用域.如果想要每次 从spring容器得到一个新创建的bean实例,可以指定scope=& ...

  6. 计算机图形学 opengl版本 第三版------胡事民 第四章 图形学中的向量工具

    计算机图形学 opengl版本 第三版------胡事民 第四章  图形学中的向量工具 一   基础 1:向量分析和变换   两个工具  可以设计出各种几何对象 点和向量基于坐标系定义 拇指指向z轴正 ...

  7. 稀疏矩阵相乘-Python版

                                          稀疏矩阵相乘-Python版 Given two sparse matrices A and B, return the r ...

  8. 网络编程--广播&组播

    广播 1.广播地址 如果用{netid, subnetid, hostid}( {网络ID,子网ID,主机ID})表示IPv4地址.那么有四种类型的广播地址,我们用-1表示所有比特位均为1的字段: 1 ...

  9. shell中的>&1和 >&2是什么意思?

    当初在shell中, 看到">&1"和">&2"始终不明白什么意思.经过在网上的搜索得以解惑.其实这是两种输出. 在 shell 程 ...

  10. CSS——(2)盒子模型与标准流

    上篇博客<CSS--(1)基础>中简单介绍了CSS的概念和几种使用方法,现在主要是介绍其的核心内容. 盒子模型 为了理解盒子模型,我们可以先从生活中的盒子入手.盒子是用来放置物品的,内部除 ...