結果

問題 No.404 部分門松列
ユーザー 37zigen37zigen
提出日時 2016-08-07 21:24:44
言語 Java19
(openjdk 21)
結果
AC  
実行時間 1,756 ms / 2,000 ms
コード長 6,315 bytes
コンパイル時間 4,217 ms
コンパイル使用メモリ 79,696 KB
実行使用メモリ 84,744 KB
最終ジャッジ日時 2023-09-11 02:35:23
合計ジャッジ時間 37,120 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
49,204 KB
testcase_01 AC 46 ms
49,208 KB
testcase_02 AC 46 ms
49,328 KB
testcase_03 AC 45 ms
49,280 KB
testcase_04 AC 61 ms
49,536 KB
testcase_05 AC 60 ms
49,920 KB
testcase_06 AC 60 ms
50,484 KB
testcase_07 AC 62 ms
48,012 KB
testcase_08 AC 61 ms
49,476 KB
testcase_09 AC 61 ms
49,828 KB
testcase_10 AC 61 ms
49,804 KB
testcase_11 AC 60 ms
49,660 KB
testcase_12 AC 60 ms
49,748 KB
testcase_13 AC 1,299 ms
81,936 KB
testcase_14 AC 1,392 ms
72,756 KB
testcase_15 AC 1,287 ms
76,596 KB
testcase_16 AC 909 ms
72,068 KB
testcase_17 AC 1,371 ms
73,996 KB
testcase_18 AC 959 ms
67,680 KB
testcase_19 AC 716 ms
72,260 KB
testcase_20 AC 872 ms
61,996 KB
testcase_21 AC 1,243 ms
72,236 KB
testcase_22 AC 1,101 ms
67,624 KB
testcase_23 AC 1,529 ms
75,304 KB
testcase_24 AC 1,132 ms
64,352 KB
testcase_25 AC 1,566 ms
74,300 KB
testcase_26 AC 1,572 ms
74,260 KB
testcase_27 AC 729 ms
64,312 KB
testcase_28 AC 1,738 ms
84,744 KB
testcase_29 AC 1,345 ms
72,328 KB
testcase_30 AC 1,380 ms
75,772 KB
testcase_31 AC 473 ms
58,356 KB
testcase_32 AC 1,369 ms
72,492 KB
testcase_33 AC 1,756 ms
82,632 KB
testcase_34 AC 1,076 ms
64,572 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

package No400番台;

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

public class Q404 {

	static InputStream is;
	static PrintWriter out;
	static String INPUT = "";

	void solver() {
		int N = ni();
		int[] a = na(N);
		int[] b = Arrays.copyOf(a, a.length);
		Arrays.sort(b);
		b = shrink(b);
		int n = b.length;
		BinaryIndexedTree[] B = new BinaryIndexedTree[2];
		B[0] = new BinaryIndexedTree(n);
		B[1] = new BinaryIndexedTree(n);

		for (int i = 0; i < N; i++) {
			a[i] = upper_bound(b, a[i]) + 1;
		}
		long[] r = new long[n + 1];
		long[] l = new long[n + 1];
		for (int i = 0; i < N; i++) {
			r[a[i]]++;
			B[1].add(a[i], 1);
		}
		r[a[0]]--;
		long same = 0;
		long[] calc = new long[n + 1];
		for (int i = 0; i < N; i++) {
			long ans = 0;
			B[1].add(a[i], -1);

			ans += B[0].sum(a[i] - 1) * B[1].sum(a[i] - 1);
			ans += (i - B[0].sum(a[i])) * (N - i - 1 - B[1].sum(a[i]));
			ans -= same - l[a[i]] * r[a[i]];

			if (i + 1 < N) {
				r[a[i + 1]]--;
				same += r[a[i]];
				same -= l[a[i + 1]];
				l[a[i]]++;
			}
			B[0].add(a[i], 1);
			calc[a[i]] += ans;
		}

		long[] sum = new long[n + 2];
		for (int i = 1; i <= n; i++) {
			sum[i] = sum[i - 1] + calc[i];
		}
		sum[n + 1] = sum[n];
		int q = ni();
		long[] L = new long[q];
		long[] H = new long[q];
		// tr(sum);
		// tr(b);
		for (int i = 0; i < q; i++) {
			L[i] = ni();
			H[i] = ni();
			int low = -999999999;
			int high = -999999999;

			if (L[i] > b[n - 1]) {
				low = n + 1;
			} else {
				low = lower_bound(b, L[i]) + 1;
			}
			if (H[i] >= b[n - 1]) {
				high = n + 1;
			} else if (H[i] < b[0]) {
				high = 0;
				low = 0;
			} else {
				high = upper_bound(b, H[i]) + 1;
			}
			System.out.println((sum[high] - sum[low]));
			// System.out.println(L[i]+" "+H[i]+" "+lower_bound(b, L[i]) + " " +
			// upper_bound(b, H[i]));
		}
	}

