結果

問題 No.1014 competitive fighting
ユーザー uwiuwi
提出日時 2020-03-20 23:06:23
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,175 ms / 2,000 ms
コード長 8,176 bytes
コンパイル時間 6,253 ms
コンパイル使用メモリ 85,136 KB
実行使用メモリ 91,188 KB
最終ジャッジ日時 2023-09-21 19:21:36
合計ジャッジ時間 38,422 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,756 KB
testcase_01 AC 43 ms
49,464 KB
testcase_02 AC 46 ms
49,944 KB
testcase_03 AC 46 ms
49,700 KB
testcase_04 AC 45 ms
50,328 KB
testcase_05 AC 1,049 ms
82,988 KB
testcase_06 AC 884 ms
81,184 KB
testcase_07 AC 997 ms
81,256 KB
testcase_08 AC 847 ms
81,692 KB
testcase_09 AC 1,007 ms
83,872 KB
testcase_10 AC 257 ms
65,920 KB
testcase_11 AC 773 ms
84,252 KB
testcase_12 AC 892 ms
81,504 KB
testcase_13 AC 458 ms
65,060 KB
testcase_14 AC 435 ms
66,492 KB
testcase_15 AC 494 ms
66,860 KB
testcase_16 AC 93 ms
51,984 KB
testcase_17 AC 267 ms
58,516 KB
testcase_18 AC 754 ms
72,592 KB
testcase_19 AC 844 ms
81,212 KB
testcase_20 AC 363 ms
62,828 KB
testcase_21 AC 672 ms
69,472 KB
testcase_22 AC 559 ms
68,096 KB
testcase_23 AC 301 ms
58,372 KB
testcase_24 AC 311 ms
58,496 KB
testcase_25 AC 164 ms
57,828 KB
testcase_26 AC 664 ms
70,312 KB
testcase_27 AC 261 ms
58,700 KB
testcase_28 AC 281 ms
59,700 KB
testcase_29 AC 82 ms
51,876 KB
testcase_30 AC 984 ms
82,940 KB
testcase_31 AC 702 ms
69,156 KB
testcase_32 AC 75 ms
50,660 KB
testcase_33 AC 193 ms
57,292 KB
testcase_34 AC 826 ms
80,984 KB
testcase_35 AC 1,175 ms
91,188 KB
testcase_36 AC 752 ms
78,492 KB
testcase_37 AC 106 ms
52,296 KB
testcase_38 AC 714 ms
71,192 KB
testcase_39 AC 409 ms
65,456 KB
testcase_40 AC 686 ms
72,792 KB
testcase_41 AC 536 ms
67,144 KB
testcase_42 AC 1,153 ms
85,708 KB
testcase_43 AC 178 ms
56,820 KB
testcase_44 AC 971 ms
84,320 KB
testcase_45 AC 606 ms
68,752 KB
testcase_46 AC 208 ms
55,692 KB
testcase_47 AC 393 ms
63,928 KB
testcase_48 AC 841 ms
79,036 KB
testcase_49 AC 139 ms
54,148 KB
testcase_50 AC 1,081 ms
85,164 KB
testcase_51 AC 662 ms
69,404 KB
testcase_52 AC 147 ms
54,016 KB
testcase_53 AC 995 ms
84,492 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Comparator;
import java.util.InputMismatchException;

public class F {
	InputStream is;
	PrintWriter out;
	String INPUT = "";
	
