結果

問題 No.878 Range High-Element Query
ユーザー startcppstartcpp
提出日時 2019-09-06 22:14:37
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 670 ms / 2,000 ms
コード長 2,287 bytes
コンパイル時間 1,895 ms
コンパイル使用メモリ 83,520 KB
実行使用メモリ 15,680 KB
最終ジャッジ日時 2023-09-07 00:41:17
合計ジャッジ時間 6,723 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 5 ms
13,516 KB
testcase_01 AC 7 ms
13,656 KB
testcase_02 AC 8 ms
13,644 KB
testcase_03 AC 7 ms
13,644 KB
testcase_04 AC 7 ms
13,604 KB
testcase_05 AC 8 ms
13,588 KB
testcase_06 AC 6 ms
13,596 KB
testcase_07 AC 5 ms
13,588 KB
testcase_08 AC 8 ms
13,516 KB
testcase_09 AC 8 ms
13,500 KB
testcase_10 AC 6 ms
13,640 KB
testcase_11 AC 564 ms
15,420 KB
testcase_12 AC 502 ms
15,276 KB
testcase_13 AC 444 ms
14,220 KB
testcase_14 AC 347 ms
14,412 KB
testcase_15 AC 482 ms
15,236 KB
testcase_16 AC 635 ms
15,316 KB
testcase_17 AC 667 ms
15,444 KB
testcase_18 AC 670 ms
15,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//や る だ け(笑)
//えー、「a[i]はlがb[i]以上なら高い項」みたいな条件になるので、lでソートして高い項の登録(0->1)をしていきます。
//b[i]はa[i]が大きい順に求めると、セグ木+2分探索で殴れます。logNでもできますが、びーとさん優しいからlog^2Nでも通してくれる。
//すると、あとは、ただの区間和ですぅ…かね。
#include <iostream>
#include <vector>
#include <tuple>
#include <queue>
#include <algorithm>
#include <functional>
#define int long long
#define rep(i, n) for(i = 0; i < n; i++)
using namespace std;

const int DEPTH = 17;
struct SegTree {
	int d[1 << (DEPTH + 1)];
	
	SegTree() {
		int i;
		rep(i, 1 << (DEPTH + 1)) { d[i] = 0; }
	}
	void add(int pos, int x) {
		pos += (1 << DEPTH) - 1;
		d[pos] += x;
		while (pos > 0) {
			pos = (pos - 1) / 2;
			d[pos] = d[pos * 2 + 1] + d[pos * 2 + 2];
		}
	}
	int sum(int l, int r, int a = 0, int b = (1 << DEPTH), int id = 0) {
		if (a >= r || b <= l) return 0;
		if (l <= a && b <= r) return d[id];
		int x = sum(l, r, a, (a + b) / 2, id * 2 + 1);
		int y = sum(l, r, (a + b) / 2, b, id * 2 + 2);
		return x + y;
	}
};

typedef pair<int, int> P;
typedef tuple<int, int, int> T;
int n, q;
int a[100000];
T query[100000];
int ans[100000];

P p_a[100000];
SegTree seg;
int b[100000];
priority_queue<P, vector<P>, greater<P>> que;
SegTree takaiSeg;

signed main() {
	int i;
	
	cin >> n >> q;
	rep(i, n) cin >> a[i];
	rep(i, q) {
		int t, l, r;
		cin >> t >> l >> r;
		l--; r--;
		query[i] = T(l, r, i);
	}
	sort(query, query + q);
	
	rep(i, n) p_a[i] = P(a[i], i);
	sort(p_a, p_a + n, greater<P>());
	
	rep(i, n) {
		int id = p_a[i].second;
		seg.add(id, 1);
		
		int st = -1, ed = id, mid; //xxxooo, (st, ed]
		while (ed - st >= 2) {
			mid = (st + ed) / 2;
			int wa = seg.sum(mid, id);
			if (wa > 0) st = mid;
			else ed = mid;
		}
		b[id] = ed;
		que.push(P(b[id], id));
	}
	
	rep(i, q) {
		int l = get<0>(query[i]);
		int r = get<1>(query[i]);
		int id = get<2>(query[i]);
		
		while (!que.empty()) {
			P now = que.top();
			if (now.first > l) break;
			takaiSeg.add(now.second, 1);
			que.pop();
		}
		int beet = takaiSeg.sum(l, r + 1);
		ans[id] = beet;
	}
	
	rep(i, q) cout << ans[i] << endl;
	return 0;
}
0