Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- import java.io.*;
- import java.io.BufferedReader;
- import java.awt.Point;
- public class slingshot{
- public static final String TASKNAME = "slingshot";
- public static long mod= 1000000007;
- public static Debug db;
- public static void main(String[] args) throws IOException {
- // InputReader in = new InputReader(System.in);
- // PrintWriter out = new PrintWriter(System.out);
- InputReader in = new InputReader(new FileInputStream(TASKNAME+".in"));
- PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(TASKNAME+".out")));
- Autocompletion solver = new Autocompletion();
- db=new Debug(System.getSecurityManager()==null);
- solver.solve(1, in, out);
- out.close();
- }
- static class Autocompletion {
- public void solve(int testNumber, InputReader in, PrintWriter out) {
- int n=in.nextInt();
- int m=in.nextInt();
- SegmentsTreeSimple seg1=new SegmentsTreeSimple(n+m);
- SegmentsTreeSimple seg2=new SegmentsTreeSimple(n+m);
- SegmentsTreeSimple seg3=new SegmentsTreeSimple(n+m);
- SegmentsTreeSimple seg4=new SegmentsTreeSimple(n+m);
- for(int i=0;i<n+m;i++) {
- seg1.set(i,Integer.MAX_VALUE);
- seg2.set(i,Integer.MAX_VALUE);
- seg3.set(i,Integer.MAX_VALUE);
- seg4.set(i,Integer.MAX_VALUE);
- }
- Triple[] sli=new Triple[n];
- for(int i=0;i<n;i++) {
- sli[i]=new Triple(in.nextInt(),in.nextInt(),in.nextInt());
- }
- Pair ret[]=new Pair[m];//ret is sorted by x
- int ret2[]=new int[m];
- for(int i=0;i<m;i++) {
- ret[i]=new Pair(in.nextInt(),in.nextInt());
- ret2[i]=Math.abs(ret[i].y-ret[i].x);
- }
- Triple com[]=new Triple[n+m];//sorted by y
- HashMap<Integer,Integer> map=new HashMap<Integer,Integer>();
- for(int i=0;i<n;i++) {
- com[i]=new Triple(sli[i].y,i,0);
- }
- for(int i=0;i<m;i++) {
- com[i+n]=new Triple(ret[i].y,i,1);
- }
- Arrays.sort(com);
- for(int i=0;i<n+m;i++) {
- map.put(com[i].x, i);
- }//coord compress
- Triple upd[]=new Triple[n+m];//sorted by x
- for(int i=0;i<n;i++) {
- upd[i]=new Triple(sli[i].x,i,0);
- }
- for(int i=0;i<m;i++) {
- upd[i+n]=new Triple(ret[i].x,i,1);
- }
- Arrays.sort(upd);
- for(int i=0;i<n+m;i++) {
- Triple tmp=upd[i];
- if(upd[i].z==0) {//sling
- Triple q=sli[tmp.y];//sx, sy, st
- seg2.set(map.get(q.y), Math.min(seg2.get(map.get(q.y)), -q.x+q.y+q.z));
- seg3.set(map.get(q.y), Math.min(seg3.get(map.get(q.y)), -q.x-q.y+q.z));
- }
- else {//query
- Pair q=ret[tmp.y];//ax, ay
- ret2[tmp.y]=(int) Math.min(ret2[tmp.y],(long)q.x-q.y+seg2.getMin(map.get(q.y), n+m));
- ret2[tmp.y]=(int) Math.min(ret2[tmp.y],(long)q.x+q.y+seg3.getMin(0, map.get(q.y)+1));
- }
- }
- for(int i=n+m-1;i>=0;i--) {
- Triple tmp=upd[i];
- if(tmp.z==0) {
- Triple q=sli[tmp.y];//sx, sy, st
- seg1.set(map.get(q.y), Math.min(seg1.get(map.get(q.y)), q.x+q.y+q.z));
- seg4.set(map.get(q.y), Math.min(seg4.get(map.get(q.y)), q.x-q.y+q.z));
- }
- else {
- Pair q=ret[tmp.y];//ax, ay
- ret2[tmp.y]=(int) Math.min(ret2[tmp.y],(long)-q.x-q.y+seg1.getMin(map.get(q.y), n+m));
- ret2[tmp.y]=(int) Math.min(ret2[tmp.y],(long)-q.x+q.y+seg4.getMin(0, map.get(q.y)+1));
- }
- }
- for(int i=0;i<m;i++) {
- out.println(ret2[i]);
- }
- }
- }
- static class SegmentsTreeSimple {
- int n;
- int[] min;
- int[] max;
- int size;
- public SegmentsTreeSimple(int n) {
- this.n = n;
- size = 1;
- while (size <= n) {
- size *= 2;
- }
- min = new int[2 * size];
- max = new int[2 * size];
- }
- void set(int index, int value) {
- int i = size + index;
- min[i] = max[i] = value;
- while (i > 1) {
- i /= 2;
- min[i] = Math.min(min[2 * i], min[2 * i + 1]);
- max[i] = Math.max(max[2 * i], max[2 * i + 1]);
- }
- }
- int get(int index) {
- return min[size + index];
- }
- int getMax(int from, int to) {
- from += size;
- to += size;
- int res = Integer.MIN_VALUE;
- while (from < to) {
- if (from % 2 == 1) {
- res = Math.max(res, max[from]);
- from++;
- }
- if (to % 2 == 1) {
- to--;
- res = Math.max(res, max[to]);
- }
- from /= 2;
- to /= 2;
- }
- return res;
- }
- int getMin(int from, int to) {
- from += size;
- to += size;
- int res = Integer.MAX_VALUE;
- while (from < to) {
- if (from % 2 == 1) {
- res = Math.min(res, min[from]);
- from++;
- }
- if (to % 2 == 1) {
- to--;
- res = Math.min(res, min[to]);
- }
- from /= 2;
- to /= 2;
- }
- return res;
- }
- }
- static class Pair implements Comparable<Pair>{
- int x;
- int y;
- Pair(int a, int b){
- x=a;
- y=b;
- }
- @Override
- public int compareTo(Pair arg0) {
- if(arg0.x!=x)return x-arg0.x;
- return y-arg0.y;
- }
- }
- static class Triple implements Comparable<Triple>{
- int x;
- int y;
- int z;
- Triple(int a, int b, int c){
- x=a;
- y=b;
- z=c;
- }
- @Override
- public int compareTo(Triple arg0) {
- if(arg0.x!=x)return x-arg0.x;
- return y-arg0.y;
- }
- }
- static void bounds() {
- int arr[]=new int[9];
- System.out.println(arr[9]);
- }
- static class InputReader {
- public BufferedReader reader;
- public StringTokenizer tokenizer;
- public InputReader(InputStream stream) {
- reader = new BufferedReader(new InputStreamReader(stream), 32768);
- tokenizer = null;
- }
- public String next() {
- while (tokenizer == null || !tokenizer.hasMoreTokens()) {
- try {
- tokenizer = new StringTokenizer(reader.readLine());
- } catch (IOException e) {
- throw new RuntimeException(e);
- }
- }
- return tokenizer.nextToken();
- }
- public int nextInt() {
- return Integer.parseInt(next());
- }
- public long nextLong() {
- return Long.parseLong(next());
- }
- public int[] nextIntArr(int n) {
- int arr[]=new int[n];
- for(int i=0;i<n;i++) {
- arr[i]=this.nextInt();
- }
- return arr;
- }
- }
- public static class Debug {
- private boolean allowDebug;
- public Debug(boolean allowDebug) {
- this.allowDebug = allowDebug;
- }
- private void outputName(String name) {
- System.out.print(name + " = ");
- }
- public void debug(String name, int x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, long x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, double x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, int[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.toString(x));
- }
- public void debug(String name, long[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.toString(x));
- }
- public void debug(String name, double[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.toString(x));
- }
- public void debug(String name, Pair[] x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- StringBuilder sb = new StringBuilder("[");
- int cnt=0;
- for(Pair y:x) {
- sb.append("("+y.x+","+y.y+')');
- if (cnt != x.length-1)sb.append(", ");
- cnt++;
- }
- System.out.println(sb.append("]").toString());
- }
- public void debug(String name, Object x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println("" + x);
- }
- public void debug(String name, Object... x) {
- if (!allowDebug) {
- return;
- }
- outputName(name);
- System.out.println(Arrays.deepToString(x));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement