結果

問題 No.878 Range High-Element Query
ユーザー tomatoma
提出日時 2019-09-06 22:59:21
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 367 ms / 2,000 ms
コード長 2,212 bytes
コンパイル時間 2,057 ms
コンパイル使用メモリ 189,012 KB
実行使用メモリ 12,400 KB
最終ジャッジ日時 2023-09-07 02:05:36
合計ジャッジ時間 5,732 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 3 ms
4,376 KB
testcase_02 AC 3 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 4 ms
4,376 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 4 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 3 ms
4,380 KB
testcase_11 AC 349 ms
12,368 KB
testcase_12 AC 210 ms
8,812 KB
testcase_13 AC 280 ms
10,132 KB
testcase_14 AC 205 ms
8,720 KB
testcase_15 AC 220 ms
9,180 KB
testcase_16 AC 335 ms
11,712 KB
testcase_17 AC 360 ms
12,236 KB
testcase_18 AC 367 ms
12,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include"bits/stdc++.h"
using namespace std;
#define REP(k,m,n) for(int (k)=(m);(k)<(n);(k)++)
#define rep(i,n) REP((i),0,(n))
using ll = long long;
using P = pair<int, int>;

// ttp://beet-aizu.hatenablog.com/entry/2019/03/12/171221
template<typename T>
class SegmentTree {
private:
	using F = function<T(T, T)>; // モノイド型
	int n; // 横幅
	F f;   // モノイド
	T e;   // モノイド単位元
	vector<T> data;

public:
	// init忘れに注意
	SegmentTree() {}
	SegmentTree(F f, T e) :f(f), e(e) {}
	void init(int n_) {
		n = 1;
		while (n < n_)n <<= 1;
		data.assign(n << 1, e);
	}
	void build(const vector<T>& v) {
		int n_ = v.size();
		init(n_);
		rep(i, n_)data[n + i] = v[i];
		for (int i = n - 1; i >= 0; i--) {
			data[i] = f(data[(i << 1) | 0], data[(i << 1) | 1]);
		}
	}
	void set_val(int idx, T val) {
		idx += n;
		data[idx] = val;
		while (idx >>= 1) {
			data[idx] = f(data[(idx << 1) | 0], data[(idx << 1) | 1]);
		}
	}
	T query(int a, int b) {
		// [a,b)
		T vl = e, vr = e;
		for (int l = a + n, r = b + n; l < r; l >>= 1, r >>= 1) {
			if (l & 1)vl = f(vl, data[l++]); // unknown
			if (r & 1)vr = f(data[--r], vr); // unknown
		}
		return f(vl, vr);
	}
};

template<typename T>
using ST = SegmentTree<T>;


int main()
{
	// 入力処理
	int N, Q;
	cin >> N >> Q;
	vector<int> a(N);
	rep(i, N)cin >> a[i];

	vector<vector<int>> querys(Q);
	rep(q, Q) {
		int com, l, r;
		cin >> com >> l >> r;
		l--; r--;
		querys[q] = { l,r,q };
	}
	sort(querys.begin(), querys.end());

	// セグ木用意
	function<int(int, int)> f = [](int a, int b) {
		return a + b;
	};
	SegmentTree<int> st(f, 0);
	st.init(N);

	// クエリ処理
	map<int, int> mp;
	priority_queue<P, vector<P>, greater<P>> pq;
	for (int i = N - 1; i >= 0; i--) {
		// 準備
		while (!pq.empty() && pq.top().first <= a[i]) {
			int idx = pq.top().second;
			st.set_val(idx, 0);
			pq.pop();
		}
		st.set_val(i, 1);

		// クエリ処理
		while (!querys.empty() && querys.back()[0] == i) {
			auto now = querys.back(); querys.pop_back();
			int l = now[0];
			int r = now[1];
			int q = now[2];
			mp[q] = st.query(l, r + 1);
		}

		pq.push({ a[i],i });
	}
	for (auto itr : mp)cout << itr.second << endl;
	return 0;
}
0