結果

問題 No.878 Range High-Element Query
ユーザー QCFiumQCFium
提出日時 2019-09-06 22:18:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 196 ms / 2,000 ms
コード長 1,332 bytes
コンパイル時間 1,790 ms
コンパイル使用メモリ 173,732 KB
実行使用メモリ 15,932 KB
最終ジャッジ日時 2023-09-07 00:51:33
合計ジャッジ時間 4,708 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,384 KB
testcase_09 AC 3 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 187 ms
14,712 KB
testcase_12 AC 122 ms
15,104 KB
testcase_13 AC 147 ms
10,460 KB
testcase_14 AC 108 ms
9,584 KB
testcase_15 AC 124 ms
14,516 KB
testcase_16 AC 182 ms
15,560 KB
testcase_17 AC 196 ms
15,792 KB
testcase_18 AC 196 ms
15,932 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

int ri() {
	int n;
	scanf("%d", &n);
	return n;
}

struct SegTree {
	std::vector<std::vector<int> > data; // real data
	int n;
	SegTree (const std::vector<int> &a) {
		for (n = 1; n < (int) a.size(); n *= 2);
		data.resize(2 * n);
		for (int i = 0; i < (int) a.size(); i++) data[i + n] = {a[i]};
		for (int i = n - 1; i; i--) {
			for (auto j : data[i << 1]) if (!data[i].size() || j > data[i].back()) data[i].push_back(j);
			for (auto j : data[i << 1 | 1]) if (!data[i].size() || j > data[i].back()) data[i].push_back(j);
		}
	}
	int query(int l, int r) {
		std::vector<int> left, right;
		for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
			if (r & 1) right.push_back(--r);
			if (l & 1) left.push_back(l++);
		}
		std::vector<int> seg = left;
		std::reverse(right.begin(), right.end());
		for (auto i : right) seg.push_back(i);
		int res = 0;
		int max = 0;
		for (auto i : seg) {
			auto itr = std::lower_bound(data[i].begin(), data[i].end(), max);
			res += data[i].end() - itr;
			max = std::max(max, data[i].back());
		}
		return res;
	}
};

int main() {
	int n = ri();
	int q = ri();
	std::vector<int> a(n);
	for (int i = 0; i < n; i++) a[i] = ri();
	SegTree tree(a);
	for (int i = 0; i < q; i++) {
		ri();
		int l = ri() - 1, r = ri() - 1;
		printf("%d\n", tree.query(l, r + 1));
	}
	return 0;
}

0