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. node-sass安装不成功的问题

    SASS_BINARY_SITE=https://npm.taobao.org/mirrors/node-sass/ npm install node-sass 简单粗暴的执行上述的命令.

  2. sqlsever存储过程学习笔记

    1,创建数据表 use test create table money( id ,) primary key, money int, monetary_unity char ); 2,考虑到货币单位的 ...

  3. The GNU C Library

    Any Unix-like operating system needs a C library: the library which defines the ``system calls'' and ...

  4. Javascript 属性高级写法

    http://www.cnblogs.com/YuanSong/p/3899287.html

  5. 《Cracking the Coding Interview》——第18章:难题——题目11

    2014-04-29 04:30 题目:给定一个由‘0’或者‘1’构成的二维数组,找出一个四条边全部由‘1’构成的正方形(矩形中间可以有‘0’),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...

  6. SSH集成cxf 发布restful webservice

    首先讲一下什么是restful webservice ,这个问题网上一搜有很多博文去长篇大论的介绍它,但是最后你看完了也会觉得云里雾里的,所以我在这里简单的讲一下我理解的rest webservice ...

  7. PYTHON -MYSQLDB安装遇到的问题和解决办法

    目前下载的mysqldb在window下没有exe安装包了,只有源码. 使用python setup.py install 命令安装, 报错如下: 异常信息如下: F:\devtools\MySQL- ...

  8. 孤荷凌寒自学python第六十一天在Fedora28版的linux系统上找搭建本地Mongodb数据服务

    孤荷凌寒自学python第六十一天在Fedora28版的linux系统上找搭建本地Mongodb数据服务 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第七天.成功在本地搭建 ...

  9. == 与 equals 之区别

    "=="和equals方法究竟有什么区别? (单独把一个东西说清楚,然后再说清楚另一个,这样,它们的区别自然就出来了,混在一起说,则很难说清楚) ==操作符专门用来比较两个变量的值 ...

  10. try-catch-finally容易犯的错误

    测试环境 JDK1.8 1. catch中包含return //有return的时候 输出13423 //无return的时候 输出134234 public class Trycatch { pub ...