Submission #1178781


Source Code Expand

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

public class Main {
	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";
	static int[] from = new int[4000000];
	static int[] to = new int[4000000];
	static int gp = 0;
	static int id;
	
	static void solve()
	{
		gp = 0;
		int n = ni();
		id = n;
		int[] L = na(n);
		int[] R = na(n);
		dfs(0, n, L, R);
		int[][] g = packD(id, from, to, gp);
		int[] clus = decomposeToSCC(g);
		int[][] cg = condense(g, clus);
		int m = cg.length;
		int[] score = new int[m];
		for(int i = 0;i < n;i++){
			score[clus[i]]++;
		}
		int[] dp = new int[m];
		for(int i = m-1;i >= 0;i--){
			dp[i] += score[i];
			for(int e : cg[i]){
				dp[e] = Math.max(dp[e], dp[i]);
			}
		}
		out.println(Arrays.stream(dp).max().getAsInt());
	}
	
	
	
	static void dfs(int l, int r, int[] B, int[] A)
	{
		int h = (l+r)/2;
		
		long[] poss = new long[2*(r-l)];
		int p = 0;
		int O = 50000;
		for(int i = l;i < r;i++){
			poss[p++] = (long)A[i]-Math.abs(i-h)+O<<32|1<<20|i;
			poss[p++] = (long)B[i]+Math.abs(i-h)+O<<32|0<<20|i;
		}
		Arrays.sort(poss);
		int bid = id;
		id++;
		for(int i = p-2;i >= 0;i--){
			from[gp] = id-1;
			to[gp] = id;
			gp++;
			id++;
		}
		for(int i = p-1;i >= 0;i--){
			if((poss[i]>>>20&1) == 1){
				from[gp] = (int)(poss[i]&(1L<<20)-1);
				to[gp] = bid + (p-1-i);
				gp++;
			}else{
				from[gp] = bid + (p-1-i);
				to[gp] = (int)(poss[i]&(1L<<20)-1);
				gp++;
			}
		}
		if(r-l > 1){
			dfs(l, h, B, A);
			dfs(h, r, B, A);
		}
	}
	
	public static int[][] packD(int n, int[] from, int[] to){ return packD(n, from, to, from.length);}
	public static int[][] packD(int n, int[] from, int[] to, int sup)
	{
		int[][] g = new int[n][];
		int[] p = new int[n];
		for(int i = 0;i < sup;i++)p[from[i]]++;
		for(int i = 0;i < n;i++)g[i] = new int[p[i]];
		for(int i = 0;i < sup;i++){
			g[from[i]][--p[from[i]]] = to[i];
		}
		return g;
	}
	
	public static int[] decomposeToSCC(int[][] g)
	{
		int n = g.length;
		int[] stack = new int[n+1];
		int[] ind = new int[n+1];
		int[] ord = new int[n];
		Arrays.fill(ord, -1);
		int[] low = new int[n];
		Arrays.fill(low, -1);
		int sp = 0;
		int id = 0; // preorder
		int[] clus = new int[n];
		int cid = 0;
		int[] cstack = new int[n+1];
		int csp = 0;
		boolean[] incstack = new boolean[n];
		for(int i = 0;i < n;i++){
			if(ord[i] == -1){
				ind[sp] = 0;
				cstack[csp++] = i;
				stack[sp++] = i;
				incstack[i] = true;
				while(sp > 0){
					int cur = stack[sp-1];
					if(ind[sp-1] == 0){
						ord[cur] = low[cur] = id++;
					}
					if(ind[sp-1] < g[cur].length){
						int nex = g[cur][ind[sp-1]];
						if(ord[nex] == -1){
							ind[sp-1]++;
							ind[sp] = 0;
							incstack[nex] = true;
							cstack[csp++] = nex;
							stack[sp++] = nex;
						}else{
							// shortcut
//							U.tr(cur, nex, incstack[nex], low[nex], stack);
							if(incstack[nex])low[cur] = Math.min(low[cur], low[nex]);
							ind[sp-1]++;
						}
					}else{
						if(ord[cur] == low[cur]){
							while(csp > 0){
								incstack[cstack[csp-1]] = false;
								clus[cstack[--csp]] = cid;
								if(cstack[csp] == cur)break;
							}
							cid++;
						}
						if(--sp >= 1)low[stack[sp-1]] = Math.min(low[stack[sp-1]], low[stack[sp]]);
					}
				}
			}
		}
		return clus;
	}
	