	int upper_bound(int[] b, long h) {
		int min = 0;
		int max = b.length;
		for (int i = 0; pow(2, i) < b.length + 2; i++) {
			int mid = (max + min) / 2;
			if (b[mid] > h) {
				max = mid;
			} else {
				min = mid;
			}
			if (max == min + 1 && b[min] == h)
				return min;
		}
		return min;
	}

	int lower_bound(int[] b, long l) {
		int min = -1;
		int max = b.length - 1;
		for (int i = 0; pow(2, i) < b.length + 2; i++) {
			int mid = (max + min) / 2;
			if (b[mid] >= l) {
				max = mid;
			} else {
				min = mid;
			}
		}
		return min;
	}

	static int[] shrink(int[] arr) {
		ArrayList<Integer> d = new ArrayList<>();
		d.add(arr[0]);
		for (int i = 0; i < arr.length; i++) {
			if (d.get(d.size() - 1) == arr[i])
				continue;
			else {
				d.add(arr[i]);
			}
		}
		int[] ret = new int[d.size()];
		for (int i = 0; i < ret.length; i++) {
			ret[i] = d.get(i);
		}
		return ret;
	}

	class BinaryIndexedTree {
		int n;
		long[] d;

		BinaryIndexedTree(int n) {
			this.n = n;
			d = new long[n + 2];
		}

		void add(int i, long diff) {
			while (i <= n) {
				d[i] += diff;
				i += (i & (-i));
			}
		}

		// sum[1,i]
		long sum(int i) {
			long s = 0;
			while (i > 0) {
				s += d[i];
				i -= (i & (-i));
			}
			return s;
		}

		// sum[r,l]
		long sum(int r, int l) {
			return sum(l) - sum(r - 1);
		}
	}

	public static void main(String[] args) throws Exception {
		is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
		out = new PrintWriter(System.out);

		new Q404().solver();
		out.flush();
	}

	static long nl() {
		try {
			long num = 0;
			boolean minus = false;
			while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'))
				;
			if (num == '-') {
				num = 0;
				minus = true;
			} else {
				num -= '0';
			}

			while (true) {
				int b = is.read();
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	static char nc() {
		try {
			int b = skip();
			if (b == -1)
				return 0;
			return (char) b;
		} catch (IOException e) {
		}
		return 0;
	}

	static double nd() {
		try {
			return Double.parseDouble(ns());
		} catch (Exception e) {
		}
		return 0;
	}

	static String ns() {
		try {
			int b = skip();
			StringBuilder sb = new StringBuilder();
			if (b == -1)
				return "";
			sb.append((char) b);
			while (true) {
				b = is.read();
				if (b == -1)
					return sb.toString();
				if (b <= ' ')
					return sb.toString();
				sb.append((char) b);
			}
		} catch (IOException e) {
		}
		return "";
	}

	public static char[] ns(int n) {
		char[] buf = new char[n];
		try {
			int b = skip(), p = 0;
			if (b == -1)
				return null;
			buf[p++] = (char) b;
			while (p < n) {
				b = is.read();
				if (b == -1 || b <= ' ')
					break;
				buf[p++] = (char) b;
			}
			return Arrays.copyOf(buf, p);
		} catch (IOException e) {
		}
		return null;
	}

	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;
	}

	public static byte[] nse(int n) {
		byte[] buf = new byte[n];
		try {
			int b = skip();
			if (b == -1)
				return null;
			is.read(buf);
			return buf;
		} catch (IOException e) {
		}
		return null;
	}

	static int skip() throws IOException {
		int b;
		while ((b = is.read()) != -1 && !(b >= 33 && b <= 126))
			;
		return b;
	}

	static boolean eof() {
		try {
			is.mark(1000);
			int b = skip();
			is.reset();
			return b == -1;
		} catch (IOException e) {
			return true;
		}
	}

	static int ni() {
		try {
			int num = 0;
			boolean minus = false;
			while ((num = is.read()) != -1 && !((num >= '0' && num <= '9') || num == '-'))
				;
			if (num == '-') {
				num = 0;
				minus = true;
			} else {
				num -= '0';
			}

			while (true) {
				int b = is.read();
				if (b >= '0' && b <= '9') {
					num = num * 10 + (b - '0');
				} else {
					return minus ? -num : num;
				}
			}
		} catch (IOException e) {
		}
		return -1;
	}

	static void tr(Object... o) {
		System.out.println(Arrays.deepToString(o));
	}
	static long pow(long a, long n) {
		long A = a;
		long ans = 1;
		for (;n >= 1;n>>=1,A*=A) {
			if ((n&1) == 1) {
				ans *= A;
			}
		}
		return ans;
	}
}
0