Codeforces#481(Div3) C. Letters
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 n
For example, in case n=2n=2, a1=3a1=3 and a2=5a2=5 an envelope can have any integer from 1
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.
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.
OutputPrint 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.
ExamplesinputCopy3 6 10 15 12 1 9 12 23 26 37outputCopy
1 1 1 9 2 2 2 13 3 1 3 12inputCopy
2 3 5 10000000000 5 6 9999999999outputCopy
1 5 2 1 2 9999999994程式碼:
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int m = scan.nextInt();
long[] sum = new long[n];
sum[0] = scan.nextLong();
for(int i = 1; i < n; i++) sum[i] = sum[i-1] + scan.nextLong();
int ans = 0;
for(int i = 0; i < m; i++){
long t = scan.nextLong();
while(t>sum[ans]) ans++;
System.out.print((ans+1)+" ");
if(ans == 0){
System.out.println(t);
}else{
System.out.println(t-sum[ans-1]);
}
}
}
}