英文题面:

De Prezer loves movies and series. He has watched the
Troy for like 100 times and also he is a big fan of
Supernatural series.So, he did some researches and found a cursed object which had
n lights on it and initially all of them were turned off.Because of his love to the
Troy, he called that object
Troy.

He looked and saw a note on it in
Khikulish (language of people of Khikuland): "Ma in hame rah umadim, hala migi ... ? ... Mage man De Prezer am k mikhay mano ... ? .... Man se sale ... To boro .... beshur manam miram ... o mishuram".

He doesn't know Khikulish, so just ignored the note and tested the
Troy.

He realized that the light number
i stays turned on for exactly ai seconds, and then it turns itself off (if it is turned on, in time
t, in time t + ai - 1 it will be turned on, but on time
t + ai it won't be) and the next light will be turned on (if
i < n, next light is the light number
i + 1, otherwise it is light with number
1).

For example if n = 2 and we turn on the first light in time
0, it will be turned on in hole interval
[0, a1) and in hole interval
[a1, a1 + a2) the second light will be turned on and so on.

In time 0 he turns on the light number
1.

De Prezer also loves query.So he gives you
q queries.In each query he will give you integer
t (the time a revengeful ghost attacked him) and you should print the number of the light that is turned on, in time
t.

Input

The first line of input contains two integers
n and q.

The second line contains n space separated integers,
a1, a2, ..., an .

The next q lines, each line contains a single integer
t .

1 ≤ n, q ≤ 105

1 ≤ ai ≤ 109

1 ≤ t ≤ 1018

Output

For each query, print the answer in one line.

        就是说有n盏灯,然后给出每盏灯亮的时间(第一盏从时刻0開始亮直到时刻0+a1-1,第二盏在a1时刻亮起,以此类推),然后题目给出q组询问,询问内容是一个t,要你给出t时刻亮的时哪盏灯。

我的做法是每次询问就来一次二分查找,log2n的复杂度再盛上10^5次询问能够接受。

Sample test(s):

Input
5 7
1 2 3 4 5
1
2
3
7
14
15
16
Output
2
2
3
4
5
1
2

        这组測试数据还是业界良心的。爆出了可能出错的坑,这一串灯是循环的首尾相接地亮。所以那个t是10^18这么大,要取模。

代码:

#include <iostream>
#include<stdio.h>
using namespace std; long long int start[100005];//每盏灯開始亮时刻
long long int End[100005];//每盏灯亮完时刻
int n,q,a;
long long int t; int work(long long int ask)//每次二分查找函数
{
int p=1;
int p1=1;
int p2=n;
while(ask<start[p]||ask>End[p])
{
p=(p1+p2)/2;
if(ask>=start[p]&&ask<=End[p])
{
break;
}else if(ask>=start[p1]&&ask<=End[p1])
{
p=p1;
break;
}else if(ask>=start[p2]&&ask<=End[p2])
{
p=p2;
break;
}else if(ask<start[p]&&ask>End[p1])
{
p2=p;
}else if(ask>End[p]&&ask<start[p2])
{
p1=p;
}
} return p;//p就是那盏灯
} int main()
{
scanf("%d%d",&n,&q);
start[1]=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a);
End[i]=start[i]+a-1;
start[i+1]=start[i]+a;
}
while(q--)
{
scanf("%I64d",&t);
printf("%d\n",work(t%start[n+1]));
}
return 0;
}

最新文章

  1. VPB和OSGGIS安装
  2. NPOI、MyXls、Aspose.Cells 导入导出Excel(转)
  3. zipimport.ZipImportError: can&#39;t decompress data; zlib not available 解决办法
  4. August 20th 2016 Week 34th Saturday
  5. du: fts_read 失败: 无法分配内存
  6. maven install 时提示“程序包 javax.crypto不存在”
  7. Oracle10GODP连接11G数据库,出现ORA - 1017用户名/口令无效; 登录被拒绝 的问题
  8. docker无法连接进程
  9. 【 UVALive - 4287】Proving Equivalences (SCC缩点)
  10. jq不识别拼接的对象id的解决方案
  11. Sprite Kit编程指南(1)-深入Sprite Kit
  12. [HeadFirst-HTMLCSS学习笔记][第十三章表格]
  13. singleton pattern
  14. jQuery软键盘插件
  15. 使用nodejs进行WEB开发
  16. MVC Anti-XSS方案
  17. python enumerate() 函数的使用方法
  18. [Swift]Xcode标记:MARK、TODO、FIXME
  19. 刷《剑指offer》笔记
  20. Instruments之Leaks学习

热门文章

  1. 刷题总结—— Scout YYF I(poj3744 矩阵快速幂+概率dp)
  2. git常用命令符
  3. cf 542E - Playing on Graph
  4. 【AtCoder Regular Contest 076 F】Exhausted (贪心)
  5. Codevs 4633 [Mz]树链剖分练习
  6. 【HDOJ5538】House Building(计算几何)
  7. Ncut matlab 代码bug 修复
  8. 转 C/C++内存分配方式与存储区
  9. Fio IO性能测试
  10. hdu 2669(扩展欧几里得)