	void solve()
	{
		int n = ni();
		int[][] rs = new int[n][];
		for(int i = 0;i < n;i++){
			rs[i] = new int[]{ni(), ni(), ni(), i};
		}
		Arrays.sort(rs, new Comparator<int[]>() {
			public int compare(int[] a, int[] b) {
				return a[0] - b[0];
			}
		});
		
		int[][] es = new int[2*n][];
		for(int i = 0;i < n;i++){
			es[i] = new int[]{rs[i][0], rs[i][1] - rs[i][2], 1, i};
			es[i+n] = new int[]{rs[i][1] - rs[i][2], rs[i][0], 0, i};
		}
		Arrays.sort(es, new Comparator<int[]>() {
			public int compare(int[] a, int[] b) {
				if(a[0] != b[0])return -(a[0] - b[0]);
				return a[2] - b[2];
			}
		});
		int miny = Integer.MAX_VALUE;
		int miny2 = Integer.MAX_VALUE;
		boolean[] ban = new boolean[n];
		for(int i = 0;i < 2*n;i++){
			int[] e = es[i];
			if(e[2] == 0){
				if(e[1] < miny){
					miny2 = miny;
					miny = e[1];
				}else if(e[1] < miny2){
					miny2 = e[1];
				}
			}else{
				int ng = e[0];
				int y = miny == ng ? miny2 : miny;
//				tr(e, y);
				if(y <= e[1]){
					ban[e[3]] = true;
				}
			}
		}
		int amin = Integer.MAX_VALUE;
		for(int i = 0;i < n;i++){
			if(ban[i]){
				amin = Math.min(amin, rs[i][0]);
			}
		}
		for(int i = 0;i < n;i++){
			if(rs[i][1] - rs[i][2] >= amin){
				ban[i] = true;
			}
		}
		
		es = new int[2*n][];
		int p = 0;
		for(int i = 0;i < n;i++){
			if(ban[i])continue;
			es[p++] = new int[]{rs[i][0], 0, i, rs[i][1] - rs[i][2]};
			es[p++] = new int[]{rs[i][1] - rs[i][2], 1, i, rs[i][0]};
		}
		es = Arrays.copyOf( es, p);
		
		int[] xs = new int[2*p];
		for(int i = 0;i < p;i++){
			xs[i] = es[i][0]+1000000000;
			xs[i+p] = es[i][3]+1000000000;
		}
		shrinkX(xs);
		for(int i = 0;i < p;i++){
			es[i][0] = xs[i];
			es[i][3] = xs[i+p];
		}
		
		Arrays.sort(es, new Comparator<int[]>() {
			public int compare(int[] a, int[] b) {
				if(a[0] != b[0])return a[0] - b[0];
				if(a[2] != b[2])return a[2] - b[2];
				return a[1] - b[1];
			}
		});
		
		long[] anss = new long[n];
		SegmentTreeRMQL st = new SegmentTreeRMQL(2*p);
		for(int[] e : es){
			if(e[1] == 1){
				if(rs[e[2]][0] > rs[e[2]][1] - rs[e[2]][2]){
					anss[e[2]] = rs[e[2]][1] - Math.min(0, st.minx(0, e[0]+1));
					st.update(e[3], -anss[e[2]]);
				}
			}else{
				if(rs[e[2]][0] <= rs[e[2]][1] - rs[e[2]][2]){
					anss[e[2]] = rs[e[2]][1] - Math.min(0, st.minx(0, e[3]+1));
					st.update(e[0], -anss[e[2]]);
				}
			}
		}
		
		String[] rets = new String[n];
		for(int i = 0;i < n;i++){
			if(ban[i]){
				rets[rs[i][3]] = "BAN";
			}else{
				rets[rs[i][3]] = "" + anss[i];
			}
		}
		for(String ret : rets){
			out.println(ret);
		}
	}
	
	public static int[] shrinkX(int[] a) {
		int n = a.length;
		if(n == 0)return new int[0];
		long[] b = new long[n];
		for (int i = 0; i < n; i++)
			b[i] = (long) a[i] << 32 | i;
		Arrays.sort(b);
		int[] ret = new int[n];
		int p = 0;
		ret[0] = (int) (b[0] >> 32);
		for (int i = 0; i < n; i++) {
			if (i > 0 && (b[i] ^ b[i - 1]) >> 32 != 0) {
				p++;
				ret[p] = (int) (b[i] >> 32);
			}
			a[(int) b[i]] = p;
		}
		return Arrays.copyOf(ret, p + 1);
	}
	
	public static class SegmentTreeRMQL {
		public int M, H, N;
		public long[] st;
		
		public SegmentTreeRMQL(int n)
		{
			N = n;
			M = Integer.highestOneBit(Math.max(N-1, 1))<<2;
			H = M>>>1;
			st = new long[M];
			Arrays.fill(st, 0, M, Long.MAX_VALUE);
		}
		
		public SegmentTreeRMQL(long[] a)
		{
			N = a.length;
			M = Integer.highestOneBit(Math.max(N-1, 1))<<2;
			H = M>>>1;
			st = new long[M];
			for(int i = 0;i < N;i++){
				st[H+i] = a[i];
			}
			Arrays.fill(st, H+N, M, Long.MAX_VALUE);
			for(int i = H-1;i >= 1;i--)propagate(i);
		}
		
