結果

問題 No.404 部分門松列
ユーザー 37zigen37zigen
提出日時 2016-08-07 21:12:04
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 6,045 bytes
コンパイル時間 3,484 ms
コンパイル使用メモリ 82,292 KB
実行使用メモリ 65,388 KB
最終ジャッジ日時 2024-04-24 23:34:04
合計ジャッジ時間 30,304 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 46 ms
37,128 KB
testcase_01 AC 45 ms
37,192 KB
testcase_02 AC 45 ms
36,916 KB
testcase_03 AC 45 ms
36,776 KB
testcase_04 AC 55 ms
37,108 KB
testcase_05 AC 56 ms
37,064 KB
testcase_06 AC 56 ms
36,832 KB
testcase_07 AC 56 ms
37,248 KB
testcase_08 AC 53 ms
36,908 KB
testcase_09 AC 54 ms
37,048 KB
testcase_10 AC 53 ms
36,756 KB
testcase_11 AC 52 ms
37,036 KB
testcase_12 AC 56 ms
36,956 KB
testcase_13 AC 937 ms
58,820 KB
testcase_14 AC 1,155 ms
62,828 KB
testcase_15 AC 1,084 ms
65,352 KB
testcase_16 AC 856 ms
61,288 KB
testcase_17 AC 1,183 ms
63,644 KB
testcase_18 AC 865 ms
55,000 KB
testcase_19 AC 641 ms
57,684 KB
testcase_20 AC 912 ms
49,712 KB
testcase_21 AC 1,148 ms
64,280 KB
testcase_22 AC 1,037 ms
57,928 KB
testcase_23 WA -
testcase_24 AC 1,047 ms
53,140 KB
testcase_25 AC 1,325 ms
65,388 KB
testcase_26 AC 1,421 ms
64,096 KB
testcase_27 AC 626 ms
52,216 KB
testcase_28 AC 1,318 ms
60,876 KB
testcase_29 AC 1,234 ms
64,052 KB
testcase_30 AC 1,254 ms
58,120 KB
testcase_31 AC 472 ms
50,620 KB
testcase_32 AC 1,073 ms
62,552 KB
testcase_33 AC 1,223 ms
59,528 KB
testcase_34 AC 987 ms
52,884 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 = lower_bound(b, L[i]) + 1;
			int high = upper_bound(b, H[i]) + 1;

			if (L[i] > b[n - 1]) {
				low = n + 1;
			}
			if (H[i] >= b[n - 1]) {
				high = n + 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; Math.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; Math.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));
	}
}
0