Letters CodeForces - 978C (二分)

Time limit4000 ms

Memory limit262144 kB

There are nn dormitories in Berland State University, they are numbered with integers from 11 to nn. Each dormitory consists of rooms, there are aiai rooms in ii-th dormitory. The rooms in ii-th dormitory are numbered from 11 to aiai.

A postman delivers letters. Sometimes there is no specific dormitory and room number in it on an envelope. Instead of it only a room number among all rooms of all nn dormitories is written on an envelope. In this case, assume that all the rooms are numbered from 11 to a1+a2++ana1+a2+⋯+an and the rooms of the first dormitory go first, the rooms of the second dormitory go after them and so on.

For example, in case n=2n=2, a1=3a1=3 and a2=5a2=5 an envelope can have any integer from 11 to 88 written on it. If the number 77 is written on an envelope, it means that the letter should be delivered to the room number 44 of the second dormitory.

For each of mm letters by the room number among all nn dormitories, determine the particular dormitory and the room number in a dormitory where this letter should be delivered.

Input

The first line contains two integers nn and m(1n,m2105)(1≤n,m≤2⋅105) — the number of dormitories and the number of letters.

The second line contains a sequence a1,a2,,ana1,a2,…,an (1ai1010)(1≤ai≤1010), where aiai equals to the number of rooms in the ii-th dormitory. The third line contains a sequence b1,b2,,bmb1,b2,…,bm (1bja1+a2++an)(1≤bj≤a1+a2+⋯+an), where bjbj equals to the room number (among all rooms of all dormitories) for the jj-th letter. All bjbj are given in increasing order.

Output

Print mm lines. For each letter print two integers ff and kk — the dormitory number f(1fn)(1≤f≤n) and the room number kk in this dormitory (1kaf)(1≤k≤af) to deliver the letter.

Examples

Input
3 6
10 15 12
1 9 12 23 26 37
Output
1 1
1 9
2 2
2 13
3 1
3 12
Input
2 3
5 10000000000
5 6 9999999999
Output
1 5
2 1
2 9999999994

Note

In the first example letters should be delivered in the following order:

  • the first letter in room 11 of the first dormitory 
  • the second letter in room 99 of the first dormitory 
  • the third letter in room 22 of the second dormitory 
  • the fourth letter in room 1313 of the second dormitory 
  • the fifth letter in room 11 of the third dormitory 
  • the sixth letter in room 1212 of the third dormitory

题意:n个寝室,每个寝室多个房间(1-a[i]),一共m封信只知道房间号,现在要求把信送到第几个寝室的第几个房间里去,房间号从第一个寝室到第n个寝室的第一个房间到最后一个房间排下去

题意:用个二分查找吧,省点时间,二分才658ms,爆搜1600多ms

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<sstream>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<map>
using namespace std;
#define INF 0x3f3f3f3f
const int maxn=200004;

int main()
{
    long long int n,m;
    cin>>n>>m;
    long long int sum[maxn]={0};
    long long int a[maxn];
    for(long long int i=1;i<=n;i++)
    {
        cin>>a[i];
        sum[i]=sum[i-1]+a[i];
    }
    while(m--)
    {
        int flag;
        long long  int t;
        scanf("%lld",&t);
        long long int l=0,r=n;
        while(r-l>1)
        {
            long long int mid=(l+r)/2;
            if(sum[mid]>t)
                r=mid;
            else
                l=mid;
        }
            if(t-sum[l]==0)
                printf("%lld %lld\n",l,a[l]);
                else printf("%lld %lld\n",l+1,t-sum[l]);
    }
}

猜你喜欢

转载自www.cnblogs.com/smallhester/p/9499813.html