File tree Expand file tree Collapse file tree 1 file changed +80
-0
lines changed
Expand file tree Collapse file tree 1 file changed +80
-0
lines changed Original file line number Diff line number Diff line change 1+ ``` java
2+
3+ import java.util.* ;
4+ import java.io.* ;
5+
6+ class Main {
7+
8+ // IO field
9+ static BufferedReader br = new BufferedReader (new InputStreamReader (System . in));
10+ static BufferedWriter bw = new BufferedWriter (new OutputStreamWriter (System . out));
11+ static StringTokenizer st;
12+
13+ static void nextLine () throws Exception {st = new StringTokenizer (br .readLine ());}
14+ static int nextInt() {return Integer . parseInt(st. nextToken());}
15+ static long nextLong() {return Long . parseLong(st. nextToken());}
16+ static void bwEnd() throws Exception {bw. flush();bw. close();}
17+
18+ // Additional field
19+
20+ public static void main(String [] args) throws Exception {
21+
22+ int T = Integer . parseInt(br. readLine());
23+ while (T -- > 0 ) {
24+
25+ int N = Integer . parseInt(br. readLine());
26+ int [][] A = new int [N ][2 ];
27+ for (int i= 0 ;i< N ;i++ ) {
28+ nextLine();
29+ A [i][0 ] = nextInt();
30+ A [i][1 ] = nextInt();
31+ }
32+ Arrays . sort(A , (a,b) - > {
33+ if (a[0 ] == b[0 ]) return a[1 ]- b[1 ];
34+ return a[0 ]- b[0 ];
35+ });
36+
37+ int [][] ans = new int [N + 1 ][2 ];
38+ int num = 1 ;
39+
40+ int prev = 0 ;
41+
42+ for (int i= 0 ;i< N ;) {
43+ int x = A [i][0 ], y = A [i][1 ], yy = y, p = i;
44+ while (i< N && x == A [i][0 ]) {
45+ yy = A [i][1 ];
46+ i++ ;
47+ }
48+ if (y == prev) {
49+ for (int j= p;j< i;j++ ) {
50+ ans[num][0 ] = A [j][0 ];
51+ ans[num][1 ] = A [j][1 ];
52+ num++ ;
53+ }
54+ prev = yy;
55+ }
56+ else {
57+ for (int j= i- 1 ;j>= p;j-- ) {
58+ ans[num][0 ] = A [j][0 ];
59+ ans[num][1 ] = A [j][1 ];
60+ num++ ;
61+ }
62+ prev = y;
63+ }
64+ }
65+
66+ nextLine();
67+ int M = nextInt();
68+ while (M -- > 0 ) {
69+ int a = nextInt();
70+ bw. write(ans[a][0 ]+ " " + ans[a][1 ]+ " \n " );
71+ }
72+
73+ }
74+
75+ bwEnd();
76+ }
77+
78+ }
79+
80+ ```
You can’t perform that action at this time.
0 commit comments