Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //package CodeChef;
- import java.io.BufferedReader;
- import java.io.BufferedWriter;
- import java.io.InputStreamReader;
- import java.io.OutputStreamWriter;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.Comparator;
- /*public*/ class CHEFRES_FAST {
- public CHEFRES_FAST() {
- super();
- }
- public static long modifiedBS(Period[] periods, long t) {
- if( t >= periods[periods.length-1].end) {
- return -1L;
- }
- else if(t <= periods[0].start){
- return (periods[0].start - t);
- }
- else {
- int lo = 0;
- int hi = periods.length - 1;
- int mid;
- while(lo < hi) {
- mid = ((hi-lo+1)/2);
- if( t >= periods[mid].start ) {
- lo = mid;
- }
- else {
- hi = mid-1;
- }
- }
- if( (t >= periods[lo].start) && ( t < periods[lo].end ) ) {
- return 0L;
- }
- else {
- return ( periods[lo+1].start - t );
- }
- }
- }
- public static void main(String[] args)throws Exception {
- BufferedReader in = new BufferedReader (new InputStreamReader(System.in));
- BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
- String[] s = in.readLine().split(" ");
- int t = Integer.parseInt(s[0]);
- //ArrayList<Long> ans = new ArrayList<>();
- while(t-- > 0) {
- s = in.readLine().split(" ");
- int n = Integer.parseInt(s[0]);
- int m = Integer.parseInt(s[1]);
- Period[] periods = new Period[n];
- long[] ans = new long[m];
- for(int i=0; i<n; i++) {
- s = in.readLine().split(" ");
- periods[i] = new Period(Long.parseLong(s[0]),Long.parseLong(s[1]));
- }
- Arrival[] times = new Arrival[m];
- for(int i=0; i<m; i++) {
- s = in.readLine().split(" ");
- times[i] = new Arrival(Long.parseLong(s[0]),i);
- }
- Arrays.sort( periods, new MyComparator() );
- Arrays.sort( times, new MyComparator1() );
- int p1 = 0;
- int p2 = 0;
- while(times[p2].time < periods[0].start) {
- ans[times[p2].customerNumber] = (periods[0].start - times[p2].time);
- p2++;
- }
- while( (p1 < n) && (p2 < m) ) {
- if( (times[p2].time >= periods[p1].start) ) {
- if( times[p2].time < periods[p1].end ) {
- ans[times[p2].customerNumber] = 0;
- p2++;
- }
- else /*if( times[p2].time > periods[p1].end )*/ {
- if( p1 < (n-1) ) {
- if( times[p2].time >= periods[p1+1].start ) {
- p1++;
- }
- else {
- ans[times[p2].customerNumber] = periods[p1+1].start - times[p2].time;
- p2++;
- }
- }
- else {
- ans[times[p2].customerNumber] = -1;
- p2++;
- }
- }
- }
- }
- for(int i=0; i<ans.length; i++) {
- log.write(Long.toString(ans[i])+"\n");
- }
- }
- log.flush();
- in.close();
- }
- }
- class Period {
- long start;
- long end;
- Period(long s, long e) {
- start = s;
- end = e;
- }
- }
- class Arrival {
- long time;
- int customerNumber;
- Arrival(long t, int cNum) {
- time = t;
- customerNumber = cNum;
- }
- }
- class MyComparator1 implements Comparator<Arrival> {
- public int compare(Arrival a1, Arrival a2) {
- if(a1.time < a2.time) {
- return -1;
- }
- else {
- return 1;
- }
- }
- }
- class MyComparator implements Comparator<Period>{
- public int compare(Period p1, Period p2) {
- if(p1.start < p2.start) {
- return -1;
- }
- else {
- return 1;
- }
- }
- }
Add Comment
Please, Sign In to add comment