結果

問題 No.404 部分門松列
ユーザー 37zigen37zigen
提出日時 2016-08-07 21:17:47
言語 Java21
(openjdk 21)
結果
AC  
実行時間 1,617 ms / 2,000 ms
コード長 6,091 bytes
コンパイル時間 5,507 ms
コンパイル使用メモリ 81,928 KB
実行使用メモリ 66,508 KB
最終ジャッジ日時 2024-04-24 23:49:11
合計ジャッジ時間 37,529 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 53 ms
36,796 KB
testcase_01 AC 54 ms
36,800 KB
testcase_02 AC 54 ms
36,964 KB
testcase_03 AC 53 ms
36,892 KB
testcase_04 AC 63 ms
37,100 KB
testcase_05 AC 67 ms
36,948 KB
testcase_06 AC 66 ms
36,828 KB
testcase_07 AC 62 ms
36,832 KB
testcase_08 AC 68 ms
37,316 KB
testcase_09 AC 63 ms
36,836 KB
testcase_10 AC 65 ms
36,836 KB
testcase_11 AC 78 ms
37,332 KB
testcase_12 AC 67 ms
37,308 KB
testcase_13 AC 1,174 ms
59,440 KB
testcase_14 AC 1,414 ms
62,968 KB
testcase_15 AC 1,231 ms
63,380 KB
testcase_16 AC 1,016 ms
61,032 KB
testcase_17 AC 1,352 ms
63,532 KB
testcase_18 AC 1,069 ms
55,260 KB
testcase_19 AC 747 ms
57,708 KB
testcase_20 AC 928 ms
49,716 KB
testcase_21 AC 1,373 ms
64,052 KB
testcase_22 AC 1,210 ms
57,944 KB
testcase_23 AC 1,573 ms
66,508 KB
testcase_24 AC 1,203 ms
53,160 KB
testcase_25 AC 1,560 ms
65,668 KB
testcase_26 AC 1,617 ms
63,756 KB
testcase_27 AC 695 ms
51,884 KB
testcase_28 AC 1,331 ms
59,832 KB
testcase_29 AC 1,418 ms
63,916 KB
testcase_30 AC 1,111 ms
53,156 KB
testcase_31 AC 573 ms
52,564 KB
testcase_32 AC 1,253 ms
62,304 KB
testcase_33 AC 1,507 ms
59,768 KB
testcase_34 AC 1,194 ms
53,864 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;
			}
			if(H[i]<b[0]){
				high=0;
				low=0;
			}
			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