結果

問題 No.728 ギブ and テイク
ユーザー 37zigen37zigen
提出日時 2018-08-25 12:01:15
言語 Java19
(openjdk 21)
結果
AC  
実行時間 2,102 ms / 3,000 ms
コード長 1,771 bytes
コンパイル時間 3,046 ms
コンパイル使用メモリ 76,852 KB
実行使用メモリ 113,012 KB
最終ジャッジ日時 2023-09-07 12:38:17
合計ジャッジ時間 26,653 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 122 ms
55,372 KB
testcase_01 AC 123 ms
55,900 KB
testcase_02 AC 128 ms
56,104 KB
testcase_03 AC 123 ms
55,740 KB
testcase_04 AC 124 ms
53,556 KB
testcase_05 AC 121 ms
55,740 KB
testcase_06 AC 121 ms
55,760 KB
testcase_07 AC 122 ms
53,828 KB
testcase_08 AC 176 ms
56,748 KB
testcase_09 AC 133 ms
55,436 KB
testcase_10 AC 143 ms
55,888 KB
testcase_11 AC 185 ms
55,840 KB
testcase_12 AC 196 ms
57,892 KB
testcase_13 AC 522 ms
61,500 KB
testcase_14 AC 596 ms
60,496 KB
testcase_15 AC 451 ms
60,696 KB
testcase_16 AC 646 ms
61,012 KB
testcase_17 AC 620 ms
63,328 KB
testcase_18 AC 1,533 ms
92,324 KB
testcase_19 AC 1,611 ms
92,344 KB
testcase_20 AC 1,920 ms
106,820 KB
testcase_21 AC 1,743 ms
95,288 KB
testcase_22 AC 1,042 ms
73,392 KB
testcase_23 AC 877 ms
69,652 KB
testcase_24 AC 1,389 ms
86,948 KB
testcase_25 AC 1,352 ms
86,960 KB
testcase_26 AC 1,009 ms
73,968 KB
testcase_27 AC 2,102 ms
113,012 KB
testcase_28 AC 1,687 ms
92,404 KB
testcase_29 AC 123 ms
55,452 KB
testcase_30 AC 123 ms
56,072 KB
testcase_31 AC 121 ms
55,720 KB
testcase_32 AC 122 ms
55,964 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
	public static void main(String[] args) {
		new Main().run();
	}

	class BIT {
		int n;
		long[] v;

		public BIT(int n_) {
			n = n_;
			v = new long[n + 1];
		}

		void add(int k, long val) {
			++k;
			while (k <= n) {
				v[k] += val;
				k += k & -k;
			}
		}

		long sum(int k) {
			++k;
			k = Math.min(k, n);
			long ret = 0;
			while (k > 0) {
				ret += v[k];
				k -= k & -k;
			}
			return ret;
		}

		long sum(long a, long b) {
			return sum((int) b) - sum((int) a - 1);
		}
	}

	void run() {
		Scanner sc = new Scanner(System.in);
		int N = sc.nextInt();
		long[][] A = new long[N][3];
		for (int i = 0; i < N; ++i) {
			A[i][0] = sc.nextLong();
		}
		for (int i = 0; i < N; ++i) {
			A[i][1] = sc.nextLong();
			A[i][2] = sc.nextLong();
		}
		ArrayList<Integer>[] rm = new ArrayList[N];
		for (int i = 0; i < N; ++i)
			rm[i] = new ArrayList<>();
		BIT bit = new BIT(N);
		long ans = 0;
		for (int i = 0; i < N; ++i) {
			{// A[m][0]>=A[i][0]-A[i][1]となる最小のmを求めたい
				int ok = N - 1, ng = -1;
				while (ok - ng > 1) {
					int m = (ok + ng) / 2;
					if (A[m][0] >= A[i][0] - A[i][1]) {
						ok = m;
					} else {
						ng = m;
					}
				}
				ans += bit.sum(ok, i);
			}
			bit.add(i, 1);
			// A[m][0] <= A[i][0] + A[i][2]なる最大のmを求めたい

			{
				int ok = i, ng = N;
				while (ng - ok > 1) {
					int m = (ok + ng) / 2;
					if (A[m][0] <= A[i][0] + A[i][2]) {
						ok = m;
					} else {
						ng = m;
					}
				}
				rm[ok].add(i);
			}
			for (int v : rm[i]) {
				bit.add(v, -1);
			}
		}
		System.out.println(ans);
	}

	void tr(Object... objects) {
		System.out.println(Arrays.deepToString(objects));
	}
}
0