Advertisement
ec1117

Untitled

Jul 9th, 2020
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.49 KB | None | 0 0
  1. import java.util.*;
  2. import java.io.*;
  3. import java.io.BufferedReader;
  4. import java.awt.Point;
  5.  
  6. public class slingshot{
  7. public static final String TASKNAME = "slingshot";
  8. public static long mod= 1000000007;
  9. public static Debug db;
  10.  
  11. public static void main(String[] args) throws IOException {
  12. // InputReader in = new InputReader(System.in);
  13. // PrintWriter out = new PrintWriter(System.out);
  14. InputReader in = new InputReader(new FileInputStream(TASKNAME+".in"));
  15. PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(TASKNAME+".out")));
  16. Autocompletion solver = new Autocompletion();
  17. db=new Debug(System.getSecurityManager()==null);
  18. solver.solve(1, in, out);
  19. out.close();
  20. }
  21. static class Autocompletion {
  22.  
  23. public void solve(int testNumber, InputReader in, PrintWriter out) {
  24. int n=in.nextInt();
  25. int m=in.nextInt();
  26. SegmentsTreeSimple seg1=new SegmentsTreeSimple(n+m);
  27. SegmentsTreeSimple seg2=new SegmentsTreeSimple(n+m);
  28. SegmentsTreeSimple seg3=new SegmentsTreeSimple(n+m);
  29. SegmentsTreeSimple seg4=new SegmentsTreeSimple(n+m);
  30. for(int i=0;i<n+m;i++) {
  31. seg1.set(i,Integer.MAX_VALUE);
  32. seg2.set(i,Integer.MAX_VALUE);
  33. seg3.set(i,Integer.MAX_VALUE);
  34. seg4.set(i,Integer.MAX_VALUE);
  35. }
  36. Triple[] sli=new Triple[n];
  37. for(int i=0;i<n;i++) {
  38. sli[i]=new Triple(in.nextInt(),in.nextInt(),in.nextInt());
  39. }
  40. Pair ret[]=new Pair[m];//ret is sorted by x
  41. int ret2[]=new int[m];
  42. for(int i=0;i<m;i++) {
  43. ret[i]=new Pair(in.nextInt(),in.nextInt());
  44. ret2[i]=Math.abs(ret[i].y-ret[i].x);
  45. }
  46.  
  47. Triple com[]=new Triple[n+m];//sorted by y
  48. HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
  49. for(int i=0;i<n;i++) {
  50. com[i]=new Triple(sli[i].y,i,0);
  51. }
  52. for(int i=0;i<m;i++) {
  53. com[i+n]=new Triple(ret[i].y,i,1);
  54. }
  55. Arrays.sort(com);
  56. for(int i=0;i<n+m;i++) {
  57. map.put(com[i].x, i);
  58. }//coord compress
  59.  
  60. Triple upd[]=new Triple[n+m];//sorted by x
  61. for(int i=0;i<n;i++) {
  62. upd[i]=new Triple(sli[i].x,i,0);
  63. }
  64. for(int i=0;i<m;i++) {
  65. upd[i+n]=new Triple(ret[i].x,i,1);
  66. }
  67. Arrays.sort(upd);
  68. for(int i=0;i<n+m;i++) {
  69. Triple tmp=upd[i];
  70. if(upd[i].z==0) {//sling
  71. Triple q=sli[tmp.y];//sx, sy, st
  72. seg2.set(map.get(q.y), Math.min(seg2.get(map.get(q.y)), -q.x+q.y+q.z));
  73. seg3.set(map.get(q.y), Math.min(seg3.get(map.get(q.y)), -q.x-q.y+q.z));
  74. }
  75. else {//query
  76. Pair q=ret[tmp.y];//ax, ay
  77. ret2[tmp.y]=(int) Math.min(ret2[tmp.y],(long)q.x-q.y+seg2.getMin(map.get(q.y), n+m));
  78. ret2[tmp.y]=(int) Math.min(ret2[tmp.y],(long)q.x+q.y+seg3.getMin(0, map.get(q.y)+1));
  79. }
  80. }
  81. for(int i=n+m-1;i>=0;i--) {
  82. Triple tmp=upd[i];
  83. if(tmp.z==0) {
  84. Triple q=sli[tmp.y];//sx, sy, st
  85. seg1.set(map.get(q.y), Math.min(seg1.get(map.get(q.y)), q.x+q.y+q.z));
  86. seg4.set(map.get(q.y), Math.min(seg4.get(map.get(q.y)), q.x-q.y+q.z));
  87. }
  88. else {
  89. Pair q=ret[tmp.y];//ax, ay
  90. ret2[tmp.y]=(int) Math.min(ret2[tmp.y],(long)-q.x-q.y+seg1.getMin(map.get(q.y), n+m));
  91. ret2[tmp.y]=(int) Math.min(ret2[tmp.y],(long)-q.x+q.y+seg4.getMin(0, map.get(q.y)+1));
  92. }
  93. }
  94. for(int i=0;i<m;i++) {
  95. out.println(ret2[i]);
  96. }
  97. }
  98. }
  99. static class SegmentsTreeSimple {
  100. int n;
  101. int[] min;
  102. int[] max;
  103. int size;
  104.  
  105. public SegmentsTreeSimple(int n) {
  106. this.n = n;
  107. size = 1;
  108. while (size <= n) {
  109. size *= 2;
  110. }
  111. min = new int[2 * size];
  112. max = new int[2 * size];
  113. }
  114.  
  115. void set(int index, int value) {
  116. int i = size + index;
  117. min[i] = max[i] = value;
  118. while (i > 1) {
  119. i /= 2;
  120. min[i] = Math.min(min[2 * i], min[2 * i + 1]);
  121. max[i] = Math.max(max[2 * i], max[2 * i + 1]);
  122. }
  123. }
  124.  
  125. int get(int index) {
  126. return min[size + index];
  127. }
  128.  
  129. int getMax(int from, int to) {
  130. from += size;
  131. to += size;
  132. int res = Integer.MIN_VALUE;
  133. while (from < to) {
  134. if (from % 2 == 1) {
  135. res = Math.max(res, max[from]);
  136. from++;
  137. }
  138. if (to % 2 == 1) {
  139. to--;
  140. res = Math.max(res, max[to]);
  141. }
  142. from /= 2;
  143. to /= 2;
  144. }
  145. return res;
  146. }
  147.  
  148. int getMin(int from, int to) {
  149. from += size;
  150. to += size;
  151. int res = Integer.MAX_VALUE;
  152. while (from < to) {
  153. if (from % 2 == 1) {
  154. res = Math.min(res, min[from]);
  155. from++;
  156. }
  157. if (to % 2 == 1) {
  158. to--;
  159. res = Math.min(res, min[to]);
  160. }
  161. from /= 2;
  162. to /= 2;
  163. }
  164. return res;
  165. }
  166. }
  167. static class Pair implements Comparable<Pair>{
  168. int x;
  169. int y;
  170. Pair(int a, int b){
  171. x=a;
  172. y=b;
  173. }
  174. @Override
  175. public int compareTo(Pair arg0) {
  176. if(arg0.x!=x)return x-arg0.x;
  177. return y-arg0.y;
  178. }
  179. }
  180. static class Triple implements Comparable<Triple>{
  181. int x;
  182. int y;
  183. int z;
  184. Triple(int a, int b, int c){
  185. x=a;
  186. y=b;
  187. z=c;
  188. }
  189. @Override
  190. public int compareTo(Triple arg0) {
  191. if(arg0.x!=x)return x-arg0.x;
  192. return y-arg0.y;
  193. }
  194. }
  195. static void bounds() {
  196. int arr[]=new int[9];
  197. System.out.println(arr[9]);
  198. }
  199. static class InputReader {
  200. public BufferedReader reader;
  201. public StringTokenizer tokenizer;
  202.  
  203. public InputReader(InputStream stream) {
  204. reader = new BufferedReader(new InputStreamReader(stream), 32768);
  205. tokenizer = null;
  206. }
  207.  
  208. public String next() {
  209. while (tokenizer == null || !tokenizer.hasMoreTokens()) {
  210. try {
  211. tokenizer = new StringTokenizer(reader.readLine());
  212. } catch (IOException e) {
  213. throw new RuntimeException(e);
  214. }
  215. }
  216. return tokenizer.nextToken();
  217. }
  218.  
  219. public int nextInt() {
  220. return Integer.parseInt(next());
  221. }
  222. public long nextLong() {
  223. return Long.parseLong(next());
  224. }
  225. public int[] nextIntArr(int n) {
  226. int arr[]=new int[n];
  227. for(int i=0;i<n;i++) {
  228. arr[i]=this.nextInt();
  229. }
  230. return arr;
  231. }
  232. }
  233. public static class Debug {
  234. private boolean allowDebug;
  235.  
  236. public Debug(boolean allowDebug) {
  237. this.allowDebug = allowDebug;
  238. }
  239.  
  240. private void outputName(String name) {
  241. System.out.print(name + " = ");
  242. }
  243.  
  244. public void debug(String name, int x) {
  245. if (!allowDebug) {
  246. return;
  247. }
  248.  
  249. outputName(name);
  250. System.out.println("" + x);
  251. }
  252.  
  253. public void debug(String name, long x) {
  254. if (!allowDebug) {
  255. return;
  256. }
  257. outputName(name);
  258. System.out.println("" + x);
  259. }
  260.  
  261. public void debug(String name, double x) {
  262. if (!allowDebug) {
  263. return;
  264. }
  265. outputName(name);
  266. System.out.println("" + x);
  267. }
  268.  
  269. public void debug(String name, int[] x) {
  270. if (!allowDebug) {
  271. return;
  272. }
  273. outputName(name);
  274. System.out.println(Arrays.toString(x));
  275. }
  276.  
  277. public void debug(String name, long[] x) {
  278. if (!allowDebug) {
  279. return;
  280. }
  281. outputName(name);
  282. System.out.println(Arrays.toString(x));
  283. }
  284.  
  285. public void debug(String name, double[] x) {
  286. if (!allowDebug) {
  287. return;
  288. }
  289. outputName(name);
  290. System.out.println(Arrays.toString(x));
  291. }
  292.  
  293. public void debug(String name, Pair[] x) {
  294. if (!allowDebug) {
  295. return;
  296. }
  297. outputName(name);
  298. StringBuilder sb = new StringBuilder("[");
  299. int cnt=0;
  300. for(Pair y:x) {
  301. sb.append("("+y.x+","+y.y+')');
  302. if (cnt != x.length-1)sb.append(", ");
  303. cnt++;
  304. }
  305. System.out.println(sb.append("]").toString());
  306. }
  307.  
  308. public void debug(String name, Object x) {
  309. if (!allowDebug) {
  310. return;
  311. }
  312. outputName(name);
  313. System.out.println("" + x);
  314. }
  315.  
  316. public void debug(String name, Object... x) {
  317. if (!allowDebug) {
  318. return;
  319. }
  320. outputName(name);
  321. System.out.println(Arrays.deepToString(x));
  322. }
  323. }
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement