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. 一个简单的同步集群的shell脚本

    编写一个xsync文件 然后放在/usr/local/bin 目录下面 xsync文件如下: #!/bin/bash #1 获取输入参数个数,如果没有参数,直接退出 pcount=$# if((pco ...

  2. 每天一个Linux命令(3):ls命令

    ls命令用来显示目标列表,在Linux中是使用率较高的命令.ls命令的输出信息可以进行彩色加亮显示,以分区不同类型的文件. 语法 ls(选项)(参数) 选项 -a:显示所有档案及目录(ls内定将档案名 ...

  3. Python 3基础教程10-全局变量和局部变量

    本文来讲讲全局变量和局部变量,前面学习了函数的基本使用,所以,这里就要注意变量的使用和访问权限. 试试下面的demo.py

  4. MySQL、MongoDB、Redis 数据库之间的区别与使用(本章迭代更新)

    MySQL.MongoDB.Redis 数据库之间的区别与使用 MySQL.MongoDB.Redis 数据库之间的区别与使用(本章迭代更新) update:2019年2月20日 15:21:19(本 ...

  5. 孤荷凌寒自学python第五十七天初次尝试使用python来连接远端MongoDb数据库

    孤荷凌寒自学python第五十七天初次尝试使用python来连接远端MongoDb数据库 (完整学习过程屏幕记录视频地址在文末) 今天是学习mongoDB数据库的第三天.感觉这个东西学习起来还是那么困 ...

  6. tomcat集群和负载均衡的实现(session同步)

      (一)环境说明 (1)服务器有4台,一台安装apache,三台安装tomcat (2)apache2.0.55.tomcat5.5.15.jk2.0.4.jdk1.5.6或jdk1.4.2 (3) ...

  7. HDU 4288 Coder ( 离散化 + 离线 + 线段树 )

    这题跟ZOJ 3606的解题思路很相似. 题意:有3中操作:1.向集合中增加一个数x(1≤x≤1e9):2.从集合中删去一个数x(保证这个数存在):3.查询集合中所有位置满足i%5==3的数a[i]的 ...

  8. [转] mysql分区性能初探

    本文转自:http://www.cnblogs.com/acpp/archive/2010/08/09/1795464.html 一,      分区概念  分区允许根据指定的规则,跨文件系统分配单个 ...

  9. nio的reactor模式

    转自:http://blog.csdn.net/it_man/article/details/38417761 线程状态转换图 就是非阻塞IO 采用多路分发方式举个例子吧,你服务器做一个聊天室,按照以 ...

  10. DataBase -- Note I

    SQL对大小写不敏感! SQL DML和DDL:可以把SQL分为两个部分:数据操作语言(DML)和数据定义语言(DDL) SQL(结构化查询语言)是用于执行查询的语法.但是SQL语言也包含用于更新.插 ...