C. Letters
time limit per test
4 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are ai.

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 a1+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 4 of the second dormitory.

For each of n 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 (1≤n,m≤2⋅105) — the number of dormitories and the number of letters.

The second line contains a sequence increasing order.

Output

Print (1≤k≤af) to deliver the letter.

Examples
input
Copy
3 6
10 15 12
1 9 12 23 26 37
output
Copy
1 1
1 9
2 2
2 13
3 1
3 12
input
Copy
2 3
5 10000000000
5 6 9999999999
output
Copy
1 5
2 1
2 9999999994
Note

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

  • the first letter in room 1 of the first dormitory
  • the second letter in room 9 of the first dormitory
  • the third letter in room 2 of the second dormitory
  • the fourth letter in room 13 of the second dormitory
  • the fifth letter in room 1 of the third dormitory
  • the sixth letter in room 12 of the third dormitory

题意:n个宿舍,每个宿舍有ai个房间,再给你m个数,问每个数在第几个宿舍的第几个房间。

分析:很简单的模拟,但是数据量很大,a数组里面每个数都要是long long,而且如果直接遍历求解的话会T掉,我第一个想到的就是学长在寒假教过的二分查找,很长时间没写二分有点忘。。。改了几次才过了样例,然后交上去就过了

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 #define fi first
 4 #define se second
 5 #define ll long long
 6 #define pb push_back
 7 const int N=2e5+5;
 8 ll a[N];
 9 ll b[N];
10 int main()
11 {
12     int n,m;
13     scanf("%d %d",&n,&m);
14     for (int i=1;i<=n;i++)
15     {
16         scanf("%I64d",&a[i]);
17         b[1]=a[1];
18         b[i]=b[i-1]+a[i];
19     }
20     int flag1,flag2;
21     while(m--)
22     {
23         int flag;
24         ll t;
25         scanf("%I64d",&t);
26         ll l=0,r=n;
27         while(l+1<r)
28         {
29             ll mid=(l+r)/2;
30             if(b[mid]>t)    flag=1;
31             if(b[mid]<=t)   flag=2;
32             if(flag==1) r=mid;
33             else if(flag==2)    l=mid;
34         }
35         if(t-b[l]==0)
36             printf("%I64d %I64d\n",l,a[l]);
37         else printf("%I64d %I64d\n",l+1,t-b[l]);
38     }
39 }

 

相关文章:

  • 2021-11-14
  • 2022-12-23
  • 2021-09-07
  • 2022-12-23
  • 2021-06-09
  • 2021-12-09
  • 2021-08-10
  • 2022-01-26
猜你喜欢
  • 2022-12-23
  • 2022-02-24
  • 2022-12-23
  • 2022-12-23
  • 2021-11-17
  • 2021-09-15
相关资源
相似解决方案