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. 一步一步学Linq to sql(一):预备知识

    什么是Linq to sql Linq to sql(或者叫DLINQ)是LINQ(.NET语言集成查询)的一部分,全称基于关系数据的 .NET 语言集成查询,用于以对象形式管理关系数据,并提供了丰富 ...

  2. .NET非常棒的开源项目,给你意想不到的东西!

    http://www.cnblogs.com/Leo_wl/p/4560146.html

  3. Windows Phone 图片扩展类

    using System.IO; using System.Text; using System.Net; using System.Threading.Tasks; using System.Win ...

  4. webdriver--定位一组元素+iframe表单切换

    定位一组元素:find_elements,返回的是list,所以可以用列表的索引对列表里的某个元素操作,也可以用for循环访问list,依次操作各元素 driver.find_elements_by_ ...

  5. hnust 档案管理

    问题 E: 档案管理 时间限制: 1 Sec  内存限制: 128 MB提交: 274  解决: 105[提交][状态][讨论版] 题目描述 X老师管理着学校的档案室,经常会有其他的老师来档案室存文件 ...

  6. STL之map&multimap使用简介

    map 1.insert 第一种:用insert函数插入pair数据 #include <map> #include <string> #include <iostrea ...

  7. 【转】TCP通信的三次握手和四次撒手的详细流程(顿悟)

    TCP(Transmission Control Protocol) 传输控制协议 三次握手 TCP是主机对主机层的传输控制协议,提供可靠的连接服务,采用三次握手确认建立一个连接: 位码即tcp标志位 ...

  8. 【bzoj3779】重组病毒 LCT+树上倍增+DFS序+树状数组区间修改区间查询

    题目描述 给出一棵n个节点的树,每一个节点开始有一个互不相同的颜色,初始根节点为1. 定义一次感染为:将指定的一个节点到根的链上的所有节点染成一种新的颜色,代价为这条链上不同颜色的数目. 现有m次操作 ...

  9. 【Luogu】P2465山贼集团(树形状压DP)

    题目链接 写了个70分暴力还挂了,第一遍提交只拿了十分……海星 首先建虚拟节点多叉树转成二叉,然后子集枚举DP 设g[x][i]是以x为根的子树内山贼集合i,x啥都不选也没贡献的时候的最大价值 f[x ...

  10. 真·APIO2018滚粗记

    有人说只有大佬才会说滚粗啊爆零啊…… 然而我真滚粗真爆零啊…… D1: 听课,然后夏眠. 咦折纸……哦好吧太神仙了. 咦AI……好妙啊好妙啊. 咦二分……哇还有wqs二分这种神奇操作,学学学. (我是 ...