|
| 1 | +```java |
| 2 | +import java.util.*; |
| 3 | +import java.io.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) nextLine(); |
| 24 | + return st.nextToken(); |
| 25 | + } |
| 26 | + |
| 27 | + int nextInt() throws Exception { |
| 28 | + return Integer.parseInt(nextToken()); |
| 29 | + } |
| 30 | + |
| 31 | + long nextLong() throws Exception { |
| 32 | + return Long.parseLong(nextToken()); |
| 33 | + } |
| 34 | + |
| 35 | + double nextDouble() throws Exception { |
| 36 | + return Double.parseDouble(nextToken()); |
| 37 | + } |
| 38 | + |
| 39 | + void close() throws Exception { |
| 40 | + bw.flush(); |
| 41 | + bw.close(); |
| 42 | + } |
| 43 | + |
| 44 | + void write(String content) throws Exception { |
| 45 | + bw.write(content); |
| 46 | + } |
| 47 | + |
| 48 | +} |
| 49 | + |
| 50 | +class SegTree { |
| 51 | + int[] tree; |
| 52 | + SegTree(int size) { |
| 53 | + tree = new int[size*4]; |
| 54 | + } |
| 55 | + |
| 56 | + void update(int s, int e, int i, int v, int n) { |
| 57 | + if(s == e) { |
| 58 | + tree[n] = Math.max(tree[n], v); |
| 59 | + return; |
| 60 | + } |
| 61 | + int m = (s+e)>>1; |
| 62 | + if(i <= m) update(s,m,i,v,n*2); |
| 63 | + else update(m+1,e,i,v,n*2+1); |
| 64 | + tree[n] = Math.max(tree[n*2],tree[n*2+1]); |
| 65 | + } |
| 66 | + |
| 67 | + int find(int s, int e, int l, int r, int n) { |
| 68 | + if(l>r || l>e || r<s) return 0; |
| 69 | + if(l<=s && e<=r) return tree[n]; |
| 70 | + int m = (s+e)>>1; |
| 71 | + return Math.max(find(s,m,l,r,n*2), find(m+1,e,l,r,n*2+1)); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +public class Main { |
| 76 | + |
| 77 | + static IOController io; |
| 78 | + |
| 79 | + // |
| 80 | + |
| 81 | + static int N, Q; |
| 82 | + static int[][] points; |
| 83 | + static SegTree seg1, seg2; |
| 84 | + |
| 85 | + static int index(List<Integer> arr, int x) { |
| 86 | + int s = 0, e = arr.size(), m = (s+e)>>1; |
| 87 | + while(s<e) { |
| 88 | + int v = arr.get(m); |
| 89 | + if(v < x) s = m+1; |
| 90 | + else e = m; |
| 91 | + m = (s+e)>>1; |
| 92 | + } |
| 93 | + return m; |
| 94 | + } |
| 95 | + |
| 96 | + public static void main(String[] args) throws Exception { |
| 97 | + |
| 98 | + io = new IOController(); |
| 99 | + |
| 100 | + N = io.nextInt(); |
| 101 | + Q = io.nextInt(); |
| 102 | + points = new int[N][]; |
| 103 | + List<Integer> list1 = new ArrayList<>(); |
| 104 | + List<Integer> list2 = new ArrayList<>(); |
| 105 | + for(int i=0;i<N;i++) { |
| 106 | + points[i] = new int[]{io.nextInt(),io.nextInt(),i+1}; |
| 107 | + list1.add(points[i][1]-points[i][0]); |
| 108 | + list2.add(points[i][1]+points[i][0]); |
| 109 | + } |
| 110 | + |
| 111 | + Collections.sort(list1); |
| 112 | + Collections.sort(list2); |
| 113 | + |
| 114 | + List<Integer> arr1 = new ArrayList<>(); |
| 115 | + List<Integer> arr2 = new ArrayList<>(); |
| 116 | + arr1.add(list1.get(0)); |
| 117 | + for(int i=1;i<N;i++) if(!list1.get(i).equals(list1.get(i-1))) arr1.add(list1.get(i)); |
| 118 | + arr2.add(list2.get(0)); |
| 119 | + for(int i=1;i<N;i++) if(!list2.get(i).equals(list2.get(i-1))) arr2.add(list2.get(i)); |
| 120 | + |
| 121 | + seg1 = new SegTree(arr1.size()); |
| 122 | + seg2 = new SegTree(arr2.size()); |
| 123 | + |
| 124 | + int[] ans = new int[N+1]; |
| 125 | + Arrays.sort(points, (a,b) -> a[1]==b[1] ? a[0]-b[0] : a[1]-b[1]); |
| 126 | + for(int i=0;i<N;) { |
| 127 | + int Y = points[i][1]; |
| 128 | + List<int[]> update1 = new ArrayList<>(); |
| 129 | + List<int[]> update2 = new ArrayList<>(); |
| 130 | + while(i<N && points[i][1] == Y) { |
| 131 | + int x = points[i][0], y = points[i][1], n = points[i][2]; |
| 132 | + int i1 = index(arr1, y-x), i2 = index(arr2, y+x); |
| 133 | + ans[n] = Math.max(seg1.find(0,arr1.size()-1,i1,arr1.size()-1,1), seg2.find(0,arr2.size()-1,i2,arr2.size()-1,1)) + 1; |
| 134 | + update1.add(new int[]{i1,ans[n]}); |
| 135 | + update2.add(new int[]{i2,ans[n]}); |
| 136 | + i++; |
| 137 | + } |
| 138 | + for(int[] up : update1) seg1.update(0,arr1.size()-1,up[0],up[1],1); |
| 139 | + for(int[] up : update2) seg2.update(0,arr2.size()-1,up[0],up[1],1); |
| 140 | + } |
| 141 | + |
| 142 | + while(Q-->0) io.write(ans[io.nextInt()] + "\n"); |
| 143 | + |
| 144 | + io.close(); |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | +} |
| 149 | +``` |
0 commit comments