补题之路:CF978C Letters

本文解析了一个关于信封投递的问题,涉及到n个公寓,每个公寓有不同数量的房间。通过使用前缀和与二分查找算法,快速确定任意指定编号的房间位于哪个公寓及其具体位置。代码实现简洁高效。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述

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 mm (1≤n,m≤2⋅105)(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 (1≤ai≤1010)(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 (1≤bj≤a1+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 ff (1≤f≤n)(1≤f≤n) and the room number kk in this dormitory (1≤k≤af)(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

题意
n个公寓,第i个有a[i]个房间。问编号为b的是第几个公寓的第几个房间。
思路
前缀和,二分查找第一个大于b的公寓t,减掉前面的部分和就好啦~
一遍过,舒服
代码

#include <iostream>
#include <algorithm>
using namespace std;
long long a[200005],b[200005],sum[200005];
int main(){
 int n,m;
 cin >> n >> m;
 for(int i = 1;i <= n;i++){
  scanf("%lld",&a[i]);
  sum[i] = sum[i-1]+a[i];
 }
 
 for(int i = 0;i < m;i++){
  scanf("%lld",&b[i]);
  int t = lower_bound(sum+1,sum+1+n,b[i])-sum;
  printf("%d ",t);
  long long num = b[i] - sum[t-1];
  printf("%lld\n",num);
 }
 
 return 0;
}

p.s.
记得开long long

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值