	public static int[][] condense(int[][] g, int[] clus)
	{
		int n = g.length;
		int m = 0;
		for(int i = 0;i < n;i++)m = Math.max(m, clus[i]);
		m++;
		
		int[] cp = new int[m];
		for(int i = 0;i < n;i++){
			cp[clus[i]] += g[i].length;
		}
		int[][] c = new int[m][];
		for(int i = 0;i < m;i++){
			c[i] = new int[cp[i]];
		}
		
		for(int i = 0;i < n;i++){
			for(int j = 0;j < g[i].length;j++){
				c[clus[i]][--cp[clus[i]]] = clus[g[i][j]];
			}
		}
		
		for(int i = 0;i < m;i++){
			Arrays.sort(c[i]);
			int jp = 0;
			for(int j = 0;j < c[i].length;j++){
				if((j == 0 || c[i][j] != c[i][j-1]) && c[i][j] != i){
					c[i][jp++] = c[i][j];
				}
			}
			c[i] = Arrays.copyOf(c[i], jp);
		}
		return c;
	}

	
	public static void main(String[] args) throws Exception
	{
		long S = System.currentTimeMillis();
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);
		
		solve();
		out.flush();
		long G = System.currentTimeMillis();
		tr(G-S+"ms");
	}
	
	private static boolean eof()
	{
		if(lenbuf == -1)return true;
		int lptr = ptrbuf;
		while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
		
		try {
			is.mark(1000);
			while(true){
				int b = is.read();
				if(b == -1){
					is.reset();
					return true;
				}else if(!isSpaceChar(b)){
					is.reset();
					return false;
				}
			}
		} catch (IOException e) {
			return true;
		}
	}
	
	private static byte[] inbuf = new byte[1024];
	static int lenbuf = 0, ptrbuf = 0;
	
	private static 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 static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
//	private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); }
	private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
	
	private static double nd() { return Double.parseDouble(ns()); }
	private static char nc() { return (char)skip(); }
	
	private static String ns()
	{
		int b = skip();
		StringBuilder sb = new StringBuilder();
		while(!(isSpaceChar(b))){
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	
	private static 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 static 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 static int[] na(int n)
	{
		int[] a = new int[n];
		for(int i = 0;i < n;i++)a[i] = ni();
		return a;
	}
	
	private static int ni()
	{
		int num = 0, 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 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) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}

Submission Info

Submission Time
Task E - 遊園地
User uwi
Language Java8 (OpenJDK 1.8.0)
Score 1200
Code Size 7422 Byte
Status AC
Exec Time 1775 ms
Memory 298908 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1200 / 1200
Status
AC × 3
AC × 36
Set Name Test Cases
Sample sample1.txt, sample2.txt, sample3.txt
All in1.txt, in10.txt, in11.txt, in12.txt, in13.txt, in14.txt, in15.txt, in16.txt, in17.txt, in18.txt, in19.txt, in2.txt, in20.txt, in21.txt, in22.txt, in23.txt, in24.txt, in25.txt, in26.txt, in27.txt, in28.txt, in29.txt, in3.txt, in30.txt, in31.txt, in32.txt, in33.txt, in4.txt, in5.txt, in6.txt, in7.txt, in8.txt, in9.txt, sample1.txt, sample2.txt, sample3.txt
Case Name Status Exec Time Memory
in1.txt AC 1168 ms 185780 KB
in10.txt AC 1202 ms 188344 KB
in11.txt AC 1572 ms 241028 KB
in12.txt AC 1524 ms 246200 KB
in13.txt AC 1602 ms 248132 KB
in14.txt AC 1579 ms 245040 KB
in15.txt AC 1559 ms 242544 KB
in16.txt AC 1279 ms 220252 KB
in17.txt AC 1238 ms 221108 KB
in18.txt AC 1220 ms 190444 KB
in19.txt AC 1222 ms 189488 KB
in2.txt AC 1235 ms 188168 KB
in20.txt AC 1262 ms 189720 KB
in21.txt AC 1768 ms 296432 KB
in22.txt AC 1648 ms 298908 KB
in23.txt AC 1668 ms 292248 KB
in24.txt AC 1677 ms 296696 KB
in25.txt AC 1775 ms 293880 KB
in26.txt AC 159 ms 59220 KB
in27.txt AC 170 ms 57428 KB
in28.txt AC 168 ms 59348 KB
in29.txt AC 1199 ms 189104 KB
in3.txt AC 1192 ms 182680 KB
in30.txt AC 1238 ms 187304 KB
in31.txt AC 1306 ms 185988 KB
in32.txt AC 1223 ms 191224 KB
in33.txt AC 1257 ms 187900 KB
in4.txt AC 1257 ms 187936 KB
in5.txt AC 1206 ms 187916 KB
in6.txt AC 1226 ms 186972 KB
in7.txt AC 1219 ms 188544 KB
in8.txt AC 1435 ms 193056 KB
in9.txt AC 1183 ms 183860 KB
sample1.txt AC 163 ms 59092 KB
sample2.txt AC 158 ms 58836 KB
sample3.txt AC 164 ms 55116 KB