結果

問題 No.878 Range High-Element Query
ユーザー autumn-eelautumn-eel
提出日時 2019-09-06 22:32:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 242 ms / 2,000 ms
コード長 1,322 bytes
コンパイル時間 1,638 ms
コンパイル使用メモリ 173,896 KB
実行使用メモリ 25,508 KB
最終ジャッジ日時 2023-09-07 01:19:21
合計ジャッジ時間 5,230 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 39 ms
24,704 KB
testcase_01 AC 40 ms
24,724 KB
testcase_02 AC 40 ms
24,804 KB
testcase_03 AC 42 ms
24,740 KB
testcase_04 AC 42 ms
24,960 KB
testcase_05 AC 42 ms
24,944 KB
testcase_06 AC 41 ms
24,776 KB
testcase_07 AC 42 ms
24,772 KB
testcase_08 AC 41 ms
24,776 KB
testcase_09 AC 42 ms
24,780 KB
testcase_10 AC 41 ms
24,780 KB
testcase_11 AC 224 ms
25,372 KB
testcase_12 AC 153 ms
25,368 KB
testcase_13 AC 180 ms
25,164 KB
testcase_14 AC 139 ms
25,084 KB
testcase_15 AC 161 ms
25,324 KB
testcase_16 AC 225 ms
25,508 KB
testcase_17 AC 239 ms
25,488 KB
testcase_18 AC 242 ms
25,468 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

const int MOD=1000000007;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;


int a[200000];
int idx[200000];

const int N=(1<<17);
vector<int>dat[2*N];
void init(int k, int l, int r) {
	if (r - l == 1) {
		dat[k].push_back(idx[l]); return;
	}
	int lb = k * 2 + 1, rb = k * 2 + 2;
	init(lb, l, (l + r) / 2);
	init(rb, (l + r) / 2, r);
	dat[k].resize(r - l);
	merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(), dat[k].begin());
}
int query(int a, int b, int c, int d, int k, int l, int r) {
	if (b <= l || r <= a)return 0;
	if (a <= l&&r <= b) {
		int u = lower_bound(dat[k].begin(), dat[k].end(), d) - dat[k].begin();
		int v = lower_bound(dat[k].begin(), dat[k].end(), c) - dat[k].begin();
		return u - v;
	}
	int lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2);
	int rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r);
	return lb + rb;
}

int main(){
	int n,q;cin>>n>>q;
	stack<P>st;
	rep(i,n){
		scanf("%d",&a[i]);
		while(!st.empty()&&st.top().first<a[i])st.pop();
		if(st.empty())idx[i]=-1;
		else idx[i]=st.top().second;
		st.push(P(a[i],i));
	}
	init(0,0,N);
	rep(i,q){
		int l,r;scanf("%*d%d%d",&l,&r);l--;
		printf("%d\n",query(l,r,-10,l,0,0,N));
	}
}
0