結果

問題 No.1079 まお
ユーザー square1001square1001
提出日時 2020-06-12 21:51:14
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 2,729 bytes
コンパイル時間 1,083 ms
コンパイル使用メモリ 93,680 KB
実行使用メモリ 36,268 KB
最終ジャッジ日時 2023-09-06 10:15:02
合計ジャッジ時間 5,048 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 3 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 74 ms
9,320 KB
testcase_13 AC 66 ms
10,496 KB
testcase_14 AC 114 ms
12,008 KB
testcase_15 AC 101 ms
13,140 KB
testcase_16 AC 96 ms
14,072 KB
testcase_17 AC 160 ms
12,104 KB
testcase_18 AC 100 ms
12,036 KB
testcase_19 AC 209 ms
12,144 KB
testcase_20 AC 171 ms
11,960 KB
testcase_21 AC 150 ms
12,084 KB
testcase_22 AC 150 ms
15,708 KB
testcase_23 AC 157 ms
16,008 KB
testcase_24 AC 156 ms
15,612 KB
testcase_25 AC 172 ms
15,724 KB
testcase_26 AC 186 ms
15,796 KB
testcase_27 AC 44 ms
6,868 KB
testcase_28 AC 86 ms
16,024 KB
testcase_29 AC 96 ms
36,268 KB
testcase_30 AC 101 ms
36,012 KB
testcase_31 AC 59 ms
6,424 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <algorithm>
#include <functional>
using namespace std;
int main() {
	int N, K;
	cin >> N >> K;
	vector<int> A(N);
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
	}
	int sz = 1;
	while (sz < N) sz *= 2;
	vector<int> seg(2 * sz);
	for (int i = 0; i < N; ++i) {
		seg[i + sz] = A[i];
	}
	for (int i = sz - 1; i >= 1; --i) {
		seg[i] = min(seg[i * 2], seg[i * 2 + 1]);
	}
	function<int(int, int)> rangemin = [&](int l, int r) {
		l += sz; r += sz;
		int ans = 1 << 30;
		while (l != r) {
			if (l & 1) ans = min(ans, seg[l++]);
			if (r & 1) ans = min(ans, seg[--r]);
			l >>= 1; r >>= 1;
		}
		return ans;
	};
	vector<int> comp = A;
	sort(comp.begin(), comp.end());
	comp.erase(unique(comp.begin(), comp.end()), comp.end());
	int S = comp.size();
	vector<vector<int> > g(S);
	for (int i = 0; i < N; ++i) {
		g[lower_bound(comp.begin(), comp.end(), A[i]) - comp.begin()].push_back(i);
	}
	vector<vector<long long> > g2(S);
	for (int i = 0; i < S; ++i) {
		g2[i].resize(g[i].size() + 1);
		for (int j = 0; j < g[i].size(); ++j) {
			g2[i][j + 1] = g2[i][j] + g[i][j];
		}
	}
	function<pair<int, long long>(int, int, int)> rangecount = [&](int l, int r, int x) {
		if (!binary_search(comp.begin(), comp.end(), x)) return make_pair(0, 0LL);
		int ptr = lower_bound(comp.begin(), comp.end(), x) - comp.begin();
		int pl = lower_bound(g[ptr].begin(), g[ptr].end(), l) - g[ptr].begin();
		int pr = lower_bound(g[ptr].begin(), g[ptr].end(), r) - g[ptr].begin();
		return make_pair(pr - pl, g2[ptr][pr] - g2[ptr][pl]);
	};
	function<long long(int, int)> calc = [&](int l, int r) {
		if (r - l <= 0) return 0LL;
		int mn = rangemin(l, r);
		int ptr = lower_bound(comp.begin(), comp.end(), mn) - comp.begin();
		int pl = lower_bound(g[ptr].begin(), g[ptr].end(), l) - g[ptr].begin();
		int pr = lower_bound(g[ptr].begin(), g[ptr].end(), r) - g[ptr].begin();
		vector<int> seq(g[ptr].begin() + pl, g[ptr].begin() + pr);
		seq.insert(seq.begin(), l - 1);
		seq.push_back(r);
		long long ans = 0;
		for (int i = 2; i < seq.size(); ++i) {
			int ls = seq[i - 1] - seq[i - 2];
			int rs = seq[i] - seq[i - 1];
			if (ls < rs) {
				for (int j = seq[i - 2] + 1; j < seq[i - 1] + 1; ++j) {
					pair<int, long long> res = rangecount(seq[i - 1], seq[i], K - A[j]);
					ans += res.second - 1LL * (j - 1) * res.first;
				}
			}
			else {
				for (int j = seq[i - 1]; j < seq[i]; ++j) {
					pair<int, long long> res = rangecount(seq[i - 2] + 1, seq[i - 1] + 1, K - A[j]);
					ans += 1LL * (j + 1) * res.first - res.second;
				}
			}
		}
		for (int i = 1; i < seq.size(); ++i) {
			ans += calc(seq[i - 1] + 1, seq[i]);
		}
		return ans;
	};
	long long res = calc(0, N);
	cout << res << endl;
	return 0;
}
0