		public void update(int pos, long x)
		{
			st[H+pos] = Math.min(x, st[H+pos]);
			for(int i = (H+pos)>>>1;i >= 1;i >>>= 1)propagate(i);
		}
		
		private void propagate(int i)
		{
			st[i] = Math.min(st[2*i], st[2*i+1]);
		}
		
		public long minx(int l, int r){
			long min = Long.MAX_VALUE;
			if(l >= r)return min;
			while(l != 0){
				int f = l&-l;
				if(l+f > r)break;
				long v = st[(H+l)/f];
				if(v < min)min = v;
				l += f;
			}
			
			while(l < r){
				int f = r&-r;
				long v = st[(H+r)/f-1];
				if(v < min)min = v;
				r -= f;
			}
			return min;
		}
		
		public long min(int l, int r){ return l >= r ? 0 : min(l, r, 0, H, 1);}
		
		private long min(int l, int r, int cl, int cr, int cur)
		{
			if(l <= cl && cr <= r){
				return st[cur];
			}else{
				int mid = cl+cr>>>1;
				long ret = Long.MAX_VALUE;
				if(cl < r && l < mid){
					ret = Math.min(ret, min(l, r, cl, mid, 2*cur));
				}
				if(mid < r && l < cr){
					ret = Math.min(ret, min(l, r, mid, cr, 2*cur+1));
				}
				return ret;
			}
		}
		
		public int firstle(int l, long v) {
			int cur = H+l;
			while(true){
				if(st[cur] <= v){
					if(cur < H){
						cur = 2*cur;
					}else{
						return cur-H;
					}
				}else{
					cur++;
					if((cur&cur-1) == 0)return -1;
					if((cur&1)==0)cur>>>=1;
				}
			}
		}
		
		public int lastle(int l, long v) {
			int cur = H+l;
			while(true){
				if(st[cur] <= v){
					if(cur < H){
						cur = 2*cur+1;
					}else{
						return cur-H;
					}
				}else{
					if((cur&cur-1) == 0)return -1;
					cur--;
					if((cur&1)==1)cur>>>=1;
				}
			}
		}
	}

	
	void run() throws Exception
	{
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		long s = System.currentTimeMillis();
		solve();
		out.flush();
		if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//		Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){
//			@Override
//			public void run() {
//				long s = System.currentTimeMillis();
//				solve();
//				out.flush();
//				if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms");
//			}
//		};
//		t.start();
//		t.join();
	}
	
	public static void main(String[] args) throws Exception { new F().run(); }
	
	private byte[] inbuf = new byte[1024];
	public int lenbuf = 0, ptrbuf = 0;
	
	private int readByte()
	{
		if(lenbuf == -1)throw new InputMismatchException();
		if(ptrbuf >= lenbuf){
			ptrbuf = 0;
			try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
			if(lenbuf <= 0)return -1;
		}
		return inbuf[ptrbuf++];
	}
	
	private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
	private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private double nd() { return Double.parseDouble(ns()); }
	private char nc() { return (char)skip(); }
	
	private String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private char[] ns(int n)
	{
		char[] buf = new char[n];
		int b = skip(), p = 0;
		while(p < n && !(isSpaceChar(b))){
			buf[p++] = (char)b;
			b = readByte();
		}
		return n == p ? buf : Arrays.copyOf(buf, p);
	}
	
	private int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private long[] nal(int n)
	{
		long[] a = new long[n];
		for(int i = 0;i < n;i++)a[i] = nl();
		return a;
	}
	
	private char[][] nm(int n, int m) {
		char[][] map = new char[n][];
		for(int i = 0;i < n;i++)map[i] = ns(m);
		return map;
	}
	
	private int[][] nmi(int n, int m) {
		int[][] map = new int[n][];
		for(int i = 0;i < n;i++)map[i] = na(m);
		return map;
	}
	
	private int ni() { return (int)nl(); }
	
	private long nl()
	{
		long num = 0;
		int b;
		boolean minus = false;
		while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
		if(b == '-'){
			minus = true;
			b = readByte();
		}
		
		while(true){
			if(b >= '0' && b <= '9'){
				num = num * 10 + (b - '0');
			}else{
				return minus ? -num : num;
			}
			b = readByte();
		}
	}
	
	private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
0