Codeforces Hello2015第一题Cursed Query

英文题面:

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;
}
时间: 2024-10-10 05:16:23

Codeforces Hello2015第一题Cursed Query的相关文章

Codeforces Gym 100571A A. Cursed Query 离线

A. Cursed QueryTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100571/problem/A Description 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 di

HDU 2966 Aragorn&#39;s Story 树链剖分第一题 基础题

Problem Description Our protagonist is the handsome human prince Aragorn comes from The Lord of the Rings. One day Aragorn finds a lot of enemies who want to invade his kingdom. As Aragorn knows, the enemy has N camps out of his kingdom and M edges c

2016/1/12 第一题 输出 i 出现次数 第二题 用for循环和if条件句去除字符串中空格 第三题不用endwith 实现尾端字符查询

1 import java.util.Scanner; 2 3 4 public class Number { 5 6 private static Object i; 7 8 /* 9 *第一题 mingrikejijavabu中字符“i” 出现了几次,并将结果输出*/ 10 public static void main(String[] args) { 11 12 String r ="imingrikejijavabi"; 13 14 15 //第一种 截取 16 int a=

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

第五届在线编程大赛月赛第一题:完全平方数的个数

第五届在线编程大赛月赛第一题:完全平方数的个数 题目详情: 给定整数区间[A,B]问其中有多少个完全平方数. 输入格式: 多组数据,包含两个正整数A,B 1<=A<=B<=2000000000. 输出格式: 每组数据输出一行包含一个整数,表示闭区间[A,B]中包含的完全平方数的个数. 答题说明: 输入样例 1 1 1 2 3 10 3 3 输出样例: 1 1 2 0 java代码: import java.util.Scanner; public class One { public s

POJ 1364 King --差分约束第一题

题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析:典型差分约束题,变换,令Ti = SUM(Xj) (0<=j<=i).  则表达式(1)可以看做T(a+b)-T(a-1) > k,也就是T(a-1)-T(a+b) < -k,又因为全是整数,所以T(a-1)-T(a+b) <= -k-1.  同理,(2)看做T(a+b)-T(

【BZOJ4927】第一题 双指针+DP(容斥?)

[BZOJ4927]第一题 Description 给定n根直的木棍,要从中选出6根木棍,满足:能用这6根木棍拼 出一个正方形.注意木棍不能弯折.问方案数. 正方形:四条边都相等.四个角都是直角的四边形. Input 第一行一个整数n. 第二行包含n个整数ai,代表每根木棍的长度. n ≤ 5000, 1 ≤ ai ≤ 10^7 Output 一行一个整数,代表方案数. Sample Input 8 4 5 1 5 1 9 4 5 Sample Output 3 题解:这...这不是沈阳集训的原

NOIP2005-普及组复赛-第一题-陶陶摘苹果

题目描述 Description 陶陶家的院子里有一棵苹果树,每到秋天树上就会结出10个苹果.苹果成熟的时候,陶陶就会跑去摘苹果.陶陶有个30厘米高的板凳,当她不能直接用手摘到苹果的时候,就会踩到板凳上再试试. 现在已知10个苹果到地面的高度,以及陶陶把手伸直的时候能够达到的最大高度,请帮陶陶算一下她能够摘到的苹果的数目.假设她碰到苹果,苹果就会掉下来. 输入输出格式 Input/output 输入格式:输入文件apple.in包括两行数据.第一行包含10个100到200之间(包括100和200

2014百度之星资格赛第一题

Energy Conversion Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11867    Accepted Submission(s): 2861 Problem Description 魔法师百小度也有遇到难题的时候-- 现在,百小度正在一个古老的石门面前,石门上有一段古老的魔法文字,读懂这种魔法文字需要耗费大量的能量和大